Repository navigation
Expand file tree
/
Copy pathgithub.py
More file actions
68 lines (55 loc) · 2.54 KB
/
Copy pathgithub.py
File metadata and controls
68 lines (55 loc) · 2.54 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
66
67
68
import json
import subprocess
from functools import cmp_to_key
from version import version_greater
# リポジトリから最新のStable/Preリリースを取得する
def get_latest_releases(repo: str, require_mpp: bool = False) -> dict:
print(f" -> Fetching release history for {repo}...")
cmd = ["gh", "api", f"repos/{repo}/releases?per_page=30"]
try:
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
releases = json.loads(result.stdout)
except Exception as e:
print(f" -> [WARNING] Failed to fetch releases for {repo}: {e}")
return {"stable": None, "pre": None}
valid_stable = []
valid_pre = []
for r in releases:
tag = r.get("tag_name")
is_pre = r.get("prerelease", False)
if require_mpp:
has_mpp = any(a.get("name", "").endswith(".mpp") for a in r.get("assets", []))
if not has_mpp: continue
if is_pre:
valid_pre.append(tag)
else:
valid_stable.append(tag)
def cmp_versions(v1, v2):
if v1 == v2: return 0
return 1 if version_greater(v1, v2) else -1
if valid_stable:
valid_stable.sort(key=cmp_to_key(cmp_versions), reverse=True)
if valid_pre:
valid_pre.sort(key=cmp_to_key(cmp_versions), reverse=True)
return {
"stable": valid_stable[0] if valid_stable else None,
"pre": valid_pre[0] if valid_pre else None
}
# GitHubリリースを作成または更新する
def publish_github_release(tag_name: str, files: list, message: str, title: str, is_prerelease: bool):
release_type = "Pre-release" if is_prerelease else "Stable release"
print(f" -> Publishing {release_type}: {tag_name}...")
check_cmd = ["gh", "release", "view", tag_name]
res = subprocess.run(check_cmd, capture_output=True)
if res.returncode == 0:
print(" -> Release already exists. Uploading assets to the existing release...")
subprocess.run(["gh", "release", "upload", tag_name] + files + ["--clobber"], check=True)
else:
print(" -> Creating new release...")
cmd_create = ["gh", "release", "create", tag_name] + files + ["-t", title, "-n", message]
if is_prerelease: cmd_create.append("--prerelease")
try:
subprocess.run(cmd_create, check=True)
except subprocess.CalledProcessError:
print(" -> Release creation failed (race condition). Falling back to upload...")
subprocess.run(["gh", "release", "upload", tag_name] + files + ["--clobber"], check=True)