Skip to content

Instantly share code, notes, and snippets.

@srkiNZ84
Created January 13, 2019 22:57
Show Gist options
  • Save srkiNZ84/cd9b120fe32446d3d6643cc1aabc74c7 to your computer and use it in GitHub Desktop.
Save srkiNZ84/cd9b120fe32446d3d6643cc1aabc74c7 to your computer and use it in GitHub Desktop.
Bash script to update BitBucket webhooks
#!/bin/bash
BB_ACCOUNT_NAME="robot_org"
BB_USERNAME="cleveron"
BB_PASSWORD="password"
NEW_REPO_HOOK_ID="a1b2c3d4e5f6"
declare -a repositoryList=( "package_manifest" )
for repository in "${repositoryList[@]}"
do
echo "Getting all webhooks for repository $repository..."
for webHookId in $( curl -s -u "$BB_USERNAME:$BB_PASSWORD" https://api.bitbucket.org/2.0/repositories/$BB_ACCOUNT_NAME/$repository/hooks | jq -r .values[].uuid ); do
# TODO put filter in place to delete only certain webhooks (e.g. ones with "jenkins" in the name)
# TODO check if hook is already there and has correct URL, in which case no need to do anything
echo "Deleting webhook with ID $webHookId..."
curl -X DELETE -u "$BB_USERNAME:$BB_PASSWORD" https://api.bitbucket.org/2.0/repositories/$BB_ACCOUNT_NAME/$repository/hooks/$webHookId
done
# Create the new webhook
echo "Adding new webhook to repo $repository"
curl -s -X POST -u "$BB_USERNAME:$BB_PASSWORD" -H 'Content-Type: application/json' https://api.bitbucket.org/2.0/repositories/$BB_ACCOUNT_NAME/$repository/hooks -d "
{
\"description\": \"Webhook Description\",
\"url\": \"https://example.com/repoupdate?id=$NEW_REPO_HOOK_ID\",
\"active\": true,
\"events\": [
\"repo:push\",
\"issue:created\",
\"issue:updated\"
]
}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment