-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_webhook.py
80 lines (73 loc) · 2.94 KB
/
create_webhook.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 config
import json
import requests
import sys
"""
Copyright (c) 2020, Cisco Systems, Inc. and/or its affiliates
Creates webhooks in a repo upon release using
GitHub API v3 POST /repos/:owner/:repo/hooks
Requires a file with repo names, one per line,
and a personal access token with access to each repo.
Usage:
python create_webhook.py devnet_repos.txt
"""
def get_webhook(gh_orgname, repo_name, gh_username, gh_api_key):
api_uri = "https://api.github.com/repos/{}/{}/hooks".format(gh_orgname, repo_name)
print(api_uri)
session = requests.Session()
session.auth = (gh_username, gh_api_key)
try:
gethooks = session.get(api_uri)
print(json.dumps(gethooks.json(), indent=4))
except:
print(gethooks.status_code)
print("Response text: {}".format(gethooks.text))
def post_create_webhook(gh_orgname, repo_name, gh_username, gh_api_key, gh_webhook_url, gh_secret):
api_uri = "https://api.github.com/repos/{}/{}/hooks".format(gh_orgname, repo_name)
print("API endpoint: {}".format(api_uri))
print("Username: {}".format(gh_username))
print("API Key: {}".format(gh_api_key))
print("Secret for payload: {}".format(gh_secret))
try:
headers = {'User-Agent': '{}'.format(gh_username),
'Content-Type': 'application/json',
'Authorization': 'token {}'.format(gh_api_key)
}
print(headers)
payload = {
'name': 'web',
'active': True,
'events': ['release'],
'config': {
'url': '{}'.format(gh_webhook_url),
'content_type': 'json',
'secret': '{}'.format(gh_secret),
'insecure_ssl': '0'
}
}
session = requests.Session()
makehooks = requests.Request('POST', api_uri, json=payload, headers=headers).prepare()
resp = session.send(makehooks)
print(resp.status_code)
print(json.dumps(resp.json(), indent=4))
except:
print(resp.status_code)
print("Response text: {}".format(resp.text))
sys.exit()
def main(filename):
if not len(args) == 1:
print("Enter the filename for the file that contains the list of repos, one per line")
return
filename = args[0]
# Read data in from a text list of all LL repo names
repolist = []
with open(filename) as f:
repolist = f.readlines()
for repo in repolist:
repo_name = repo.rstrip('\n')
print("Working on this repo: " + repo_name)
#getresponse = get_webhook(config.gh_orgname, repo_name, config.gh_username, config.gh_api_key)
postresponse = post_create_webhook(config.gh_orgname, repo_name, config.gh_username, config.gh_api_key, config.gh_webhook_url, config.gh_secret)
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))