Skip to content

Commit b648680

Browse files
committed
Download release artifacts from github.
1 parent 56e1bce commit b648680

File tree

1 file changed

+45
-33
lines changed

1 file changed

+45
-33
lines changed

github_release.py

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import os
99
import sys
1010
import re
11+
import zipfile
1112

1213
from github3 import login
1314
import requests
@@ -37,24 +38,35 @@ def error_exit(msg, code=1):
3738
sys.exit(code)
3839

3940

40-
def download_circleci(circle_build):
41-
""" download build artifacts from circleCI and exit """
42-
print("I: downloading release artifacts")
43-
os.mkdir("_build")
44-
artifacts = requests.get("https://circleci.com/api/v1.1/project/github/gpodder/gpodder-osx-bundle/%s/artifacts" % circle_build).json()
45-
items = set([u["url"] for u in artifacts
46-
if re.match(".+/pythonbase-.+\.zip.*$", u["path"])
47-
or u["path"].endswith("/pythonbase.contents")])
48-
if len(items) == 0:
41+
def download_github(github_workflow):
42+
""" download workflow artifacts from github and exit """
43+
headers = {'Accept': 'application/vnd.github+json', 'Authorization': 'token %s' % github_token}
44+
45+
print("I: downloading release artifacts for workflow %d" % github_workflow)
46+
r = requests.get("https://api.github.com/repos/gpodder/gpodder-osx-bundle/actions/artifacts", headers=headers)
47+
if not r.ok:
48+
print('ERROR: API fetch failed %d %s' % (r.status_code, r.reason))
49+
sys.exit(1)
50+
artifacts = r.json()
51+
artifact = [(a['id'], a['archive_download_url']) for a in artifacts['artifacts'] if a['workflow_run']['id'] == github_workflow]
52+
if len(artifact) != 1:
4953
error_exit("Nothing found to download")
50-
print("D: downloading %s" % items)
51-
for url in items:
52-
print("I: downloading %s" % url)
53-
output = os.path.join("_build", url.split('/')[-1])
54-
with requests.get(url, stream=True) as r:
55-
with open(output, "wb") as f:
56-
for chunk in r.iter_content(chunk_size=1000000):
57-
f.write(chunk)
54+
id, url = artifact[0]
55+
print("I: found artifact %d" % id)
56+
57+
print("I: downloading %s" % url)
58+
os.mkdir("_build")
59+
with requests.get(url, stream=True, headers=headers) as r:
60+
if not r.ok:
61+
print('ERROR: artifact fetch failed %d %s' % (r.status_code, r.reason))
62+
sys.exit(1)
63+
with open('_build/bundle.zip', "wb") as f:
64+
for chunk in r.iter_content(chunk_size=1000000):
65+
f.write(chunk)
66+
print("I: unzipping _build/bundle.zip")
67+
with zipfile.ZipFile('_build/bundle.zip', 'r') as z:
68+
z.extractall('_build')
69+
os.remove('_build/bundle.zip')
5870
checksum()
5971
print("I: download success. Rerun without --download to upload")
6072
sys.exit(0)
@@ -114,7 +126,7 @@ def get_diff_previous_tag(tag, previous_tag):
114126
return "```\n%s\n```" % "".join(diff)
115127

116128

117-
def upload(repo, tag, previous_tag, circle_build):
129+
def upload(repo, tag, previous_tag, github_workflow):
118130
""" create github release (draft) and upload assets """
119131
print("I: creating release %s" % tag)
120132
try:
@@ -139,12 +151,12 @@ def upload(repo, tag, previous_tag, circle_build):
139151

140152
print("I: updating release description with diff")
141153
diff = get_diff_previous_tag(tag, previous_tag)
142-
if circle_build:
143-
build = ("\ncircleCI build [%i](https://circleci.com/gh/gpodder/gpodder-osx-bundle/%i)"
144-
% (circle_build, circle_build))
154+
if github_workflow:
155+
workflow = ("\ngithub workflow [%i](https://github.com/gpodder/gpodder-osx-bundle/actions/runs/%i)"
156+
% (github_workflow, github_workflow))
145157
else:
146-
build = ""
147-
if release.edit(body=diff + build):
158+
workflow = ""
159+
if release.edit(body=diff + workflow):
148160
print("I: updated release description with diff")
149161
else:
150162
error_exit("E: updating release description")
@@ -153,15 +165,15 @@ def upload(repo, tag, previous_tag, circle_build):
153165
if __name__ == "__main__":
154166
parser = argparse.ArgumentParser(description='upload gpodder-osx-bundle artifacts to a github release\n'
155167
'Example usage: \n'
156-
' GITHUB_TOKEN=xxx python github_release.py --download --circle-build 33 --previous-tag base-3.10.0_0 base-3.10.2_0\n'
157-
' GITHUB_TOKEN=xxx python github_release.py --circle-build 33 --previous-tag base-3.10.0_0 base-3.10.2_0\n',
168+
' GITHUB_TOKEN=xxx python github_release.py --download --github-workflow 1234567890 --previous-tag 22.7.27 22.7.28\n'
169+
' GITHUB_TOKEN=xxx python github_release.py --github-workflow 1234567890 --previous-tag 22.7.27 22.7.28\n',
158170
formatter_class=argparse.RawTextHelpFormatter)
159171
parser.add_argument('tag', type=str,
160172
help='gpodder-osx-bundle git tag to create a release from')
161173
parser.add_argument('--download', action='store_true',
162-
help='download artifacts from given circle.ci build number')
163-
parser.add_argument('--circle-build', type=int, required=False,
164-
help='circleCI build number')
174+
help='download artifacts from given github workflow')
175+
parser.add_argument('--github-workflow', type=int, required=False,
176+
help='github workflow number (in URL)')
165177
parser.add_argument('--previous-tag', type=str, required=False,
166178
help='previous github tag for contents comparison')
167179
parser.add_argument('--debug', '-d', action='store_true',
@@ -180,14 +192,14 @@ def upload(repo, tag, previous_tag, circle_build):
180192
repo = gh.repository('gpodder', 'gpodder-osx-bundle')
181193

182194
if args.download:
183-
if not args.circle_build:
184-
error_exit("E: --download requires --circle-build number")
195+
if not args.github_workflow:
196+
error_exit("E: --download requires --github-workflow number")
185197
if os.path.isdir("_build"):
186198
error_exit("E: _build directory exists", -1)
187-
download_circleci(args.circle_build)
199+
download_github(args.github_workflow)
188200
else:
189201
if not os.path.exists("_build"):
190-
error_exit("E: _build directory doesn't exist. Maybe you want to download circleci build artifacts (see Usage)", -1)
202+
error_exit("E: _build directory doesn't exist. Maybe you want to download github workflow artifacts (see Usage)", -1)
191203

192204
checksum()
193-
upload(repo, args.tag, args.previous_tag, args.circle_build)
205+
upload(repo, args.tag, args.previous_tag, args.github_workflow)

0 commit comments

Comments
 (0)