user1942541 user1942541 -4 years ago 261
reST (reStructuredText) Question

How to: [REST] Get and Set Default Reviewers for a repository in Atlassian Stash (Bitbucket Server) programmatically using REST API

So... my current project includes making a script which will configure repositories programmatically on our Stash server (aka BitBucket server). This includes setting the default reviewers for each repository. Is there a REST API for this?

Answer Source

There is no documented REST API for the default reviewers functionality, at least for the Stash version we are using (4.8.x) and probably the current version. And by "no documented API" I mean not just in the official REST API documentation, but anywhere on the web. Fortunately, there is a secret API that the plugin uses.

Long story short, here are the calls you need to do. They are plain GET, POST and DELETE on url. I will use curl for the examples for brevity:

  • Get all default reviewers for a repository:

Do a GET on the following link:

curl -X GET -u (username):(password) (base_url)/rest/default-reviewers/latest/projects/(project_name)/repos/(repo_name)/conditions

Result will be a JSON list. Each item in the list includes the id of the entry in the list (which is different from user id) followed by information about the default reviewer rule.

  • Add a default reviewer:

Do a POST on the following with the headers and data shown:

curl -X POST -H 'Content-Type: application/json' -u (username):(password) (base_url)/rest/default-reviewers/latest/projects/(project_name)/repos/(repo_name)/condition --data-binary '{"sourceMatcher":{"id":"any","type":{"id":"ANY_REF"}},"targetMatcher":{"id":"refs/heads/master","type":{"id":"BRANCH"}},"reviewers":[{"id":XXXX}],"requiredApprovals":"0"}' 

where XXXX is the user id (user name, e-mail or full name won't work). You can get a user's id using the standard REST API. Aka do a GET on: (base_url)/rest/api/1.0/users?filter=(username, email or full name)

  • Remove a default reviewer:

Do a DELETE on the following

curl -X DELETE -u (username):(password) (base_url)/rest/default-reviewers/latest/projects/(project_name)/repos/(repo_name)/condition/YYY

Where YYY is the entry id in the default reviewers list (not the user id). If you know the user id, email, username, or full name of the user you want to delete, you can get all the default reviewers (see above), find the user you want, and then you can get the entry id of the list item.

One more thing: you can observe the default-reviewers plugin using the POST and DELETE commands using Chrome Inspector ("Network" tab), but the GET command is not used directly by the plugin (the user-facing part of it) and cannot be observed with Chrome (the reason I am making this post is because this GET command might be hard to figure out - it was for me)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download