Skip to content

Instantly share code, notes, and snippets.

@growtopiajaw
Last active December 13, 2018 18:04
Show Gist options
  • Save growtopiajaw/33d74efa36a681c8a9cfbb5d588f07c4 to your computer and use it in GitHub Desktop.
Save growtopiajaw/33d74efa36a681c8a9cfbb5d588f07c4 to your computer and use it in GitHub Desktop.
GitHub APIv3 release assets uploader without any external dependencies
#!/usr/bin/env python3
import json
import os
import sys
from urllib.parse import urlencode
from urllib.request import Request, urlopen
repo = sys.argv[1]
tag = sys.argv[2]
upload_file = sys.argv[3]
token = os.environ['GITHUB_TOKEN']
url_template = 'https://{}.github.com/repos/' + repo + '/releases'
# Create.
_json = json.loads(urlopen(Request(
url_template.format('api'),
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']
# Upload.
with open(upload_file, 'br') as myfile:
content = myfile.read()
_json = json.loads(urlopen(Request(
url_template.format('uploads') + '/' + 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())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment