ci: handle missing Gitea release when syncing release body
This commit is contained in:
@@ -156,16 +156,53 @@ jobs:
|
|||||||
env:
|
env:
|
||||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||||
run: |
|
run: |
|
||||||
|
python3 - <<'PY'
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import urllib.error
|
||||||
|
import urllib.request
|
||||||
|
|
||||||
|
base = "https://gitea.emulab.it/api/v1"
|
||||||
|
repo = "Simone/nsis-plugin-ns7zip"
|
||||||
tag = "${{ github.ref_name }}"
|
tag = "${{ github.ref_name }}"
|
||||||
body=$(cat release_body.md)
|
token = os.environ["GITEA_TOKEN"]
|
||||||
# Get release ID by tag
|
|
||||||
release_id=$(curl -s \
|
with open("release_body.md", "r", encoding="utf-8") as f:
|
||||||
-H "Authorization: token ${GITEA_TOKEN}" \
|
body = f.read()
|
||||||
"https://gitea.emulab.it/api/v1/repos/Simone/nsis-plugin-ns7zip/releases/tags/${tag}" \
|
|
||||||
| python3 -c "import sys,json; print(json.load(sys.stdin)['id'])")
|
def request(method, path, payload=None):
|
||||||
# Update release body
|
data = json.dumps(payload).encode("utf-8") if payload is not None else None
|
||||||
curl -s -X PATCH \
|
req = urllib.request.Request(
|
||||||
-H "Authorization: token ${GITEA_TOKEN}" \
|
f"{base}{path}",
|
||||||
-H "Content-Type: application/json" \
|
data=data,
|
||||||
"https://gitea.emulab.it/api/v1/repos/Simone/nsis-plugin-ns7zip/releases/${release_id}" \
|
method=method,
|
||||||
-d "{\"body\": $(echo "$body" | python3 -c "import sys,json; print(json.dumps(sys.stdin.read()))")}"
|
headers={
|
||||||
|
"Authorization": f"token {token}",
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Accept": "application/json",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
with urllib.request.urlopen(req) as resp:
|
||||||
|
return json.loads(resp.read().decode("utf-8"))
|
||||||
|
|
||||||
|
try:
|
||||||
|
release = request("GET", f"/repos/{repo}/releases/tags/{tag}")
|
||||||
|
rid = release["id"]
|
||||||
|
request("PATCH", f"/repos/{repo}/releases/{rid}", {"body": body})
|
||||||
|
print(f"Updated existing Gitea release id={rid} for tag {tag}")
|
||||||
|
except urllib.error.HTTPError as e:
|
||||||
|
if e.code != 404:
|
||||||
|
raise
|
||||||
|
created = request(
|
||||||
|
"POST",
|
||||||
|
f"/repos/{repo}/releases",
|
||||||
|
{
|
||||||
|
"tag_name": tag,
|
||||||
|
"name": tag,
|
||||||
|
"body": body,
|
||||||
|
"draft": False,
|
||||||
|
"prerelease": False,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
print(f"Created Gitea release id={created['id']} for tag {tag}")
|
||||||
|
PY
|
||||||
|
|||||||
Reference in New Issue
Block a user