mirror of
https://github.com/ChristianLight/tutor.git
synced 2025-01-25 22:18:24 +00:00
Upgrade script for pushing github releases
The github-release is no longer maintained by aktau. We need to upgrade because of Github authentication warnings. We also add a custom python script for uploading assets to github. We thought it would be a simple script, but instead we need to deal with deletion of existing assets and releases, so we decide instead to keep relying on that 3rd-party script.
This commit is contained in:
parent
0960449405
commit
fac336e6d7
2
Makefile
2
Makefile
@ -95,7 +95,7 @@ ci-bundle: bundle ## Create bundle and run basic tests
|
||||
./releases/github-release: ## Download github-release binary
|
||||
mkdir -p releases/
|
||||
cd releases/ \
|
||||
&& curl -sSL -o ./github-release.tar.bz2 "https://github.com/aktau/github-release/releases/download/v0.7.2/$(shell uname -s | tr "[:upper:]" "[:lower:]")-amd64-github-release.tar.bz2" \
|
||||
&& curl -sSL -o ./github-release.tar.bz2 "https://github.com/meterup/github-release/releases/download/v0.7.5/$(shell uname -s | tr "[:upper:]" "[:lower:]")-amd64-github-release.tar.bz2" \
|
||||
&& bzip2 -d -f ./github-release.tar.bz2 \
|
||||
&& tar xf github-release.tar \
|
||||
&& mv "bin/$(shell uname -s | tr "[:upper:]" "[:lower:]")/amd64/github-release" .
|
||||
|
83
github-release.py
Executable file
83
github-release.py
Executable file
@ -0,0 +1,83 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
"""
|
||||
This is a quick-and-dirty script to upload release assets to Github via the API.
|
||||
Currently, we do not use this script and rely instead on the github-release binary. This
|
||||
is just in case the github-release binary stops working (as it has before).
|
||||
|
||||
Run script with:
|
||||
|
||||
GITHUB_TOKEN=s3cr3t ./github-release.py v3.00.00 ./dist/tutor-openedx-3.00.00.tar.gz "tutor-$(uname -s)_$(uname -m)"
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import os
|
||||
from urllib.parse import urlencode
|
||||
|
||||
import requests
|
||||
|
||||
HEADERS = {
|
||||
"Accept": "application/vnd.github.v3+json",
|
||||
"Authorization": "token {}".format(os.environ["GITHUB_TOKEN"]),
|
||||
}
|
||||
RELEASES_URL = "https://api.github.com/repos/overhangio/tutor/releases"
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Create github releases and upload assets"
|
||||
)
|
||||
parser.add_argument("tag")
|
||||
parser.add_argument("asset")
|
||||
parser.add_argument("asset_name")
|
||||
args = parser.parse_args()
|
||||
release = get_or_create_release(args.tag)
|
||||
overwrite_asset(args.asset, args.asset_name, release)
|
||||
|
||||
|
||||
def get_or_create_release(tag):
|
||||
# https://developer.github.com/v3/repos/releases/#get-a-release-by-tag-name
|
||||
url = "{}/tags/{}".format(RELEASES_URL, tag)
|
||||
response = requests.get(url)
|
||||
if response.status_code == 200:
|
||||
print("Release {} already exists".format(tag))
|
||||
return response.json()
|
||||
|
||||
print("Creating release {}".format(tag))
|
||||
description = open(
|
||||
os.path.join(os.path.dirname("__file__"), "docs", "_release_description.md")
|
||||
).read()
|
||||
params = {"tag_name": tag, "name": tag, "body": description}
|
||||
# https://developer.github.com/v3/repos/releases/#create-a-release
|
||||
return requests.post(RELEASES_URL, json=params, headers=HEADERS,).json()
|
||||
|
||||
|
||||
def overwrite_asset(asset, asset_name, release):
|
||||
# https://developer.github.com/v3/repos/releases/#list-assets-for-a-release
|
||||
url = "{}/{}/assets".format(RELEASES_URL, release["id"])
|
||||
for existing_asset in requests.get(url).json():
|
||||
if existing_asset["name"] == asset_name:
|
||||
print("Deleting existing asset")
|
||||
# https://developer.github.com/v3/repos/releases/#delete-a-release-asset
|
||||
delete_url = "{}/assets/{}".format(RELEASES_URL, existing_asset["id"])
|
||||
response = requests.delete(delete_url, headers=HEADERS)
|
||||
if response.status_code != 204:
|
||||
print(response, response.content)
|
||||
raise ValueError("Could not delete asset")
|
||||
print("Uploading asset")
|
||||
upload_asset(asset, asset_name, release)
|
||||
|
||||
|
||||
def upload_asset(asset, asset_name, release):
|
||||
upload_url = release["upload_url"].replace(
|
||||
"{?name,label}", "?" + urlencode({"name": asset_name})
|
||||
)
|
||||
files = {"file": (asset_name, open(asset, "rb"))}
|
||||
response = requests.post(upload_url, files=files, headers=HEADERS)
|
||||
if response.status_code > 299:
|
||||
print(response, response.content)
|
||||
raise ValueError("Could not upload asset to release")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
x
Reference in New Issue
Block a user