This repository has been archived on 2026-11-08. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
StyleForces/.gitea/scripts/release.py
T

36 lines
1.5 KiB
Python

import json
import os
import uuid
import urllib.error
import urllib.parse
import urllib.request
from pathlib import Path
server = 'https://git.ermnvldmr.com/api/v1/repos/' + os.environ['GITHUB_REPOSITORY']
headers = {'Authorization': 'token ' + os.environ['RELEASE_TOKEN']}
tag = os.environ['GITHUB_REF_NAME']
def request(path, data=None, content_type=None):
current = headers.copy()
if content_type:
current['Content-Type'] = content_type
req = urllib.request.Request(server + path, data=data, headers=current)
with urllib.request.urlopen(req, timeout=120) as response:
return json.load(response)
try:
release = request('/releases/tags/' + urllib.parse.quote(tag, safe=''))
except urllib.error.HTTPError as error:
if error.code != 404:
raise
release = request('/releases', json.dumps({'tag_name': tag, 'name': tag, 'draft': False}).encode(), 'application/json')
for artifact in sorted(Path('artifacts').glob('*')):
if not artifact.is_file():
continue
boundary = uuid.uuid4().hex
filename = artifact.name.replace('"', '_').replace('\r', '_').replace('\n', '_')
body = (f'--{boundary}\r\nContent-Disposition: form-data; name="attachment"; filename="{filename}"\r\nContent-Type: application/octet-stream\r\n\r\n'.encode() + artifact.read_bytes() + f'\r\n--{boundary}--\r\n'.encode())
request('/releases/' + str(release['id']) + '/assets?name=' + urllib.parse.quote(filename), body, 'multipart/form-data; boundary=' + boundary)