All checks were successful
Validate and publish Kaleido / android (push) Successful in 2m24s
31 lines
2.1 KiB
Python
31 lines
2.1 KiB
Python
"""Publish the checked-in, signed APK using Forgejo's repository-scoped CI token."""
|
|
import hashlib, json, os, pathlib, re, urllib.request, uuid
|
|
ROOT=pathlib.Path(__file__).resolve().parents[1]
|
|
API='https://git.elijahkuntz.com/api/v1/repos/Elijah/Kaleido'
|
|
token=os.environ['KALEIDO_RELEASE_TOKEN']
|
|
version=dict(l.split('=',1) for l in (ROOT/'version.properties').read_text(encoding='utf-8').splitlines() if '=' in l)
|
|
tag='v'+version['versionName']
|
|
ref=os.environ.get('RELEASE_REF','')
|
|
assert ref=='refs/tags/'+tag,'Tag must match version.properties exactly'
|
|
out=ROOT/'distribution'
|
|
def notes():
|
|
"""Only this version's changelog section; the app shows the body as release notes."""
|
|
text=(ROOT/'CHANGELOG.md').read_text(encoding='utf-8')
|
|
m=re.search(r'^## v?'+re.escape(version['versionName'])+r'[ \t]*$(.*?)(?=^## |\Z)',text,re.M|re.S)
|
|
return m.group(1).strip() if m else ''
|
|
assert hashlib.sha256((out/'Kaleido.apk').read_bytes()).hexdigest()==(out/'Kaleido.apk.sha256').read_text().split()[0]
|
|
def call(path,data=None,method=None,content='application/json'):
|
|
req=urllib.request.Request(API+path,data=data,method=method,headers={'Authorization':'token '+token,'Content-Type':content})
|
|
with urllib.request.urlopen(req,timeout=120) as response:return json.load(response)
|
|
try:release=call('/releases/tags/'+tag)
|
|
except urllib.error.HTTPError as e:
|
|
if e.code!=404:raise
|
|
release=call('/releases',json.dumps({'tag_name':tag,'name':'Kaleido '+version['versionName'],'body':notes(),'draft':True,'prerelease':False}).encode(),'POST')
|
|
existing={a['name']:a for a in release.get('assets',[])}
|
|
for name in ['Kaleido.apk','Kaleido.apk.sha256']:
|
|
if name in existing:continue
|
|
boundary='kaleido'+uuid.uuid4().hex
|
|
data=(f'--{boundary}\r\nContent-Disposition: form-data; name="attachment"; filename="{name}"\r\nContent-Type: application/octet-stream\r\n\r\n').encode()+(out/name).read_bytes()+f'\r\n--{boundary}--\r\n'.encode()
|
|
call(f'/releases/{release["id"]}/assets?name='+name,data,'POST','multipart/form-data; boundary='+boundary)
|
|
call('/releases/'+str(release['id']),json.dumps({'draft':False}).encode(),'PATCH')
|
|
print('Published',tag)
|