Created
April 8, 2012 19:53
-
-
Save aaronpk/2339561 to your computer and use it in GitHub Desktop.
Automatically configure post-receive hooks on all your Github repositories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
require 'uri' | |
require 'httpclient' | |
require 'redis' | |
require 'mw-irc' | |
# First, get an access token for your account on the command line | |
# curl -d '{"scopes":["repo"],"note":"Post-Receive Hooks"}' -u 'username:******' https://api.github.com/authorizations | |
# Paste your access token here | |
access_token = '*****' | |
# Set your organization | |
org = 'geoloqi' | |
# Desired post-receive hook | |
post_receive_hook = 'http://example.com/github' | |
# Configure MediaWiki bot to be notified of additions | |
bot = MWIRC::Bot.new :host => 'example.net', :port => 51666 | |
# Redis config for caching known URLs added | |
redis = Redis.new :host => 'localhost', :port => 6379 | |
auth = {"Authorization" => "Token #{access_token}"} | |
base_uri = 'https://api.github.com' | |
redis_key = 'github-hooks' | |
client = HTTPClient.new | |
# Fetch all repos for the organization | |
response = client.get("#{base_uri}/orgs/#{org}/repos", nil, auth) | |
repos = JSON.parse response.body | |
repos.each do |repo| | |
# Skip if we've already seen it | |
next if redis.sismember redis_key, repo['name'] | |
puts '==================================' | |
puts repo['name'] | |
puts "\tfetching web hook list..." | |
response = client.get("#{base_uri}/repos/#{org}/#{repo['name']}/hooks", nil, auth).body | |
# Look for the web hook in the list of hooks for this repo | |
has_web_hook = false | |
hooks = JSON.parse response | |
if !hooks.count && !hooks['message'].nil? | |
puts hooks['message'] | |
else | |
hooks.each do |hook| | |
next unless hook['name'] == 'web' | |
if hook['config']['url'] == post_receive_hook | |
has_web_hook = true | |
redis.sadd redis_key, repo['name'] | |
end | |
puts "#{hook['name']}: #{hook['config']['url']}" | |
end | |
end | |
if !has_web_hook | |
# Add a new web hook | |
puts "Adding new web hook!" | |
response = client.post("#{base_uri}/repos/#{org}/#{repo['name']}/hooks", {'name' => 'web', | |
'active' => true, | |
'config' => { 'url' => post_receive_hook } | |
}.to_json, auth).body | |
puts response | |
redis.sadd redis_key, repo['name'] | |
bot.send "[Github] Added new web hook for #{org}/#{repo['name']}" | |
end | |
puts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment