Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Assign PRs randomly to a specific list of users in Bitbucket Cloud

Every pull request needs a reviewer but selecting the right person is something many teams want to automate. After chatting with a few developers and Atlassian teams, I realized that different teams have different ideas about the logic of assigning a reviewer to a PR.

This blog shows how I implemented one of the most popular options so that you can either use it for your team or take it as a starting point for building your team’s preferred logic.

Forge - Atlassian serverless app development platform

The magic for assigning the reviewers to a PR is implemented using Forge, Atlassian serverless app development platform. Everybody with an Atlassian account can use Forge and you install the app right away on a workspace you administer.

You can always create a new workspace for testing the app and then make it available on your team’s workspace at a later stage.

 

What does the app do?

With this app installed, every time a pull request is created a user from a predefined list is randomly selected and added as the PR reviewer. A comment is also added mentioning so that it’s clear that the assignment has been performed by the app.

Let's see it in action:

AssignPR.gif

 

Let’s break down the app into its main components:

  • the fetchRepository function is used to load the current repository. This is required to get the name of the default branch.

  • the .reviewers file is loaded by the fetchFile function and the list of possible reviewers is saved in the reviewers variable. The version of the file is the one at the head of the default branch, whose name has been retrieved in the first step.

  • the getPRAuthor function retrieves the author of the PR

  • the author of the PR is removed from the list of possible reviewers. This is because the author cannot be set as a reviewer.

  • a random number is generated and used to select one of the possible reviewers

  • a reviewer is assigned to the PR via the assignPR function

  • the commentOnPR function adds a comment to the PR and the reviewer is mentioned

 The app is triggered by a PR creation event (avi:bitbucket:created:pullrequest).

 

Let’s have a look at the source code

The repository for this app with the instructions on how to install it is available here: https://bitbucket.org/atlassian/forge-bitbucket-assign-pr-reviewer .

index.js file

The app logic is defined in a single file called index.js which is available in the src folder, here you’ll find all the functions mentioned above.

Because this app is triggered by a PR creation event, the main prtrigger function has access to the event and context parameters which are used throughout the code to access the identifiers for the workspace (event.workspace.uuid), the repository (event.repository.uuid) and the pull request (event.pullrequest.id).

The repository.mainbranch.name retrieved from the fetchRepository function is used as the commit path parameter when calling the /2.0/repositories/${workspaceUuid}/${repoUuid}/src/${commit}/${path}. This is how we retrieve the most recent version of the file on the main branch.

Fetching the file, the PR author, assigning the PR and adding a comment are operations supported by the Bitbucket REST APIs called using the requestBitbucket Forge function.

manifest.yml file

Each Forge app comes with a manifest.yml file that describes it.

For this app, it contains the logic to trigger the index.prtrigger (the prtrigger function in the index.js file) every time a new PR is created. This is done using the avi:bitbucket:created:pullrequest event trigger.

  trigger:
    - key: pullrequest-created-event
      function: main
      events:
        - avi:bitbucket:created:pullrequest
  function:
    - key: main
      handler: index.prtrigger

 

The manifest file also contains all the required scopes for the app to run the REST API endpoints:

permissions:
  scopes:
    - read:pullrequest:bitbucket
    - write:pullrequest:bitbucket
    - read:repository:bitbucket

The read:repository:bitbucket is required for the GET Get a repository and the GET Get file or directory contents endpoint.

For reading the PR author of a PR via the GET Get a pull request endpoint and adding a comment using the POST Create a comment on a pull request endpoint, the read:pullrequest:bitbucket is needed.

Assigning a PR is done using the PUT Update a pull request and requires both the read:pullrequest:bitbucket and the write:pullrequest:bitbucket scope.

 

While this app doesn’t add any extension points to the UI, Forge for Bitbucket provides a multitude of UI modules that can be used to show your app information.

Wrapping up

If you too, like many others, have your own preferred way to select users to review a pull request, you can build your own Forge app to do that. This page walks you through a few implementation details of an app and feel free to tweak it, change it, build on it - whatever you need to make it work for you.

Check the repository for this app and install it on your workspace by following the instructions in the README.

We can’t wait to see what amazing alternatives you can come up with! Let us know in the comments!

 

If you need any help, reach out to our community.

 

Join the Conversation about Forge for Bitbucket

The community group for Forge for Bitbucket is available here.
Multiple Bitbucket customers, Solution and Marketplace partners have already been active in the group and, I'm sure, would love for more activity in the group.

We would love to see you there to collaborate and exchange ideas with others, stay on top of any announcement and changes and provide feedback.

2 comments

Raul Pelaez (External)
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 26, 2024

Bravo Caterina!!

Like Caterina Curti likes this
Raul Pelaez _TecnoFor - Marketplace Partner_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
January 26, 2024

Congrats for the new badge 175 likes!!!

Like Caterina Curti likes this

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events