-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_release.py
49 lines (43 loc) · 1.34 KB
/
auto_release.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
#!/usr/bin/env python3
import json
import os
import sys
from urllib.parse import urlencode
from urllib.request import Request, urlopen
if __name__ == "__main__":
owner = sys.argv[1]
repo = sys.argv[2]
tag = sys.argv[3]
upload_file = sys.argv[4]
token = os.environ['GITHUB_TOKEN']
api_url_template = f'https://api.github.com/repos/{owner}/{repo}/releases'
uploads_url_template = f'https://uploads.github.com/repos/{owner}/{repo}/releases'
# Create.
_json = json.loads(urlopen(Request(
api_url_template,
json.dumps({
'tag_name': tag,
'name': tag,
'prerelease': True,
}).encode(),
headers={
'Accept': 'application/vnd.github.v3+json',
'Authorization': 'token ' + token,
},
)).read().decode())
release_id = _json['id']
print(_json)
# Upload.
with open(upload_file, 'br') as myfile:
content = myfile.read()
_json = json.loads(urlopen(Request(
uploads_url_template + '/' + str(release_id) + '/assets?'
+ urlencode({'name': os.path.split(upload_file)[1]}),
content,
headers={
'Accept': 'application/vnd.github.v3+json',
'Authorization': 'token ' + token,
'Content-Type': 'application/zip',
},
)).read().decode())
print(_json)