-
Notifications
You must be signed in to change notification settings - Fork 4
/
captainhook.py
executable file
·76 lines (71 loc) · 2.26 KB
/
captainhook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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