This repository has been archived by the owner on Feb 26, 2018. It is now read-only.
forked from RJ/captainhook
-
Notifications
You must be signed in to change notification settings - Fork 1
/
captainhook.py
executable file
·80 lines (75 loc) · 2.46 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
77
78
79
80
#!/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": "",
"room": "#captainhook",
"server": "irc.example.com",
"port": "6667"
}
}
username = input('Enter github username: ')
password = getpass.getpass("Enter github.com password for '%s': " % (username,))
org = input('Enter github org: ')
server = input("Enter irc server hostname: ")
room = input("Enter #channel::key or #channel: ")
overwrite = input("Overwrite existing hooks? [y/N] ")
overwrite = overwrite == "Y" or overwrite == "y"
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?per_page=100' % (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 = 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
if not overwrite:
## Get all existing hooks
hs = requests.get(hurl, auth=auth)
if not hs.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 overwrite or 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)
else:
print(" Failed:", r)