bitbucket add notifications to all repositories

121 Views Asked by At

I have more than 100 repositories in BitBucket. I want to subscribe to all notifications to all respositories. I can do it one by one (go to each respository -> '...' option -> manage notifications -> select all notifications)

enter image description here

enter image description here

but maybe someone know a way to do it to all resposiories of a project. Can someone help me? Thanks for your time. Jon

1

There are 1 best solutions below

3
Jeremy Fiel On

If you're using Bitbucket Server, one way to do this is fetch all the repository names and loop through them and send a request to watch all the repos. This query can be further filtered with query parameters defined in the api docs.

https://developer.atlassian.com/server/bitbucket/rest/v811/api-group-repository/#api-api-latest-repos-get

https://developer.atlassian.com/server/bitbucket/rest/v811/api-group-repository/#api-api-latest-projects-projectkey-repos-repositoryslug-watch-post

  • store the output of all the repo names in a variable DATA as an array of objects with keys project and repo. These are the path variables required to create a new watch request
  • loop over each object and POST a request to .../watch
  • in the POST, the X-Atlassian-Token header is required because Atlassian api checks for CSRF validation and will send an error, if omitted. This should only be used for cli requests. https://confluence.atlassian.com/cloudkb/xsrf-check-failed-when-calling-cloud-apis-826874382.html
#!/bin/bash

baseUrl=""
apiKey=""
readarray -t data < <(curl -X GET https://$baseUrl/rest/api/latest/repos \
   -H "accept: application/json" \
   -H "authorization: Bearer $apiKey" \
   -H "cache-control: no-store" \
   | jq -c ' .values[] | {repo: .slug, project: .project.key} ')

for repos in "${data[@]}"

do
repo=$(jq -r ' .repo' <<< $repos)
proj=$(jq -r ' .project' <<< $repos)

echo -e "Processing $repo : $proj"

curl -X POST https://$baseUrl/rest/api/latest/projects/$proj/repos/$repo/watch \
  -H "authorization: Bearer $apiKey" \
  -H "X-Atlassian-Token: no-check"
done;


there is one small issue on Atlassian's side when using the api. The Manage Account > Watched repositories doesn't seem to update after this operation. If you view the actual repository, you will see the Watching icon is updated.

I filed a bug with them https://support.atlassian.com/requests/CA-2562481/