Solution - How to cleanup repo webhooks that are no longer valid

Robert Redd
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 25, 2024

I recently needed to cleanup a bunch of webhooks on our repositories that were basically pointing to an older jenkins url that is no longer valid.  As a result, the logs were full of 404 errors in Bitbucket.  This wasn't really causing a real issue, but the noise in the logs was bothering me.  So, I wrote a bash script to loop through all our repositories and if it found a webhook URL based on a search string, it'd delete it.  In my case. the search string was just "http://" since all of our jenkins servers now use https://.

The bash script is pretty self-explanatory.  define your Bitbucket base url, id/pw, and search string at the top and it'll do the rest.  If you want to test it out first to see what it would delete, just comment out the one line in the script that does the actual api call to delete the webhook.  It'll still print out the webhook it would have deleted.

Enjoy!

remove_webhooks.sh

#!/bin/bash
#set -x

# Replace with your Bitbucket Server URL, username, and password
BITBUCKET_URL="https://bitbucket.company.com"
BITBUCKET_USERNAME="userid"
BITBUCKET_PASSWORD="password"

# Replace with the string you want to search for in webhook URLs
SEARCH_STRING="http://"

# Frunction to retrieve projects
get_projects() {
  curl -s -u "${BITBUCKET_USERNAME}:${BITBUCKET_PASSWORD}" "${BITBUCKET_URL}/rest/api/latest/projects?limit=1000" | jq -r '.values[] | .key'
}

# Function to retrieve repositories
get_repositories() {
  curl -s -u "${BITBUCKET_USERNAME}:${BITBUCKET_PASSWORD}" "${BITBUCKET_URL}/rest/api/latest/projects/${1}/repos?limit=1000" | jq -r '.values[] | .slug'
}

# Function to retrieve webhooks for a given repository
get_webhooks() {
  curl -s -u "${BITBUCKET_USERNAME}:${BITBUCKET_PASSWORD}" "${BITBUCKET_URL}/rest/api/latest/projects/${1}/repos/${2}/webhooks?limit=1000" | jq -r '.values[] | .id'
}

get_webhook_url() {
  curl -s -u "${BITBUCKET_USERNAME}:${BITBUCKET_PASSWORD}" "${BITBUCKET_URL}/rest/api/latest/projects/${1}/repos/${2}/webhooks/${3}?limit=1000" | jq -r '.url'
}

delete_webhook() {
  curl -s -u "${BITBUCKET_USERNAME}:${BITBUCKET_PASSWORD}" --request DELETE \
    --url "${BITBUCKET_URL}/rest/api/latest/projects/${1}/repos/${2}/webhooks/${3}"
}

for project in $(get_projects); do
  echo "======================"
  echo "Project: ${project}"
  for repository in $(get_repositories "${project}"); do
    echo "  Repository: ${repository}"
    for webhook_id in $(get_webhooks "${project}" "${repository}"); do
      webhook_url=$(get_webhook_url "${project}" "${repository}" "${webhook_id}")
      if [[ $webhook_url =~ "$SEARCH_STRING" ]]; then
        echo "    Webhook URL: ${webhook_url}"
	echo "    Webhook ID: ${webhook_id}"
	delete_webhook ${project} ${repository} ${webhook_id}
      fi
    done
  done
done

 

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events