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
2733class 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