Skip to content

Instantly share code, notes, and snippets.

@jensim
Last active January 6, 2023 15:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jensim/c157b935f3c3874bd9857509a6b36f82 to your computer and use it in GitHub Desktop.
Save jensim/c157b935f3c3874bd9857509a6b36f82 to your computer and use it in GitHub Desktop.
Add default reviewers to bitbucket-server pull requests where there are currently 0 reviewers
#!/bin/bash
#set -x
set -e
host='https://my-code.local'
if ! which jq ; then
echo 'jq not installed.' >&2
exit 1
fi
username="$(whoami)"
echo "Username ($username):" >&2
read -r username_input
if ! [ "$username_input" = '' ]; then
username="$username_input"
fi
password=''
while [ "$password" = '' ]; do
echo "Password: " >&2
read -s -r password
done
function handle_single_pr(){
pr="$1"
# echo "$pr"
pr_id="$(jq -r '.id' <<< "$pr")"
pr_version="$(jq -r '.version' <<< "$pr")"
to_ref="$(jq -r '.toRef.id' <<< "$pr")"
from_ref="$(jq -r '.fromRef.id' <<< "$pr")"
to_repo="$(jq -r '.toRef.repository.id' <<< "$pr")"
from_repo="$(jq -r '.fromRef.repository.id' <<< "$pr")"
key="$(jq -r '.fromRef.repository.project.key' <<< "$pr")"
repo_slug="$(jq -r '.fromRef.repository.slug' <<< "$pr")"
default_reviewers="$(curl -s -f -u "${username}:${password}" "${host}/rest/default-reviewers/1.0/projects/$key/repos/$repo_slug/reviewers?sourceRefId=${from_ref}&targetRefId=${to_ref}&sourceRepoId=${from_repo}&targetRepoId=${to_repo}")"
default_reviewers_formatted="$(jq -c '[ .[] | {"user":{"name":.name}}]' <<<"$default_reviewers")"
body="{\"id\":${pr_id},\"version\":${pr_version},\"reviewers\":${default_reviewers_formatted}}"
default_reviewers_printable="$(jq -c '[ .[] | .name ]' <<<"$default_reviewers")"
curl -s -f -u "${username}:${password}" -X PUT -H 'Content-Type: application/json' -d "${body}" "${host}/rest/api/1.0/projects/${key}/repos/${repo_slug}/pull-requests/${pr_id}" > /dev/null
echo "PUT default reviewers ${default_reviewers_printable} to pr against repo '${key}:${repo_slug}'"
printf '.'
}
is_last_page=false
start=0
while [ "$is_last_page" == 'false' ] ; do
prs_json="$(curl -s -f -u "${username}:${password}" "${host}/rest/api/1.0/dashboard/pull-requests?limit=10&state=OPEN&role=AUTHOR&start=${start}")"
is_last_page="$(jq -r '.isLastPage' <<< "$prs_json")"
size="$(jq -r '.size' <<< "$prs_json")"
if [ "$size" == 0 ] ; then
echo 'Nothing to do.'
break
else
echo "Working start:$start is_last_page:$is_last_page size:$size"
start=$((size+start))
IFS='
'
for pr in $(jq -c '.values[] | select((.reviewers | length) == 0)' <<< "$prs_json") ; do
handle_single_pr "$pr" &
done
wait
fi
done
echo ""
echo "Done"
@MilesHopkins
Copy link

Hi,
Really sorry to bug you, but this is the only example of getting default reviewers with all the params I've found.
How are you getting/creating the source/targetRefId and the source/targetRepoId? Are they json objects like
{ "id": "branch/name", "repository": { "slug": "repo_name", "name": null, "project": { "key": "REPO_KEY" } } }
Or are they some ID in the bitbucket settings?

Thanks

@jensim
Copy link
Author

jensim commented Feb 17, 2021

No worries! Im glad to help

How are you getting/creating the source/targetRefId and the source/targetRepoId? Are they json objects like

Try this query out in your terminal and use jq to make it a bit more readable =)
https://gist.github.com/jensim/c157b935f3c3874bd9857509a6b36f82#file-add_default_reviewers-sh-L49

curl -s -f -u "${username}:${password}" "${host}/rest/api/1.0/dashboard/pull-requests?limit=10&state=OPEN&role=AUTHOR&start=${start}"

It's a paginated walk of the users open PRs, the ones you would find on the "Your work" page..
This scipt then takes filters the PRs that do not have any reviewers
for pr in $(jq -c '.values[] | select((.reviewers | length) == 0)' <<< "$prs_json") ; do
And the handle_single_pr function then takes the PR-info containing all the source&target info, and asks what the default reviewers of that PRs should be, then sends an update containing those reviewers.

@MilesHopkins
Copy link

Hi, thanks so much for getting back to me, was super helpful in what I was trying to do.

@jensim
Copy link
Author

jensim commented Feb 24, 2021

@MilesHopkins I'm glad you feel that way 🥳

@nspassov
Copy link

nspassov commented Jan 6, 2023

Great work, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment