How to retrieve default reviewers via REST API in Bitbucket Server
Platform notice: Server and Data Center only. This article only applies to Atlassian products on the Server and Data Center platforms.
Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.
*Except Fisheye and Crucible
Purpose
This page describes how to retrieve default reviewers for a pull request in Bitbucket server via REST API queries.
Environment
Applicable for Bitbucket versions < 8.0
Solution
Below are the steps to follow:
Step 1
Add 2 default reviewers in the Repository settings page:
Step 2
Then create a pull request between from test
to master
branch:
Step 3
Fill sourceRepoId
and targetRepoId
fields from the Repository settings:
Step 4
Run the below the SQL query to identify all ref changes for a branch as mentioned in the documentation for Missing Commits in Bitbucket Server to gather the ref changes to put in the sourceRefId
and targetRefId
fields of the API query:
select p.project_key, r.slug, pr.ref_id, pr.change_type, pr.from_hash, pr.to_hash, nu.name from sta_repo_push_ref pr join sta_repo_activity ra on ra.activity_id = pr.activity_id join repository r on r.id = ra.repository_id join project p on p.id = r.project_id join sta_activity a on a.id = pr.activity_id join sta_normal_user nu on nu.user_id = a.user_id where p.project_key = <PROJECT-KEY> and r.slug = <REPOSITORY-SLUG> and pr.ref_id = '<refs/heads/[BRANCH_NAME>' order by a.created_timestamp desc;
The project_key
should be uppercase. For example, the following query can be used to identify all ref changes on from the repository called new-repo
in the FIR
project on the master
branch:
bitbucket_db=# select p.project_key, r.slug, pr.ref_id, pr.change_type, pr.from_hash, pr.to_hash, nu.name
bitbucket_db-# from sta_repo_push_ref pr
bitbucket_db-# join sta_repo_activity ra on ra.activity_id = pr.activity_id
bitbucket_db-# join repository r on r.id = ra.repository_id
bitbucket_db-# join project p on p.id = r.project_id
bitbucket_db-# join sta_activity a on a.id = pr.activity_id
bitbucket_db-# join sta_normal_user nu on nu.user_id = a.user_id
bitbucket_db-# where p.project_key = 'FIR'
bitbucket_db-# and r.slug = 'new-repo'
bitbucket_db-# and pr.ref_id = 'refs/heads/master'
bitbucket_db-# order by a.created_timestamp desc;
project_key | slug | ref_id | change_type | from_hash | to_hash | name
-------------+----------+-------------------+-------------+------------------------------------------+------------------------------------------+---------
FIR | new-repo | refs/heads/master | 3 | 05468ca62c8510a8b4260c67bb29908dc9a68efb | 7245ef691c0ae9d6a78db591eded756ca29c92db | <username>
FIR | new-repo | refs/heads/master | 1 | 0000000000000000000000000000000000000000 | 05468ca62c8510a8b4260c67bb29908dc9a68efb | <username>
(2 rows)
then use the following query can be used to identify all ref changes on from the repository called new-repo
in the FIR
project on the test
branch:
bitbucket_db=# select p.project_key, r.slug, pr.ref_id, pr.change_type, pr.from_hash, pr.to_hash, nu.name
from sta_repo_push_ref pr
join sta_repo_activity ra on ra.activity_id = pr.activity_id
join repository r on r.id = ra.repository_id
join project p on p.id = r.project_id
join sta_activity a on a.id = pr.activity_id
join sta_normal_user nu on nu.user_id = a.user_id
where p.project_key = 'FIR'
and r.slug = 'new-repo'
and pr.ref_id = 'refs/heads/test'
order by a.created_timestamp desc;
project_key | slug | ref_id | change_type | from_hash | to_hash | name
-------------+----------+-----------------+-------------+------------------------------------------+------------------------------------------+---------
FIR | new-repo | refs/heads/test | 3 | 2727eea29f4c496464b7ad94843b439da24b2513 | d9e85a250e601036783d5c7e9aff0388bcabee8d | <username>
FIR | new-repo | refs/heads/test | 1 | 0000000000000000000000000000000000000000 | 2727eea29f4c496464b7ad94843b439da24b2513 | <username>
(2 rows)
Note: The SQL commands are applicable for Postgres Database. If there are using Oracle and other databases, the queries needs to be modified as per the database.
Step 5
Please note that passing commit hashes to sourceRefID and targetRefIDs is supported prior to 8.0. Starting Bitbucket 8. x, commit hashes are no longer considered as a valid RefID. A valid RefID in 8. x is branch name or tag.
To get the default reviewers in 8. x, replace sourceRefID and targetRefIDs with branch names or tags.
curl -u <Bitbucket-username>:<Bitbucket-password> -X GET -H 'Content-Type: application/json' -H 'Accept: application/json' "http://localhost:6760/b760/rest/default-reviewers/1.0/projects/fir/repos/new-repo/reviewers?sourceRepoId=1&targetRepoId=1&sourceRefId=test&targetRefId=master | jq .
Create the REST API query using the following references:
sourceRefId:
latest to_hash
value in the output of the source
branch. In this example, it is the test
branch and the value is d9e85a250e601036783d5c7e9aff0388bcabee8d
targetRefId
: latest to_hash
value in the output from the target
branch. In this example, it is the master
branch and the value is 7245ef691c0ae9d6a78db591eded756ca29c92db
Method 1: Via the command line:
curl -u <Bitbucket-username>:<Bitbucket-password> -X GET -H 'Content-Type: application/json' -H 'Accept: application/json' "http://localhost:6760/b760/rest/default-reviewers/1.0/projects/fir/repos/new-repo/reviewers?sourceRepoId=1&targetRepoId=1&sourceRefId=d9e85a250e601036783d5c7e9aff0388bcabee8d&targetRefId=7245ef691c0ae9d6a78db591eded756ca29c92db" | jq .
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 431 0 431 0 0 5256 0 --:--:-- --:--:-- --:--:-- 5256
[
{
"name": "new-user1",
"emailAddress": "<username>@email.com",
"id": 2,
"displayName": "New User1",
"active": true,
"slug": "new-user1",
"type": "NORMAL",
"links": {
"self": [
{
"href": "http://localhost:6760/b760/users/new-user1"
}
]
}
},
{
"name": "new-user2",
"emailAddress": "<username>@email.com",
"id": 3,
"displayName": "New User2",
"active": true,
"slug": "new-user2",
"type": "NORMAL",
"links": {
"self": [
{
"href": "http://localhost:6760/b760/users/new-user2"
}
]
}
}
]
Method 2: Using the REST API browser plugin
You can use the Atlassian REST API Browser to run queries. It is a very useful tool to find appropriate API queries and testing them with the required parameters. You can directly install it from your Bitbucket instance UI and use it to see all the available options of REST API queries available that you can use with Bitbucket.
Make sure to uncheck the Show only public APIs option to see all the queries. Below screenshot shows the output when you put in the required parameters such as sourceRepoId
, targetRepoId
, sourceRefId
and targetRefId. Please be careful with the syntax of the query to avoid running into issues.