gitbitbucketgithooksatlassian-plugin-sdkbitbucket-cloud

Can you develop custom bitbucket remote server hook that can reject pushes?


During my research on how to implement a solution for filtering commits on bitbucket's git repo, i.e. rejecting pushes that introduce invalid changes, I found out about bitbucket's custom plugins and allegedly you can implement custom hooks as java apps that you can connect with bitbucket's git repo. One example: https://blog.developer.atlassian.com/beer-o-clock-stash-plugin-tutorial/.

In this example plugin is used as a part of the custom "Stash" server that is run on the localhost. My question is: could that same custom plugin be used on an existing bitbucket remote repo somehow, and can you point me in a right direction on how to achieve that?


Solution

  • The answer here depends on whether you're using the hosted version of Bitbucket (also known as Bitbucket Cloud; found at bitbucket.org) or an on-premise version of Bitbucket (known either as Bitbucket Server or Bitbucket Datacenter, and formerly known as Stash; found at some other hostname).

    If you're hosting the Bitbucket instance(s), then you can add your own pre-receive and/or update hooks using the documentation at https://confluence.atlassian.com/bitbucketserver/using-repository-hooks-776639836.html. Note that a pre-receive hook runs once per push, and an update hook runs once per branch per push.

    If you're using the Bitbucket that Atlassian hosts, then you cannot add your own pre-receive or update hooks, for security and reliability reasons. (Similar story for github.com and gitlab.com.)

    In either case, instead of a pre-receive hook, you could use a CI/CD system to test that your specific conditions are met, or you could have your team install a tool like pre-commit and implement some checks there, or you could do both.