This repository has been archived by the owner on Mar 22, 2021. It is now read-only.
/
gh.py
executable file
·172 lines (135 loc) · 5.49 KB
/
gh.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python3
"""
Small utility to facilitate updating GitHub commit CI
status: https://developer.github.com/v3/repos/statuses/
Parameters are passed on the command-line. They can be
prefixed by 'env:' to denote a lookup in an environment
variable. E.g. --token env:token.
"""
import os
import sys
import json
import argparse
import requests
import datetime
from simplejson.scanner import JSONDecodeError
class CommitNotFoundException(Exception):
pass
def _main():
"Main entry point."
args = _parse_args()
status(args.repo, args.commit, args.token, args.state,
args.context, args.description, args.url)
def _parse_args():
"""
Parses program arguments and optionally resolves
pointers to environment variables.
"""
parser = argparse.ArgumentParser()
required_args = ['repo', 'commit', 'token', 'state']
optional_args = ['context', 'description', 'url']
for arg in required_args:
parser.add_argument('--' + arg, required=True)
for arg in optional_args:
parser.add_argument('--' + arg)
args = parser.parse_args()
# resolve env vars, possibly to None (only allowed for optional args)
for arg in required_args + optional_args:
val = getattr(args, arg)
if val is not None:
if val.startswith('env:'):
new_val = os.environ.get(val[4:])
if new_val is None and arg in required_args:
parser.error(
"Parameter '%s' is required, but the given "
"environment variable '%s' is missing." % (
arg, val[4:]))
setattr(args, arg, new_val)
# we allow users to pass "" for optional vars to mean None so that
# they don't have to resort to e.g. eval
elif val == "":
if arg in required_args:
parser.error(
"Parameter '%s' is required, but the given "
"argument is empty." % arg)
setattr(args, arg, None)
return args
def status(repo, commit, token, state,
context=None, description=None, url=None):
data = _craft_data_dict(state, context, description, url)
_update_status(repo, commit, token, data)
def _craft_data_dict(state, context, description, url):
"Creates the data dictionary as required by the API."
data = {'state': state}
if context is not None:
data['context'] = context
if description is not None:
data['description'] = description
if url is not None:
data['target_url'] = url
return data
def _update_status(repo, commit, token, data):
"Sends the status update's data using the GitHub API."
header = {'Authorization': 'token ' + token}
api_url = ("https://api.github.com/repos/%s/statuses/%s" %
(repo, commit))
if __name__ == '__main__':
eprint("Updating status of commit", commit, "with data", data)
try:
# use data= instead of json= in case we're running on an older requests
resp = requests.post(api_url, data=json.dumps(data), headers=header)
_print_ratelimit_info(resp)
body = resp.json()
except JSONDecodeError:
eprint("Expected JSON, but received:")
eprint("---")
eprint(resp.content)
eprint("---")
eprint("Retrying...")
resp = requests.post(api_url, data=json.dumps(data), headers=header)
body = resp.json()
# pylint: disable=no-member
if resp.status_code != requests.codes.created:
if (resp.status_code == requests.codes.unprocessable
and body is not None and 'message' in body
and "No commit found for SHA" in body['message']):
raise CommitNotFoundException()
# Some other error happened.
errmsg = "Failed to update commit status [HTTP %d]" % resp.status_code
errmsg += "\n" + str(resp.headers)
if body is not None:
errmsg += "\n" + str(body)
raise Exception(errmsg)
# XXX: add CLI support and deduplicate with status()
def comment(repo, token, issue, text):
"Creates a comment using the GitHub API."
token_header = {'Authorization': 'token ' + token}
api_url = ("https://api.github.com/repos/%s/issues/%d/comments" %
(repo, issue))
data = {'body': text}
# use data= instead of json= in case we're running on an older requests
resp = requests.post(api_url, data=json.dumps(data), headers=token_header)
_print_ratelimit_info(resp)
body = resp.json()
# pylint: disable=no-member
if resp.status_code != requests.codes.created:
errmsg = "Failed to update commit status [HTTP %d]" % resp.status_code
errmsg += "\n" + str(resp.headers)
if body is not None:
errmsg += "\n" + str(body)
raise Exception(errmsg)
def _print_ratelimit_info(resp):
# informational; don't croak if somehow the keys are missing/not int
try:
utc_now = int(datetime.datetime.utcnow().strftime("%s"))
utc_reset = int(resp.headers['X-RateLimit-Reset'])
next_reset = int((utc_reset - utc_now) / 60)
print("X-RateLimit-Remaining: %s/%s (resets in %s mins)" %
(resp.headers['X-RateLimit-Remaining'],
resp.headers['X-RateLimit-Limit'], next_reset))
except Exception as e:
print("Can't print rate limit info: %s" % e)
def eprint(*args):
print(*args, file=sys.stderr)
if __name__ == '__main__':
sys.exit(_main())