Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support BitBucket server's new webhook #436

Closed
nick-george opened this issue Jun 5, 2018 · 7 comments · Fixed by #462
Closed

Support BitBucket server's new webhook #436

nick-george opened this issue Jun 5, 2018 · 7 comments · Fixed by #462

Comments

@nick-george
Copy link

nick-george commented Jun 5, 2018

Hi there,

The BitBucket hook supported by this module is now a paid plugin (https://marketplace.atlassian.com/plugins/com.ngs.stash.externalhooks.external-hooks/server/overview). Since mid 2015, BitBucket Server has had its own implementation of webhooks (https://blog.bitbucket.org/2015/06/24/the-new-bitbucket-webhooks/).

I tried pointing our BitBucket at this module's webhook service and got some interesting errors. It appears that the new protocol is a bit chattier than the one this plugin currently supports. Any chance of having the new webhooks supported?

Also, I haven't tried it yet, but does this module support the BitBucket cloud webhooks (which appear to be implemented differently to the server webhooks)?

Many thanks,
Nick George

@ChetHosey
Copy link

FYI, I was able to get Bitbucket's webhook working by adding a line in /usr/local/bin/webhook to check data['changes'][0]['refId'] for the branch name. For context:

    branch = (
        data['ref']                                          ||  # github & gitlab
        data['refChanges'][0]['refId']            rescue nil ||  # stash
        data['push']['changes'][0]['new']['name'] rescue nil ||  # bitbucket (legacy external)
        data['changes'][0]['refId']               rescue nil ||  # bitbucket (native)
        data['resource']['refUpdates'][0]['name'] rescue nil ||  # TFS/VisualStudio-Git
        data['repository']['default_branch']                     # github tagged release; no ref.
      ).sub('refs/heads/', '') rescue nil

@nick-george
Copy link
Author

Thanks @ChetHosey,

Is that for BitBucket cloud or server?

Thanks,
Nick

@ChetHosey
Copy link

This was tested with Bitbucket server, v5.7.1. Unfortunately I've found that puppet-r10k manages the content of /usr/local/bin/webhook, so a local fix like this is likely to be overwritten.

@nick-george
Copy link
Author

Thanks for that,

I'll just patch the module until an official patch comes out! Do you know if the same fix might work against BitBucket cloud, or are the hooks completely different?

Regards,
Nick

@ChetHosey
Copy link

Sorry, i'm not sure. If you test the webhook using cloud and find out it didn't work, please share a copy of the request body (Bitbucket should let you see the request / response from the last call to a webhook) and I can suggest a manual edit that should work.

@nick-george
Copy link
Author

Hi @ChetHosey ,

Unfortunately I'm unable to test if this works or not with the r10k webhook as I'm using that on a client site that is using BB Server. On my own infrastructure, I'm currently using Jenkins to handle the hooks (which is working fine), but I'd much rather move to a "native" approach.

Looking at the content of a request, it's apparent that the following line will work!

data['push']['changes'][0]['new']['name'] rescue nil ||  # bitbucket (legacy external)

Many thanks for your time!
Nick

For reference, here's the content of the BitBucket cloud hook.

Headers
X-Request-UUID	xxxxxxxx-yyyy-zzzz-wwww-vvvvvvvvvvv
X-Event-Key	repo:push
User-Agent	Bitbucket-Webhooks/2.0
X-Attempt-Number	1
X-Hook-UUID	xxxxxxxx-yyyy-zzzz-wwww-vvvvvvvvvvv
Content-Type	application/json

Body
{
  "push": {
    "changes": [
      {
        "forced": false,
        "old": {
          "type": "branch",
          "name": "development",
          "links": {
            "commits": {
              "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commits/development"
            },
            "self": {
              "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/refs/branches/development"
            },
            "html": {
              "href": "https://bitbucket.org/XXXXXXXXX/puppet/branch/development"
            }
          },
          "target": {
            "hash": "93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7",
            "links": {
              "self": {
                "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commit/93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7"
              },
              "html": {
                "href": "https://bitbucket.org/XXXXXXXXX/puppet/commits/93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7"
              }
            },
            "author": {
              "raw": "FIRST LAST <my@email.address>",
              "type": "author",
              "user": {
                "username": "myUserName",
                "display_name": "FIRSTNAME LASTNAME",
                "account_id": "account ID:my UUID",
                "links": {
                  "self": {
                    "href": "https://api.bitbucket.org/2.0/users/myUserName"
                  },
                  "html": {
                    "href": "https://bitbucket.org/myUserName/"
                  },
                  "avatar": {
                    "href": "https://bitbucket.org/account/myUserName/avatar/"
                  }
                },
                "type": "user",
                "uuid": "{xxxxxxxx-yyyy-zzzz-wwww-vvvvvvvvvvv}"
              }
            },
            "summary": {
              "raw": "Adding auditbeat to the mix\n",
              "markup": "markdown",
              "html": "<p>Adding auditbeat to the mix</p>",
              "type": "rendered"
            },
            "parents": [
              {
                "type": "commit",
                "hash": "36be4ec72c37fd672e795c08f2516036aa18a739",
                "links": {
                  "self": {
                    "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commit/36be4ec72c37fd672e795c08f2516036aa18a739"
                  },
                  "html": {
                    "href": "https://bitbucket.org/XXXXXXXXX/puppet/commits/36be4ec72c37fd672e795c08f2516036aa18a739"
                  }
                }
              }
            ],
            "date": "2018-07-15T00:55:43+00:00",
            "message": "Adding auditbeat to the mix\n",
            "type": "commit"
          }
        },
        "links": {
          "commits": {
            "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commits?include=0556d0c36802c4a491bad2fd139ace77dace1b64&exclude=93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7"
          },
          "html": {
            "href": "https://bitbucket.org/XXXXXXXXX/puppet/branches/compare/0556d0c36802c4a491bad2fd139ace77dace1b64..93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7"
          },
          "diff": {
            "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/diff/0556d0c36802c4a491bad2fd139ace77dace1b64..93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7"
          }
        },
        "truncated": false,
        "commits": [
          {
            "hash": "0556d0c36802c4a491bad2fd139ace77dace1b64",
            "links": {
              "self": {
                "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commit/0556d0c36802c4a491bad2fd139ace77dace1b64"
              },
              "comments": {
                "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commit/0556d0c36802c4a491bad2fd139ace77dace1b64/comments"
              },
              "patch": {
                "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/patch/0556d0c36802c4a491bad2fd139ace77dace1b64"
              },
              "html": {
                "href": "https://bitbucket.org/XXXXXXXXX/puppet/commits/0556d0c36802c4a491bad2fd139ace77dace1b64"
              },
              "diff": {
                "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/diff/0556d0c36802c4a491bad2fd139ace77dace1b64"
              },
              "approve": {
                "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commit/0556d0c36802c4a491bad2fd139ace77dace1b64/approve"
              },
              "statuses": {
                "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commit/0556d0c36802c4a491bad2fd139ace77dace1b64/statuses"
              }
            },
            "author": {
              "raw": "FIRST LAST <my@email.address>",
              "type": "author",
              "user": {
                "username": "myUserName",
                "display_name": "FIRSTNAME LASTNAME",
                "account_id": "account ID:my UUID",
                "links": {
                  "self": {
                    "href": "https://api.bitbucket.org/2.0/users/myUserName"
                  },
                  "html": {
                    "href": "https://bitbucket.org/myUserName/"
                  },
                  "avatar": {
                    "href": "https://bitbucket.org/account/myUserName/avatar/"
                  }
                },
                "type": "user",
                "uuid": "{xxxxxxxx-yyyy-zzzz-wwww-vvvvvvvvvvv}"
              }
            },
            "summary": {
              "raw": "Small fix\n",
              "markup": "markdown",
              "html": "<p>Small fix</p>",
              "type": "rendered"
            },
            "parents": [
              {
                "type": "commit",
                "hash": "93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7",
                "links": {
                  "self": {
                    "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commit/93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7"
                  },
                  "html": {
                    "href": "https://bitbucket.org/XXXXXXXXX/puppet/commits/93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7"
                  }
                }
              }
            ],
            "date": "2018-07-15T00:57:23+00:00",
            "message": "Small fix\n",
            "type": "commit"
          }
        ],
        "created": false,
        "closed": false,
        "new": {
          "type": "branch",
          "name": "development",
          "links": {
            "commits": {
              "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commits/development"
            },
            "self": {
              "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/refs/branches/development"
            },
            "html": {
              "href": "https://bitbucket.org/XXXXXXXXX/puppet/branch/development"
            }
          },
          "target": {
            "hash": "0556d0c36802c4a491bad2fd139ace77dace1b64",
            "links": {
              "self": {
                "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commit/0556d0c36802c4a491bad2fd139ace77dace1b64"
              },
              "html": {
                "href": "https://bitbucket.org/XXXXXXXXX/puppet/commits/0556d0c36802c4a491bad2fd139ace77dace1b64"
              }
            },
            "author": {
              "raw": "FIRST LAST <my@email.address>",
              "type": "author",
              "user": {
                "username": "myUserName",
                "display_name": "FIRSTNAME LASTNAME",
                "account_id": "account ID:my UUID",
                "links": {
                  "self": {
                    "href": "https://api.bitbucket.org/2.0/users/myUserName"
                  },
                  "html": {
                    "href": "https://bitbucket.org/myUserName/"
                  },
                  "avatar": {
                    "href": "https://bitbucket.org/account/myUserName/avatar/"
                  }
                },
                "type": "user",
                "uuid": "{xxxxxxxx-yyyy-zzzz-wwww-vvvvvvvvvvv}"
              }
            },
            "summary": {
              "raw": "Small fix\n",
              "markup": "markdown",
              "html": "<p>Small fix</p>",
              "type": "rendered"
            },
            "parents": [
              {
                "type": "commit",
                "hash": "93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7",
                "links": {
                  "self": {
                    "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commit/93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7"
                  },
                  "html": {
                    "href": "https://bitbucket.org/XXXXXXXXX/puppet/commits/93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7"
                  }
                }
              }
            ],
            "date": "2018-07-15T00:57:23+00:00",
            "message": "Small fix\n",
            "type": "commit"
          }
        }
      }
    ]
  },
  "repository": {
    "scm": "git",
    "website": "",
    "name": "puppet",
    "links": {
      "self": {
        "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet"
      },
      "html": {
        "href": "https://bitbucket.org/XXXXXXXXX/puppet"
      },
      "avatar": {
        "href": "https://bytebucket.org/ravatar/%7Bd93b88cd-cc1b-4bc1-8a92-c4776db50694%7D?ts=767208"
      }
    },
    "project": {
      "links": {
        "self": {
          "href": "https://api.bitbucket.org/2.0/teams/XXXXXXXXX/projects/PROJ"
        },
        "html": {
          "href": "https://bitbucket.org/account/user/XXXXXXXXX/projects/PROJ"
        },
        "avatar": {
          "href": "https://bitbucket.org/account/user/XXXXXXXXX/projects/PROJ/avatar/32"
        }
      },
      "type": "project",
      "uuid": "{xxxxxxxx-yyyy-zzzz-wwww-vvvvvvvvvvv}",
      "key": "PROJ",
      "name": "myProject"
    },
    "full_name": "XXXXXXXXX/puppet",
    "owner": {
      "username": "XXXXXXXXX",
      "type": "team",
      "display_name": "XXXXXXXXX",
      "uuid": "{xxxxxxxx-yyyy-zzzz-wwww-vvvvvvvvvvv}",
      "links": {
        "self": {
          "href": "https://api.bitbucket.org/2.0/teams/XXXXXXXXX"
        },
        "html": {
          "href": "https://bitbucket.org/XXXXXXXXX/"
        },
        "avatar": {
          "href": "https://bitbucket.org/account/XXXXXXXXX/avatar/"
        }
      }
    },
    "type": "repository",
    "is_private": true,
    "uuid": "{xxxxxxxx-yyyy-zzzz-wwww-vvvvvvvvvvv}"
  },
  "actor": {
    "username": "myUserName",
    "display_name": "FIRSTNAME LASTNAME",
    "account_id": "account ID:my UUID",
    "links": {
      "self": {
        "href": "https://api.bitbucket.org/2.0/users/myUserName"
      },
      "html": {
        "href": "https://bitbucket.org/myUserName/"
      },
      "avatar": {
        "href": "https://bitbucket.org/account/myUserName/avatar/"
      }
    },
    "type": "user",
    "uuid": "{xxxxxxxx-yyyy-zzzz-wwww-vvvvvvvvvvv}"
  }
}Headers
X-Request-UUID	xxxxxxxx-yyyy-zzzz-wwww-vvvvvvvvvvv
X-Event-Key	repo:push
User-Agent	Bitbucket-Webhooks/2.0
X-Attempt-Number	1
X-Hook-UUID	xxxxxxxx-yyyy-zzzz-wwww-vvvvvvvvvvv
Content-Type	application/json

Body
{
  "push": {
    "changes": [
      {
        "forced": false,
        "old": {
          "type": "branch",
          "name": "development",
          "links": {
            "commits": {
              "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commits/development"
            },
            "self": {
              "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/refs/branches/development"
            },
            "html": {
              "href": "https://bitbucket.org/XXXXXXXXX/puppet/branch/development"
            }
          },
          "target": {
            "hash": "93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7",
            "links": {
              "self": {
                "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commit/93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7"
              },
              "html": {
                "href": "https://bitbucket.org/XXXXXXXXX/puppet/commits/93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7"
              }
            },
            "author": {
              "raw": "FIRST LAST <my@email.address>",
              "type": "author",
              "user": {
                "username": "myUserName",
                "display_name": "FIRSTNAME LASTNAME",
                "account_id": "account ID:my UUID",
                "links": {
                  "self": {
                    "href": "https://api.bitbucket.org/2.0/users/myUserName"
                  },
                  "html": {
                    "href": "https://bitbucket.org/myUserName/"
                  },
                  "avatar": {
                    "href": "https://bitbucket.org/account/myUserName/avatar/"
                  }
                },
                "type": "user",
                "uuid": "{xxxxxxxx-yyyy-zzzz-wwww-vvvvvvvvvvv}"
              }
            },
            "summary": {
              "raw": "Adding auditbeat to the mix\n",
              "markup": "markdown",
              "html": "<p>Adding auditbeat to the mix</p>",
              "type": "rendered"
            },
            "parents": [
              {
                "type": "commit",
                "hash": "36be4ec72c37fd672e795c08f2516036aa18a739",
                "links": {
                  "self": {
                    "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commit/36be4ec72c37fd672e795c08f2516036aa18a739"
                  },
                  "html": {
                    "href": "https://bitbucket.org/XXXXXXXXX/puppet/commits/36be4ec72c37fd672e795c08f2516036aa18a739"
                  }
                }
              }
            ],
            "date": "2018-07-15T00:55:43+00:00",
            "message": "Adding auditbeat to the mix\n",
            "type": "commit"
          }
        },
        "links": {
          "commits": {
            "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commits?include=0556d0c36802c4a491bad2fd139ace77dace1b64&exclude=93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7"
          },
          "html": {
            "href": "https://bitbucket.org/XXXXXXXXX/puppet/branches/compare/0556d0c36802c4a491bad2fd139ace77dace1b64..93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7"
          },
          "diff": {
            "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/diff/0556d0c36802c4a491bad2fd139ace77dace1b64..93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7"
          }
        },
        "truncated": false,
        "commits": [
          {
            "hash": "0556d0c36802c4a491bad2fd139ace77dace1b64",
            "links": {
              "self": {
                "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commit/0556d0c36802c4a491bad2fd139ace77dace1b64"
              },
              "comments": {
                "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commit/0556d0c36802c4a491bad2fd139ace77dace1b64/comments"
              },
              "patch": {
                "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/patch/0556d0c36802c4a491bad2fd139ace77dace1b64"
              },
              "html": {
                "href": "https://bitbucket.org/XXXXXXXXX/puppet/commits/0556d0c36802c4a491bad2fd139ace77dace1b64"
              },
              "diff": {
                "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/diff/0556d0c36802c4a491bad2fd139ace77dace1b64"
              },
              "approve": {
                "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commit/0556d0c36802c4a491bad2fd139ace77dace1b64/approve"
              },
              "statuses": {
                "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commit/0556d0c36802c4a491bad2fd139ace77dace1b64/statuses"
              }
            },
            "author": {
              "raw": "FIRST LAST <my@email.address>",
              "type": "author",
              "user": {
                "username": "myUserName",
                "display_name": "FIRSTNAME LASTNAME",
                "account_id": "account ID:my UUID",
                "links": {
                  "self": {
                    "href": "https://api.bitbucket.org/2.0/users/myUserName"
                  },
                  "html": {
                    "href": "https://bitbucket.org/myUserName/"
                  },
                  "avatar": {
                    "href": "https://bitbucket.org/account/myUserName/avatar/"
                  }
                },
                "type": "user",
                "uuid": "{xxxxxxxx-yyyy-zzzz-wwww-vvvvvvvvvvv}"
              }
            },
            "summary": {
              "raw": "Small fix\n",
              "markup": "markdown",
              "html": "<p>Small fix</p>",
              "type": "rendered"
            },
            "parents": [
              {
                "type": "commit",
                "hash": "93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7",
                "links": {
                  "self": {
                    "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commit/93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7"
                  },
                  "html": {
                    "href": "https://bitbucket.org/XXXXXXXXX/puppet/commits/93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7"
                  }
                }
              }
            ],
            "date": "2018-07-15T00:57:23+00:00",
            "message": "Small fix\n",
            "type": "commit"
          }
        ],
        "created": false,
        "closed": false,
        "new": {
          "type": "branch",
          "name": "development",
          "links": {
            "commits": {
              "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commits/development"
            },
            "self": {
              "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/refs/branches/development"
            },
            "html": {
              "href": "https://bitbucket.org/XXXXXXXXX/puppet/branch/development"
            }
          },
          "target": {
            "hash": "0556d0c36802c4a491bad2fd139ace77dace1b64",
            "links": {
              "self": {
                "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commit/0556d0c36802c4a491bad2fd139ace77dace1b64"
              },
              "html": {
                "href": "https://bitbucket.org/XXXXXXXXX/puppet/commits/0556d0c36802c4a491bad2fd139ace77dace1b64"
              }
            },
            "author": {
              "raw": "FIRST LAST <my@email.address>",
              "type": "author",
              "user": {
                "username": "myUserName",
                "display_name": "FIRSTNAME LASTNAME",
                "account_id": "account ID:my UUID",
                "links": {
                  "self": {
                    "href": "https://api.bitbucket.org/2.0/users/myUserName"
                  },
                  "html": {
                    "href": "https://bitbucket.org/myUserName/"
                  },
                  "avatar": {
                    "href": "https://bitbucket.org/account/myUserName/avatar/"
                  }
                },
                "type": "user",
                "uuid": "{xxxxxxxx-yyyy-zzzz-wwww-vvvvvvvvvvv}"
              }
            },
            "summary": {
              "raw": "Small fix\n",
              "markup": "markdown",
              "html": "<p>Small fix</p>",
              "type": "rendered"
            },
            "parents": [
              {
                "type": "commit",
                "hash": "93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7",
                "links": {
                  "self": {
                    "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet/commit/93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7"
                  },
                  "html": {
                    "href": "https://bitbucket.org/XXXXXXXXX/puppet/commits/93d8e41b1ee11f2e0758a3f3ee0f0d3ab27114b7"
                  }
                }
              }
            ],
            "date": "2018-07-15T00:57:23+00:00",
            "message": "Small fix\n",
            "type": "commit"
          }
        }
      }
    ]
  },
  "repository": {
    "scm": "git",
    "website": "",
    "name": "puppet",
    "links": {
      "self": {
        "href": "https://api.bitbucket.org/2.0/repositories/XXXXXXXXX/puppet"
      },
      "html": {
        "href": "https://bitbucket.org/XXXXXXXXX/puppet"
      },
      "avatar": {
        "href": "https://bytebucket.org/ravatar/%7Bd93b88cd-cc1b-4bc1-8a92-c4776db50694%7D?ts=767208"
      }
    },
    "project": {
      "links": {
        "self": {
          "href": "https://api.bitbucket.org/2.0/teams/XXXXXXXXX/projects/PROJ"
        },
        "html": {
          "href": "https://bitbucket.org/account/user/XXXXXXXXX/projects/PROJ"
        },
        "avatar": {
          "href": "https://bitbucket.org/account/user/XXXXXXXXX/projects/PROJ/avatar/32"
        }
      },
      "type": "project",
      "uuid": "{xxxxxxxx-yyyy-zzzz-wwww-vvvvvvvvvvv}",
      "key": "PROJ",
      "name": "myProject"
    },
    "full_name": "XXXXXXXXX/puppet",
    "owner": {
      "username": "XXXXXXXXX",
      "type": "team",
      "display_name": "XXXXXXXXX",
      "uuid": "{xxxxxxxx-yyyy-zzzz-wwww-vvvvvvvvvvv}",
      "links": {
        "self": {
          "href": "https://api.bitbucket.org/2.0/teams/XXXXXXXXX"
        },
        "html": {
          "href": "https://bitbucket.org/XXXXXXXXX/"
        },
        "avatar": {
          "href": "https://bitbucket.org/account/XXXXXXXXX/avatar/"
        }
      }
    },
    "type": "repository",
    "is_private": true,
    "uuid": "{xxxxxxxx-yyyy-zzzz-wwww-vvvvvvvvvvv}"
  },
  "actor": {
    "username": "myUserName",
    "display_name": "FIRSTNAME LASTNAME",
    "account_id": "account ID:my UUID",
    "links": {
      "self": {
        "href": "https://api.bitbucket.org/2.0/users/myUserName"
      },
      "html": {
        "href": "https://bitbucket.org/myUserName/"
      },
      "avatar": {
        "href": "https://bitbucket.org/account/myUserName/avatar/"
      }
    },
    "type": "user",
    "uuid": "{xxxxxxxx-yyyy-zzzz-wwww-vvvvvvvvvvv}"
  }
}

@venushka
Copy link
Contributor

venushka commented Nov 24, 2018

Unfortunately the BitBucket Cloud built-in hook content is different from BitBucket Server's build in hook.

The payload definition is published here: https://confluence.atlassian.com/bitbucketserver/event-payload-938025882.html#Eventpayload-Push

This is a sample payload I got tried it out on a test deployment.

{
  "eventKey": "repo:refs_changed",
  "date": "2018-11-24T20:43:06+0000",
  "actor": {
    "name": "testuser",
    "emailAddress": "testuser@mydomain.com",
    "id": 57,
    "displayName": "Test User",
    "active": true,
    "slug": "testuser",
    "type": "NORMAL"
  },
  "repository": {
    "slug": "puppet",
    "id": 13,
    "name": "Puppet",
    "scmId": "git",
    "state": "AVAILABLE",
    "statusMessage": "Available",
    "forkable": true,
    "project": {
      "key": "DEVOPS",
      "id": 43,
      "name": "DevOps",
      "public": false,
      "type": "NORMAL"
    },
    "public": false
  },
  "changes": [
    {
      "ref": {
        "id": "refs/heads/master",
        "displayId": "master",
        "type": "BRANCH"
      },
      "refId": "refs/heads/master",
      "fromHash": "e9f59b74ed261033090f9be81755db01a09aab5f",
      "toHash": "a08698c9a6b1dcd50f49a5dee5bb00d7d583ea57",
      "type": "UPDATE"
    }
  ]
}

BitBucket Server payloads differentiate events based on the eventKey property and based on the available data, seems like we'll have to use this:

    branch = (
        data['ref']                                          ||  # github & gitlab
        data['refChanges'][0]['refId']            rescue nil ||  # stash
        data['push']['changes'][0]['new']['name'] rescue nil ||  # bitbucket
        data['changes'][0]['ref']['displayId']    rescue nil ||  # bitbucket server native
        data['resource']['refUpdates'][0]['name'] rescue nil ||  # TFS/VisualStudio-Git
        data['repository']['default_branch']                     # github tagged release; no ref.
      ).sub('refs/heads/', '') rescue nil

I've tested this with and now our BitBucket server successfully triggers the appropriate environment deployment.

It would be great to include this in to the module than having to use a modified version of the module as it works perfectly so far :) I'll raise a PR for this. PR #462 raised.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants