Remove Github Repo Webhooks by URL

2 minute read Published:

I've built a little script to remove outdated webhooks from your Github repos

Quickly clean up all you repos from outdated Github service webhooks your no longer using.

Do you know this situation? After a while you’ve created a bunch of repositories on Github and added this one nice service that helped you a lot in the beginning. Maybe that was some Contionous Integration service, Jira or whatever else. Anyways, at some point you want to clean up.

I’ve been in this situation just recently and started to click through 5 of our 50 repositories and manually deleted the webhooks for exactly these services. Then I stopped and said:

I need to script this

That’s what I did. And here’s my result. Pretty simple Ruby script. Just install the github_api GEM before.

You can easily create a temporary personal access token on Github here: Creating a personal access token for the command line

require 'github_api'

# Add your personal API token here
github = Github.new oauth_token: 'YOUR_PERSONAL_TOKEN'

# Enter the Regex to search in the webhook's URL
webhook_regex = /versioneye/

# Enter your username or organisation name
username = 'YOUR_USER_OR_ORG_NAME'

# Set this to true if it's an organisation account
is_org = false

repos = if is_org
          github.repos.list org: username, auto_pagination: true
        else
          github.repos.list user: username, auto_pagination: true
        end

repos.each do |repo|
  puts "Checking repo: #{repo.name}"

  github.repos.hooks(
    user: username,
    repo: repo.name
  ).list.each do |hook|
    next unless hook.config.url =~ search_regex
    puts "\tHook found: #{hook.config.url} ... deleting it"

    begin
      github.repos.hooks.delete(
        user: username,
        repo: repo.name,
        id: hook.id
      )
    rescue StandardError => e
      puts e.inspect
    end
  end
end

References

Big ideas in small packages - How microservices helped us to scale our vision

Github

Github Webhooks

github_api

Creating a personal access token for the command line