-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_release.py
More file actions
65 lines (54 loc) · 1.87 KB
/
Copy pathcreate_release.py
File metadata and controls
65 lines (54 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from __future__ import annotations
import json
import os
import sys
import urllib.error
import urllib.request
from pathlib import Path
def read_token() -> str:
token = str(os.getenv("GITHUB_TOKEN") or os.getenv("GH_TOKEN") or "").strip()
if token:
return token
raise RuntimeError("Set GITHUB_TOKEN or GH_TOKEN before creating a release.")
def main() -> int:
token = read_token()
repository = str(os.getenv("GITHUB_REPOSITORY") or "odegaard12/Saga-Engine").strip()
body = Path(__file__).with_name("RELEASE_NOTES.md").read_text(encoding="utf-8")
version = Path(__file__).with_name("VERSION").read_text(encoding="utf-8").strip()
if not version:
raise RuntimeError("VERSION must not be empty.")
data = json.dumps(
{
"tag_name": f"v{version}",
"target_commitish": os.getenv("GITHUB_TARGET_BRANCH", "main"),
"name": f"SAGA Engine v{version}",
"body": body,
"draft": False,
"prerelease": False,
}
).encode("utf-8")
req = urllib.request.Request(
f"https://api.github.com/repos/{repository}/releases",
data=data,
method="POST",
headers={
"Authorization": "Bearer " + token,
"Accept": "application/vnd.github+json",
"Content-Type": "application/json",
"User-Agent": "saga-engine-release",
},
)
try:
with urllib.request.urlopen(req) as response:
payload = json.loads(response.read())
except urllib.error.HTTPError as exc:
print(exc.read().decode("utf-8"), file=sys.stderr)
return exc.code or 1
print(payload.get("html_url", "release created"))
return 0
if __name__ == "__main__":
try:
raise SystemExit(main())
except Exception as exc:
print(str(exc), file=sys.stderr)
raise SystemExit(1)