You can now vote for scripts to help people know if they're useful or not. Login or create an account to vote!

1

Add Random Reviewers to Pull Request from Group

Overview

Add Random Reviewers to Pull Request from Group.

Example

As a tech leader, I want to add random reviewers to pull requests from my development team group in order to improve the way of sharing the pull request review between the team members. The reviewers should be added after the pull request has been created.

Good to Know

  • This script must be configured as a Custom listener for the PullRequestOpenedEvent event by someone with at least global administrator permission.
  • Use the GROUP_NAME string to set the group name that will be used get the reviewers.
  • Use the PROJECT_KEY and REPOSITORY_NAME strings to specify the project and repository that this script will be executed.
  • Use the NUMBER_OF_USERS integer to set how many reviewers from the group should be added to the pull request.

Requirements

  • Bitbucket Bitbucket (6.3 - 7.12)
  • ScriptRunner for Bitbucket ScriptRunner for Bitbucket (6.11.0)
    
import com.atlassian.bitbucket.pull.PullRequestService import com.atlassian.bitbucket.user.UserService import com.atlassian.bitbucket.user.ApplicationUser import com.atlassian.bitbucket.util.PageUtils import com.atlassian.sal.api.component.ComponentLocator def final GROUP_NAME = 'dev-reviewer-group' def final PROJECT_KEY = 'LIBRARY' def final REPOSITORY_NAME = 'scripts-repository' def final NUMBER_OF_USERS = 2 def final PAGE_SIZE = 1000 def pullRequestService = ComponentLocator.getComponent(PullRequestService) def userService = ComponentLocator.getComponent(UserService) def pullRequest = event.pullRequest if (pullRequest.toRef.repository.project.key != PROJECT_KEY || pullRequest.toRef.repository.name != REPOSITORY_NAME) { return } def usersFromGroup = PageUtils.toStream({ pageRequest -> userService.findUsersByGroup(GROUP_NAME, pageRequest) }, PAGE_SIZE).collect { it as ApplicationUser } def randomUsersFromGroup = getRandomUsersFromGroup(usersFromGroup, NUMBER_OF_USERS, pullRequest.author.user) randomUsersFromGroup.each { user -> pullRequestService.addReviewer(pullRequest.toRef.repository.id, pullRequest.id, user.name) } Set getRandomUsersFromGroup (List users, int numberOfUsers, ApplicationUser prUserAuthor) { def randomUsers = [] as Set while (randomUsers.size() < numberOfUsers) { Collections.shuffle(users) // The PR Author can't be reviewer and to avoid the user being duplicated if (prUserAuthor != users.first() && !randomUsers.contains(users.first())) { randomUsers << users.first() } } randomUsers }
Discovered an issue? Report it here

Suggested for you