Created
August 28, 2013 02:17
-
-
Save loopj/6361436 to your computer and use it in GitHub Desktop.
Add HipChat hooks to GitHub repos.
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
#!/usr/bin/env ruby | |
require "httparty" | |
require "json" | |
class Github | |
include HTTParty | |
HIPCHAT_HOOK = { | |
name: "hipchat", | |
active: true, | |
config: { | |
auth_token: ENV["HIPCHAT_TOKEN"], | |
room: ENV["HIPCHAT_ROOM"] | |
}, | |
events: %w{commit_comment fork fork_apply issues issue_comment pull_request push} | |
} | |
base_uri "https://api.github.com" | |
headers "Authorization" => "token #{ENV["GITHUB_OAUTH_TOKEN"]}" | |
def self.get_repos(user) | |
get "/users/#{user}/repos" | |
end | |
def self.add_hipchat_hook(user, repo) | |
response = post "/repos/#{user}/#{repo}/hooks", body: JSON.generate(HIPCHAT_HOOK) | |
post response["test_url"] | |
end | |
def self.get_hooks(user, repo_name) | |
get "/repos/#{user}/#{repo_name}/hooks" | |
end | |
def self.add_all_hipchat_hooks(user) | |
get_repos(user).each do |r| | |
hipchat_hook = get_hooks(user, r["name"]).find {|h| h["name"] == "hipchat"} | |
add_hipchat_hook(user, r["name"]) if hipchat_hook.nil? | |
end | |
end | |
end | |
if ENV["HIPCHAT_TOKEN"].nil? || ENV["HIPCHAT_ROOM"].nil? | |
puts "HIPCHAT_TOKEN and HIPCHAT_ROOM environment variables must be set" | |
exit | |
end | |
if ENV["GITHUB_OAUTH_TOKEN"].nil? | |
puts "GITHUB_OAUTH_TOKEN environment variable must be set" | |
exit | |
end | |
if ARGV.length == 1 | |
Github.add_all_hipchat_hooks(ARGV[0]) | |
elsif ARGV.length == 2 | |
Github.add_hipchat_hook(ARGV[0], ARGV[1]) | |
else | |
puts "Usage: ./hipchat-hooks.rb [user] [repo]" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment