Skip to content

Commit 0110b97

Browse files
committed
fix resolving tag to hash
1 parent 42231c1 commit 0110b97

2 files changed

Lines changed: 58 additions & 5 deletions

File tree

scripts/tooling/cli/misc/html_report.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from jinja2 import Environment, FileSystemLoader, select_autoescape
2424

25-
from scripts.tooling.lib.github import fetch_compare
25+
from scripts.tooling.lib.github import fetch_compare, resolve_ref_sha
2626
from scripts.tooling.lib.known_good import KnownGood, load_known_good
2727

2828
_LOG = logging.getLogger(__name__)
@@ -73,9 +73,22 @@ def _collect_entries(known_good: KnownGood) -> list[dict[str, Any]]:
7373

7474
def _enrich_with_compare_data(entries: list[dict[str, Any]], token: str) -> None:
7575
for entry in entries:
76-
if not entry.get("owner_repo") or not entry.get("hash") or entry.get("version"):
76+
if not entry.get("owner_repo"):
7777
continue
78-
result = fetch_compare(entry["owner_repo"], entry["hash"], entry["branch"], token)
78+
79+
# Modules are pinned either by commit hash or by release version. A version
80+
# pin (e.g. "0.2.9") is resolved to its tag's commit so it can be compared
81+
# against the branch HEAD just like a hash pin.
82+
base_ref = entry.get("hash")
83+
if not base_ref and entry.get("version"):
84+
base_ref = resolve_ref_sha(entry["owner_repo"], entry["version"], token)
85+
if base_ref:
86+
# Surface the resolved commit as the pinned hash for display/linking.
87+
entry["hash"] = base_ref
88+
if not base_ref:
89+
continue
90+
91+
result = fetch_compare(entry["owner_repo"], base_ref, entry["branch"], token)
7992
if result:
8093
entry["current_hash"] = result.head_sha
8194
entry["behind_by"] = result.ahead_by

scripts/tooling/lib/github.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
_LOG = logging.getLogger(__name__)
2424

2525

26+
def _get_repo(owner_repo: str, token: Optional[str]):
27+
"""Return the PyGithub ``Repository`` for *owner_repo*, authenticated if *token* is set."""
28+
gh = Github(token) if token else Github()
29+
return gh.get_repo(owner_repo)
30+
31+
2632
@dataclass
2733
class CompareResult:
2834
"""Result of comparing a pinned commit against a branch HEAD."""
@@ -54,8 +60,7 @@ def fetch_compare(
5460
Without a token requests are unauthenticated (60 req/h rate limit).
5561
"""
5662
try:
57-
gh = Github(token) if token else Github()
58-
repo = gh.get_repo(owner_repo)
63+
repo = _get_repo(owner_repo, token)
5964
comparison = repo.compare(base_hash, branch)
6065
commits = list(comparison.commits)
6166
head_sha = commits[-1].sha if commits else base_hash
@@ -69,3 +74,38 @@ def fetch_compare(
6974
except Exception as exc: # noqa: BLE001
7075
_LOG.debug("Unexpected error comparing %s: %s", owner_repo, exc)
7176
return None
77+
78+
79+
def resolve_ref_sha(
80+
owner_repo: str,
81+
ref: str,
82+
token: Optional[str] = None,
83+
) -> Optional[str]:
84+
"""Resolve a git ref (tag, branch or SHA) to a commit SHA.
85+
86+
Modules pinned by ``version`` store the bare release string (e.g. ``"0.2.9"``)
87+
while the git tag is usually prefixed with ``v`` (``"v0.2.9"``). Both spellings
88+
are tried so a version pin resolves to the tag's commit.
89+
90+
Args:
91+
owner_repo: GitHub ``owner/repo`` slug (e.g. ``"eclipse-score/baselibs"``).
92+
ref: The tag/branch/version string to resolve.
93+
token: Optional GitHub PAT or ``GITHUB_TOKEN``.
94+
95+
Returns:
96+
The resolved commit SHA, or ``None`` if no candidate ref could be resolved.
97+
"""
98+
candidates = [ref]
99+
if not ref.startswith("v"):
100+
candidates.append(f"v{ref}")
101+
try:
102+
repo = _get_repo(owner_repo, token)
103+
for candidate in candidates:
104+
try:
105+
return repo.get_commit(candidate).sha
106+
except GithubException:
107+
continue
108+
_LOG.debug("Could not resolve ref %r for %s (tried %s)", ref, owner_repo, candidates)
109+
except Exception as exc: # noqa: BLE001
110+
_LOG.debug("Unexpected error resolving ref %s for %s: %s", ref, owner_repo, exc)
111+
return None

0 commit comments

Comments
 (0)