-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub-release-upload.py
79 lines (66 loc) · 2.91 KB
/
github-release-upload.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
import os
import sys
import requests
import urllib3
def getRequiredEnvVar(name):
envVar = os.environ.get(name)
if envVar is None:
print('Required environment variable: ' + name + ' is missing. Exiting...')
sys.exit(1)
return envVar
# Creates a new Github release and returns the ID of the newly created release. Although it returns the existing
# release ID, if the release already exits.
def main():
# Disable SSL warnings
urllib3.disable_warnings()
owner = getRequiredEnvVar('OWNER')
package = getRequiredEnvVar('PACKAGE')
productVersion = getRequiredEnvVar('PRODUCT_VERSION')
githubToken = getRequiredEnvVar('GITHUB_TOKEN')
githubApiBaseUrl = 'https://api.github.com'
headers = {'Authorization': 'token ' + githubToken}
payload = {'tag_name': productVersion, 'name': productVersion}
r = requests.post(githubApiBaseUrl + '/repos/' + owner + '/' + package + '/releases', json=payload, headers=headers)
# print(r.status_code, r.reason)
# print(r.text)
responseJson = r.json()
if r.status_code == 200 or r.status_code == 201:
releaseId = responseJson['id']
if releaseId is None:
# This is unexpected. There is supposed to be a Github release ID in the response JSON.
print('Error! Release ID was not found for the newly created Github release! Response:')
print(r.text)
sys.exit(1)
# We are all good, let's print the ID of the newly created Github release
print(releaseId)
sys.exit(0)
releaseAlreadyExists = False
if r.status_code == 422:
# This is the status code we get, when the release already exists
for error in responseJson['errors']:
if error['code'] == "already_exists":
releaseAlreadyExists = True
break
if not releaseAlreadyExists:
print('Error! Release was expected to already exist, but it does not. Response:')
print(r.text)
sys.exit(2)
r = requests.get(githubApiBaseUrl + '/repos/' + owner + '/' + package + '/releases', headers=headers)
for ver in r.json():
if ver['tag_name'] == productVersion:
releaseId = ver['id']
if releaseId is None:
# This is unexpected. There is supposed to be a Github release ID in the response JSON.
print('Error! Release ID was not found in the existing Github release! Response:')
print(r.text)
sys.exit(1)
# We are all good, let's print the ID of the existing Github release
print(releaseId)
sys.exit(0)
# Github release ID was not found, and the response code was unexpected anyway
print('Unexpected response status code: ', r.status_code)
print('Response message: ')
print(r.text)
sys.exit(3)
if __name__ == "__main__":
main()