-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub_runtime.py
More file actions
221 lines (187 loc) · 6.69 KB
/
github_runtime.py
File metadata and controls
221 lines (187 loc) · 6.69 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python3
"""Runtime helpers for Legends GitHub headless workflows."""
from __future__ import annotations
import json
import os
import re
import shutil
import subprocess
import importlib.util
from pathlib import Path
from typing import Any
GITHUB_REMOTE_RE = re.compile(r"github\.com[:/](?P<owner>[^/]+)/(?P<repo>[^/]+?)(?:\.git)?$")
def have_command(name: str) -> bool:
"""Return whether a command is available."""
return shutil.which(name) is not None
def pillow_available() -> bool:
"""Return whether Pillow is importable in the current Python runtime."""
return importlib.util.find_spec("PIL") is not None
def run_command(args: list[str], cwd: Path | None = None, check: bool = True) -> subprocess.CompletedProcess[str]:
"""Run a subprocess and return the completed process."""
completed = subprocess.run(
args,
cwd=str(cwd) if cwd else None,
capture_output=True,
text=True,
encoding="utf-8",
errors="replace",
check=False,
)
if check and completed.returncode != 0:
raise RuntimeError(completed.stderr.strip() or completed.stdout.strip() or f"Command failed: {' '.join(args)}")
return completed
def gh_auth_ok() -> bool:
"""Return whether GitHub CLI appears authenticated."""
if not have_command("gh"):
return False
result = run_command(["gh", "auth", "status"], check=False)
return result.returncode == 0
def find_git_root(start: Path) -> Path | None:
"""Resolve the git repo root containing the given path."""
current = start.resolve()
if current.is_file():
current = current.parent
while True:
if (current / ".git").exists():
return current
if current.parent == current:
return None
current = current.parent
def parse_remote_slug(remote_url: str) -> str | None:
"""Parse owner/repo from a GitHub remote URL."""
match = GITHUB_REMOTE_RE.search(remote_url.strip())
if not match:
return None
return f"{match.group('owner')}/{match.group('repo')}"
def repo_slug_from_git(repo_root: Path) -> str | None:
"""Resolve owner/repo from git remote origin."""
if not have_command("git"):
return None
result = run_command(["git", "remote", "get-url", "origin"], cwd=repo_root, check=False)
if result.returncode != 0:
return None
return parse_remote_slug(result.stdout.strip())
def resolve_repo_root(path_arg: str | None = None) -> Path:
"""Resolve a local repo root from an explicit path or the current working directory."""
candidate = Path(path_arg).expanduser().resolve() if path_arg else Path.cwd().resolve()
repo_root = find_git_root(candidate)
if repo_root is None:
raise ValueError(f"Could not find a git repo from {candidate}")
return repo_root
def load_env_file(path: Path) -> dict[str, str]:
"""Load one dotenv-style file."""
values: dict[str, str] = {}
if not path.exists():
return values
for line in path.read_text(encoding="utf-8").splitlines():
stripped = line.strip()
if not stripped or stripped.startswith("#") or "=" not in stripped:
continue
key, value = stripped.split("=", 1)
values[key.strip()] = value.strip().strip('"').strip("'")
return values
def resolve_kie_api_key(repo_root: Path) -> tuple[str, str]:
"""Resolve KIE API key from env or standard dotenv locations."""
if os.environ.get("KIE_API_KEY", "").strip():
return os.environ["KIE_API_KEY"].strip(), "env:KIE_API_KEY"
skill_root = Path(__file__).resolve().parents[1]
search_paths = [
repo_root / ".env.local",
repo_root / ".env",
skill_root / ".env.local",
skill_root / ".env",
Path.home() / ".env.local",
Path.home() / ".env",
]
for path in search_paths:
values = load_env_file(path)
key = values.get("KIE_API_KEY", "").strip()
if key:
return key, str(path)
return "", ""
def gh_repo_view(repo_slug: str) -> dict[str, Any] | None:
"""Fetch repository metadata through gh if available."""
if not gh_auth_ok():
return None
fields = [
"name",
"description",
"url",
"homepageUrl",
"repositoryTopics",
"visibility",
"defaultBranchRef",
"licenseInfo",
"stargazerCount",
"forkCount",
"watchers",
"primaryLanguage",
"createdAt",
"updatedAt",
"isArchived",
"isFork",
"parent",
"hasIssuesEnabled",
"hasWikiEnabled",
"hasDiscussionsEnabled",
"hasProjectsEnabled",
"isSecurityPolicyEnabled",
"usesCustomOpenGraphImage",
]
result = run_command(["gh", "repo", "view", repo_slug, "--json", ",".join(fields)], check=False)
if result.returncode != 0:
return None
try:
return json.loads(result.stdout)
except json.JSONDecodeError:
return None
def gh_release_rows(repo_slug: str, limit: int = 5) -> list[dict[str, str]]:
"""Fetch a short release listing via gh."""
if not gh_auth_ok():
return []
result = run_command(
[
"gh",
"release",
"list",
"--repo",
repo_slug,
"--limit",
str(limit),
"--json",
"tagName,name,isDraft,isPrerelease,publishedAt",
],
check=False,
)
if result.returncode != 0:
return []
try:
payload = json.loads(result.stdout)
except json.JSONDecodeError:
return []
releases: list[dict[str, str]] = []
for row in payload:
release_type = "Draft" if row.get("isDraft") else "Pre-release" if row.get("isPrerelease") else "Latest"
releases.append(
{
"tag": str(row.get("tagName") or "").strip(),
"title": str(row.get("name") or "").strip(),
"type": release_type,
"published": str(row.get("publishedAt") or "").strip(),
}
)
return releases
def git_recent_commit(repo_root: Path) -> str:
"""Return the most recent commit timestamp if available."""
if not have_command("git"):
return ""
result = run_command(["git", "log", "-1", "--format=%cI"], cwd=repo_root, check=False)
return result.stdout.strip() if result.returncode == 0 else ""
def git_tags(repo_root: Path) -> list[str]:
"""Return local git tags."""
if not have_command("git"):
return []
result = run_command(["git", "tag", "--list"], cwd=repo_root, check=False)
if result.returncode != 0:
return []
return [line.strip() for line in result.stdout.splitlines() if line.strip()]