Dynamically Add Reviewers to a Pull Request

Using this script, you can dynamically add default and mandatory reviewers based on a script when a pull request (PR) is created or updated. 

You can combine a script that sets reviewers with a condition so that you can automate some PR functions, like:

  • Include a senior reviewer from the onsite team when a pull request from an outsourced team is created
  • Include the mentor of a new team member when a PR is created by the new team member
  • Include mandatory reviewers on the pull request, which are determined dynamically based on naming standards and AD groups

Since Bitbucket 4.8 there is a feature that allows you to assign default reviewers for pull requests: BSERV-2924.

These features can work alongside each other without any interference, with one notable caveat. That is, if you add default reviewers using the Bitbucket feature and set a certain number of them to be required, then reviewers added using the ScriptRunner listener are not counted toward the approval requirement. This is consistent with how the Bitbucket server’s default reviewers feature normally behaves. In short, the reviewers that are required to weigh in by Bitbucket server settings always have a say on a pull request.

Dynamically Mandate a Senior Developer to Review a Junior Developer's PRs to the default branch

In the example shown below, when pull requests are made to the default branch, a senior developer is added as a mandatory reviewer if the pull requests are created by junior developers. Otherwise, the senior developer is added by default but can be removed.

Click the '?' to see a list of available binding variables. Some are ScriptRunner built-in closures while others provide links to Java docs. There are also various snippet examples available via the Show snippets link that can help you understand how to use these variables.

Since Senior Dev was added as Mandatory Reviewer, they cannot be removed from the PR. If they are removed, a merge check will not allow the PR to be merged until they have been added back.