forked from dainnilsson/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gh-release
executable file
·135 lines (102 loc) · 3.39 KB
/
gh-release
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
#! /usr/bin/env python
from requests import get, post
from subprocess import check_output
from getpass import getpass
import argparse
import json
import sys
import os
CONTENT_TYPES = {
'.tar.gz': 'application/x-gzip',
'.exe': 'application/x-msdownload'
}
def get_url(owner, repo, tag, token):
base = "https://api.github.com"
url = "%s/repos/%s/%s/releases/tags/%s" % (base, owner, repo, tag)
release = get(url).json()
if release.get('id') is None:
release = create_release(owner, repo, tag, token)
print release
return release['upload_url'].replace('{?name,label}', '')
def create_release(owner, repo, tag, token):
base = "https://api.github.com"
url = "%s/repos/%s/%s/releases" % (base, owner, repo)
print "Creating new draft release for: %s" % tag
response = post(
url,
headers={'Authorization': 'token %s' % token},
data=json.dumps({
'tag_name': tag,
'name': tag
})
)
if not response.ok:
print "Error creating release"
print response.text
sys.exit(1)
return response.json()
def get_content_type(fname):
for ext in CONTENT_TYPES:
if fname.endswith(ext):
return CONTENT_TYPES[ext]
return 'application/octet-stream'
def upload(url, token, fname):
print "Uploading %s to %s..." % (fname, url)
with open(fname, 'rb') as f:
payload = f.read()
result = post(
url,
params={'name': os.path.basename(fname)},
headers={'Content-Type': get_content_type(fname),
'Authorization': 'token %s' % token},
data=payload
)
print result.text
print result.json()
if not result.ok:
print "ERROR"
sys.exit(1)
def get_tag():
return check_output(['git', 'describe', '--abbrev=0']).strip()
def get_repo():
output = check_output(['git', 'remote', '-v'])
urls = output.split()[1::3]
for url in urls:
if 'github.com' in url:
url, owner, repo = url.replace(':', '/').rsplit('/', 2)
repo = repo[:-4] # Remove .git
return owner, repo
print "Unable to get GitHub owner and repo from remotes!"
sys.exit(1)
def parse_args():
parser = argparse.ArgumentParser(
description='Upload binaries to GitHub releases.')
parser.add_argument('-t', '--tag', help='name of the tag to upload for')
parser.add_argument('-r', '--repo', help='Github repo "owner/name"')
parser.add_argument('files', nargs='+', metavar='FILE',
help='files to upload')
return parser.parse_args()
def main():
args = parse_args()
args.tag = args.tag or get_tag()
if args.repo:
owner, repo = args.repo.split('/')
else:
owner, repo = get_repo()
print "Files:", args.files
print "Uploading to %s/%s, release: %s" % (owner, repo, args.tag)
if sys.stdin.isatty():
print "You need a GitHub auth token with the public_repo permission set."
print "If you do not have one, create one here:"
print " https://github.com/settings/tokens"
print ""
token = getpass('Enter auth token: ')
else:
token = sys.stdin.read().strip()
url = get_url(owner, repo, args.tag, token)
print "Upload URL: %s" % url
for fname in args.files:
upload(url, token, fname)
print "ALL DONE!"
if __name__ == '__main__':
main()