From 4c239e3620dd734b50df6cece928cae9e1fa2c64 Mon Sep 17 00:00:00 2001 From: Minh Vu Date: Thu, 28 May 2026 00:06:41 +0200 Subject: [PATCH] ci: peel annotated tags in lookup-run-id --- ci/tools/lookup-run-id | 2 +- ci/tools/tests/test_lookup_run_id.py | 105 +++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 ci/tools/tests/test_lookup_run_id.py diff --git a/ci/tools/lookup-run-id b/ci/tools/lookup-run-id index b874a6af6e9..bd8ba413974 100755 --- a/ci/tools/lookup-run-id +++ b/ci/tools/lookup-run-id @@ -117,7 +117,7 @@ fi # ── Mode: tag ── echo "Looking up run ID for tag: ${REF} in repository: ${REPOSITORY}" >&2 -if ! COMMIT_SHA=$(git rev-parse "${REF}"); then +if ! COMMIT_SHA=$(git rev-parse "${REF}^{commit}"); then echo "Error: Could not resolve git tag '${REF}' to a commit SHA" >&2 echo "Make sure the tag exists and you have fetched it" >&2 exit 1 diff --git a/ci/tools/tests/test_lookup_run_id.py b/ci/tools/tests/test_lookup_run_id.py new file mode 100644 index 00000000000..951b9a5b007 --- /dev/null +++ b/ci/tools/tests/test_lookup_run_id.py @@ -0,0 +1,105 @@ +from __future__ import annotations + +import os +import shutil +import stat +import subprocess +from pathlib import Path + +GIT = shutil.which("git") +LOOKUP_RUN_ID = Path(__file__).resolve().parents[1] / "lookup-run-id" + + +def _run(cmd: list[str], cwd: Path) -> str: + return subprocess.check_output(cmd, cwd=cwd, text=True).strip() # noqa: S603 + + +def _run_checked(cmd: list[str], cwd: Path) -> None: + subprocess.run(cmd, cwd=cwd, check=True) # noqa: S603 + + +def _init_repo(tmp_path: Path) -> tuple[Path, str]: + assert GIT is not None + repo = tmp_path / "repo" + repo.mkdir() + _run_checked([GIT, "init"], cwd=repo) + _run_checked([GIT, "config", "user.name", "Test User"], cwd=repo) + _run_checked([GIT, "config", "user.email", "test@example.com"], cwd=repo) + (repo / "README.md").write_text("hello\n", encoding="utf-8") + _run_checked([GIT, "add", "README.md"], cwd=repo) + _run_checked([GIT, "commit", "-m", "init"], cwd=repo) + commit_sha = _run([GIT, "rev-parse", "HEAD"], cwd=repo) + _run_checked([GIT, "tag", "-a", "cuda-core-v0.6.0", "-m", "release"], cwd=repo) + return repo, commit_sha + + +def _write_fake_gh(tmp_path: Path, commit_sha: str, tag_name: str) -> Path: + fakebin = tmp_path / "fakebin" + fakebin.mkdir() + gh = fakebin / "gh" + gh.write_text( + f"""#!/usr/bin/env bash +set -euo pipefail + +if [[ "${{1:-}}" == "run" && "${{2:-}}" == "list" ]]; then + shift 2 + commit="" + while [[ $# -gt 0 ]]; do + case "$1" in + --commit) + commit="$2" + shift 2 + ;; + --repo|--workflow|--status|--json|--limit|-R|-w|-s|-L|-b) + shift 2 + ;; + *) + shift + ;; + esac + done + + if [[ "$commit" == "{commit_sha}" ]]; then + cat <&2 +exit 1 +""", + encoding="utf-8", + ) + gh.chmod(gh.stat().st_mode | stat.S_IXUSR) + return fakebin + + +def test_lookup_run_id_should_peel_annotated_tag_to_commit_if_tag_mode(tmp_path): + tag_name = "cuda-core-v0.6.0" + repo, commit_sha = _init_repo(tmp_path) + fakebin = _write_fake_gh(tmp_path, commit_sha, tag_name) + env = os.environ.copy() + env["GH_TOKEN"] = "test-token" # noqa: S105 + env["PATH"] = f"{fakebin}{os.pathsep}{env['PATH']}" + + result = subprocess.run( # noqa: S603 + [str(LOOKUP_RUN_ID), "--tag", tag_name, "NVIDIA/cuda-python"], + cwd=repo, + env=env, + text=True, + capture_output=True, + check=False, + ) + + assert result.returncode == 0, result.stderr + assert result.stdout == "123\n" + assert f"Resolved tag '{tag_name}' to commit: {commit_sha}" in result.stderr