Use compatible Gitea API client for release publication
All checks were successful
Test and publish WordPress plugin / release (push) Successful in 19s
All checks were successful
Test and publish WordPress plugin / release (push) Successful in 19s
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Publish a tested tag using Gitea Actions' ephemeral repository token."""
|
||||
from pathlib import Path
|
||||
import json, os, re, sys, urllib.error, urllib.request, uuid
|
||||
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', '')
|
||||
@@ -10,22 +11,19 @@ if not re.fullmatch(r'v\d+\.\d+\.\d+', tag):
|
||||
token = os.environ.get('GITEA_TOKEN', '')
|
||||
if not token:
|
||||
sys.exit('Gitea Actions repository token is unavailable.')
|
||||
class NoRedirect(urllib.request.HTTPRedirectHandler):
|
||||
def redirect_request(self, *args):
|
||||
return None
|
||||
opener = urllib.request.build_opener(NoRedirect())
|
||||
def request(method, path, data=None, content_type='application/json'):
|
||||
body = json.dumps(data).encode() if isinstance(data, dict) else data
|
||||
req = urllib.request.Request(base + path, data=body, method=method, headers={
|
||||
'Authorization': 'token ' + token, 'Content-Type': content_type, 'Accept': 'application/json'})
|
||||
try:
|
||||
with opener.open(req, timeout=60) as response:
|
||||
raw = response.read()
|
||||
return response.status, json.loads(raw) if raw else {}
|
||||
except urllib.error.HTTPError as error:
|
||||
if error.code == 404:
|
||||
return 404, {}
|
||||
sys.exit(f'Gitea release API failed: HTTP {error.code} ({method} {path}).')
|
||||
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.')
|
||||
|
||||
Reference in New Issue
Block a user