All checks were successful
Test and publish WordPress plugin / release (push) Successful in 19s
53 lines
2.9 KiB
Python
53 lines
2.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Publish a tested tag using Gitea Actions' ephemeral repository token."""
|
|
from pathlib import Path
|
|
import json, os, re, sys, uuid
|
|
import requests
|
|
root = Path(__file__).resolve().parents[1]
|
|
base = 'https://git.labs.casa/api/v1/repos/josh/jenhomeforms'
|
|
tag = os.environ.get('RELEASE_TAG', '')
|
|
if not re.fullmatch(r'v\d+\.\d+\.\d+', tag):
|
|
sys.exit('Release requires a stable vMAJOR.MINOR.PATCH tag.')
|
|
token = os.environ.get('GITEA_TOKEN', '')
|
|
if not token:
|
|
sys.exit('Gitea Actions repository token is unavailable.')
|
|
def request(method, path, data=None, content_type='application/json'):
|
|
body = json.dumps(data).encode() if isinstance(data, dict) else data
|
|
try:
|
|
response = requests.request(method, base + path, data=body, timeout=60,
|
|
allow_redirects=False, headers={'Authorization': 'token ' + token,
|
|
'Content-Type': content_type, 'Accept': 'application/json'})
|
|
except requests.RequestException:
|
|
sys.exit('Gitea release API connection failed; no credentials are logged.')
|
|
if response.status_code == 404:
|
|
return 404, {}
|
|
if not 200 <= response.status_code < 300:
|
|
sys.exit(f'Gitea release API failed: HTTP {response.status_code} ({method} {path}).')
|
|
return response.status_code, response.json() if response.content else {}
|
|
status, release = request('GET', '/releases/tags/' + tag)
|
|
if status == 200 and not release.get('draft'):
|
|
sys.exit('This version is already published; published releases are not overwritten.')
|
|
if status == 404:
|
|
status, release = request('POST', '/releases', {
|
|
'tag_name': tag, 'target_commitish': os.environ['SOURCE_COMMIT'], 'name': tag,
|
|
'body': (root / 'CHANGELOG.md').read_text(), 'draft': True, 'prerelease': False})
|
|
if status != 201:
|
|
sys.exit('Gitea did not create the draft release.')
|
|
release_id = int(release['id'])
|
|
_, assets = request('GET', f'/releases/{release_id}/assets')
|
|
existing = {asset['name'] for asset in assets}
|
|
for suffix in ['.zip', '.zip.sha256']:
|
|
path = root / 'dist' / ('jenhome-contact-exports-' + tag[1:] + suffix)
|
|
if path.name in existing:
|
|
sys.exit('A draft asset already exists. Review the draft before retrying publication.')
|
|
boundary = 'jenhome-' + uuid.uuid4().hex
|
|
body = (f'--{boundary}\r\nContent-Disposition: form-data; name="attachment"; filename="{path.name}"\r\n'
|
|
'Content-Type: application/octet-stream\r\n\r\n').encode() + path.read_bytes() + f'\r\n--{boundary}--\r\n'.encode()
|
|
status, _ = request('POST', f'/releases/{release_id}/assets?name={path.name}', body, 'multipart/form-data; boundary=' + boundary)
|
|
if status != 201:
|
|
sys.exit('Release asset upload failed; the release remains a draft.')
|
|
status, _ = request('PATCH', f'/releases/{release_id}', {'draft': False})
|
|
if status != 200:
|
|
sys.exit('Release publication failed; review the draft in Gitea.')
|
|
print('Published ' + tag + ' with the plugin ZIP and SHA256 checksum.')
|