Skip to content

Commit

Permalink
Tick.. tick.. tick..
Browse files Browse the repository at this point in the history
  • Loading branch information
RJ committed Mar 4, 2013
0 parents commit 45c29da
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Captain Hook

*Set a github IRC service hook for all repos in your org.*

Sick of manually adding IRC hooks for each new repo. Run this to set
them all at once.

Prompts for the following info when you run it:
* Github username
* Github password
* Github org name
* IRCd hostname
* IRC Channel::password

Then prompts for each org. Check source.
76 changes: 76 additions & 0 deletions captainhook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env python
import sys
import json
import getpass
import requests

hook = {
"name": "irc",
"active": True,
"events": [
"push"
],
"config": {
"branch_regexes": "",
"nick": "github",
"password": "",
"long_url": "1",
"room": "#captainhook",
"server": "irc.example.com",
"port": "6667"
}
}

username = raw_input('Enter github username:')
password = getpass.getpass("Enter github.com password for '%s':" % (username,))
org = raw_input('Enter github org:')
server = raw_input("Enter irc server hostname:")
room = raw_input("Enter #channel::key or #channel:")

hook['config']['server'] = server
hook['config']['room'] = room

auth = requests.auth.HTTPBasicAuth(username, password)

doall = False
r = requests.get('https://api.github.com/orgs/%s/repos' % (org,), auth=auth)
if r.ok:
j = json.loads(r.text or r.content)
for org in j:
name = org['name']
hurl = org['hooks_url']
print name
## Prompt
if not doall:
inp = raw_input("Add hook for %s? [Y/n/a/q] " % (name,))
if inp == "q":
sys.exit(0)
if inp == "a":
doall = True
else:
if not (inp == "" or inp == "y" or inp == "Y"):
continue
## Get all existing hooks
hs = requests.get(hurl, auth=auth)
if not r.ok:
print " Failed: ", name
continue
hj = json.loads(hs.text or hs.content)
## Look for existing hook that matches this one
found = False
for h in hj:
if h['name'] != hook['name']:
continue
if h['config']['room'] == hook['config']['room'] and h['config']['server'] == hook['config']['server'] and h['active']:
found = True
break
## Setup hook, if matching one not found
if not found:
headers = {'Content-type': 'application/json'}
k = requests.post(hurl, auth=auth, data=json.dumps(hook), headers=headers)
if k.ok:
print " Set hook for ", name
else:
print " Failed to set hook for ", name
else:
print " Hook already exists for ", name

0 comments on commit 45c29da

Please sign in to comment.