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. You also have the option to click Example Scripts and copy code from a selection of relevant script examples. 

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.

Retrieving reviewers in a merge check

You can retrieve the list of reviewers added by this built-in script in a merge check.  Merge checks help you write your own rules around which reviewers should approve a PR and when. Use the following code to retrieve a list of reviewers:

groovy
import com.atlassian.bitbucket.pull.PullRequestParticipant import com.atlassian.bitbucket.user.ApplicationUser // Reviewers set by ScriptRunner listeners. Set<ApplicationUser> mandatoryUsers = mergeRequest.pullRequest.autoAddedReviewers.mandatoryUsers Set<ApplicationUser> defaultUsers = mergeRequest.pullRequest.autoAddedReviewers.defaultUsers Set<String> mandatoryGroupNames = mergeRequest.pullRequest.autoAddedReviewers.mandatoryGroups Set<String> defaultGroupNames = mergeRequest.pullRequest.autoAddedReviewers.defaultGroups // All pull request reviewers Set<ApplicationUser> allReviewers = mergeRequest.pullRequest.autoAddedReviewers.allUsers // Or Set<PullRequestParticipant> allReviewersFromPR = mergeRequest.pullRequest.reviewers
On this page