Missed Team ’24? Catch up on announcements here.

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

Add a reviewer to a PR using the REST API and Forge

Every pull request needs a reviewer but who is the right person seems to be one of the most complicated questions to answer in tech :wink:. Every company and developer has a different opinion but everybody wants to automate the PR assignment.

 

This is why I want to show you how to assign a PR to a reviewer in a programmatic way using Forge.

Here is the function that can be used to add a reviewer:

const assignPR = async (workspaceUuid, repoUuid, pullRequestId, reviewer) => {
    const bodyData = {
        "reviewers": [{"account_id": reviewer}]
    };

    try {
        const res = await api
        .asApp()
        .requestBitbucket(route`/2.0/repositories/${workspaceUuid}/${repoUuid}/pullrequests/${pullRequestId}`, {
            method: 'PUT',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(bodyData)
        });
        const data = await res.json();
        return data;
    } catch (error) {
        throw error;
    }
}

 

It uses the PUT Update a pull request REST API endpoint and it takes as input the pull request details, including its workspace and repository, as well as the Atlassian Account ID (also knows as AAID) of the reviewer.

 

The PR author cannot be a reviewer

In Bitbucket, the PR author author cannot be added as a reviewer so you’ll need to make sure that they are not passed as parameter to the request.

If that happens, the request fails with a 400 status code and a "reviewers: Caterina Curti is the author and cannot be included as a reviewer." error message.

 

The reviewer must have access to the repository

If the reviewer doesn’t have access to the repository, the request fails with a 400 status code and a "reviewers: Malformed reviewers list error message".

Check that all users who might be set as reviewers have access to the repository by reviewing the Repository permissions.

 

New to Forge? Want to check a real-life example?

If you would like to try this logic out in a fully working app, I’ve created this one for assigning reviewers automatically when creating a PR. The list of reviewers is defined in each repository via a configuration file that the apps read.

And if you want to read more, check out the documentation and the getting started example.

 

0 comments

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events