-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub_release_refs.py
69 lines (56 loc) · 1.89 KB
/
github_release_refs.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
# -*- coding: utf-8 -*-
import urllib3
import json
import os
import re
import sys
# TODO: only_version doesn't account for semver rc/pre/alpha
only_version = re.compile(r'([0-9]+(\.[0-9]+)+)')
http = urllib3.PoolManager(timeout=urllib3.Timeout(connect=7.0, read=3.0))
headers = {
"accept": "application/json",
"user-agent": "bazel/TODO:<bazel version from env>",
}
if __name__ == '__main__':
url = sys.argv[1]
auth_token = sys.argv[2]
release = sys.argv[3]
if len(sys.argv) != 4:
print("Usage: <url> <token> <name>", file=sys.stderr)
exit(1)
if auth_token:
headers['authorization'] = 'token ' + auth_token
r = http.request('GET', url, headers=headers)
if r.status != 200:
print("{} status: {}".format(url, r.status), file=sys.stderr)
exit(1)
releases = json.loads(r.data.decode('utf-8'))
for rel in releases:
if rel['draft']:
continue
# if rel['prerelease']:
# continue
tag = rel['tag_name']
tag_digits = only_version.findall(tag)
if not tag_digits:
print("no digits in {}".format(tag), file=sys.stderr)
exit(1)
tag_digits = tag_digits[0][0]
expected = release.format(tag=tag, tag_digits=tag_digits)
for asset in rel['assets']:
url = asset['browser_download_url']
name = os.path.basename(url)
if name != expected:
continue
ty = {
'application/x-compressed': 'tar.gz',
'application/x-compressed-tar': 'tar.gz',
'application/x-zip-compressed': 'zip',
'application/zip': 'zip',
}[asset['content_type']]
print("{tag}\t{tag_digits}\t{ty}\t{url}".format(
tag=tag,
tag_digits=tag_digits,
ty=ty,
url=url,
))