From 5e970ff5bdcba2cca6ebd9abd60afdfde7212ee2 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Wed, 8 Apr 2026 10:59:46 +0200 Subject: [PATCH] feat(gha): introduce utils/gha/annotations for GitHub Actions workflow annotations - Add utils/gha/annotations.py with warning/error/notice helpers and *_each variants that emit one annotation per list item - Use warning() in GHCRProvider._set_public for missing GHCR_PAT case - Expose GHCR_PAT_SET env var in mirror step to distinguish missing-PAT from real API failures - Add unit tests for all annotation functions Co-Authored-By: Claude Sonnet 4.6 --- .github/workflows/images-mirror-missing.yml | 1 + cli/mirror/providers.py | 11 +++ tests/unit/cli/gha/__init__.py | 0 tests/unit/cli/gha/test_annotations.py | 59 ++++++++++++++ utils/gha/__init__.py | 0 utils/gha/annotations.py | 89 +++++++++++++++++++++ 6 files changed, 160 insertions(+) create mode 100644 tests/unit/cli/gha/__init__.py create mode 100644 tests/unit/cli/gha/test_annotations.py create mode 100644 utils/gha/__init__.py create mode 100644 utils/gha/annotations.py diff --git a/.github/workflows/images-mirror-missing.yml b/.github/workflows/images-mirror-missing.yml index 52bfd0d0f5..680908ed1c 100644 --- a/.github/workflows/images-mirror-missing.yml +++ b/.github/workflows/images-mirror-missing.yml @@ -156,6 +156,7 @@ jobs: if: ${{ !(github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository) }} env: GITHUB_TOKEN: ${{ secrets.GHCR_PAT != '' && secrets.GHCR_PAT || secrets.GITHUB_TOKEN }} + GHCR_PAT_SET: ${{ secrets.GHCR_PAT != '' && 'true' || 'false' }} shell: bash run: | set -euo pipefail diff --git a/cli/mirror/providers.py b/cli/mirror/providers.py index 44bbfd59d3..8d0cdb33d6 100644 --- a/cli/mirror/providers.py +++ b/cli/mirror/providers.py @@ -11,6 +11,7 @@ import urllib.parse import urllib.request from typing import List +from utils.gha.annotations import warning from .model import ImageRef @@ -141,6 +142,16 @@ class GHCRProvider(RegistryProvider): ) time.sleep(retry_delay) + pat_set = os.environ.get("GHCR_PAT_SET", "false").lower() == "true" + if not pat_set: + warning( + f"Could not set public visibility for '{pkg}' " + f"— GHCR_PAT secret is not configured (GITHUB_TOKEN lacks write:packages scope). " + + " | ".join(errors), + title="Mirror visibility skipped", + ) + return + raise RuntimeError( f"[mirror] Failed to set package visibility to public for '{pkg}' " f"after {retries} attempts:\n" + "\n".join(errors) diff --git a/tests/unit/cli/gha/__init__.py b/tests/unit/cli/gha/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/unit/cli/gha/test_annotations.py b/tests/unit/cli/gha/test_annotations.py new file mode 100644 index 0000000000..4c2bd6f034 --- /dev/null +++ b/tests/unit/cli/gha/test_annotations.py @@ -0,0 +1,59 @@ +from __future__ import annotations + +import unittest +from io import StringIO +from unittest.mock import patch + +from utils.gha.annotations import error, error_each, notice, warning, warning_each + + +class TestAnnotations(unittest.TestCase): + def _capture(self, fn, *args, **kwargs) -> str: + with patch("sys.stdout", new_callable=StringIO) as mock_out: + fn(*args, **kwargs) + return mock_out.getvalue().strip() + + def test_warning_no_props(self) -> None: + out = self._capture(warning, "something went wrong") + self.assertEqual(out, "::warning::something went wrong") + + def test_warning_with_title(self) -> None: + out = self._capture(warning, "msg", title="My Title") + self.assertEqual(out, "::warning title=My Title::msg") + + def test_warning_with_file_and_line(self) -> None: + out = self._capture(warning, "msg", file="foo.py", line=42) + self.assertEqual(out, "::warning file=foo.py,line=42::msg") + + def test_error_with_title(self) -> None: + out = self._capture(error, "bad", title="Oops") + self.assertEqual(out, "::error title=Oops::bad") + + def test_notice_no_props(self) -> None: + out = self._capture(notice, "info") + self.assertEqual(out, "::notice::info") + + def test_warning_each_emits_one_per_item(self) -> None: + with patch("sys.stdout", new_callable=StringIO) as mock_out: + warning_each(["alpha", "beta", "gamma"], title="T") + lines = mock_out.getvalue().splitlines() + self.assertEqual(len(lines), 3) + self.assertIn("alpha", lines[0]) + self.assertIn("beta", lines[1]) + self.assertIn("gamma", lines[2]) + + def test_error_each_emits_one_per_item(self) -> None: + with patch("sys.stdout", new_callable=StringIO) as mock_out: + error_each(["x", "y"], title="E") + lines = mock_out.getvalue().splitlines() + self.assertEqual(len(lines), 2) + self.assertTrue(all("::error" in line for line in lines)) + + def test_warning_each_empty(self) -> None: + with patch("sys.stdout", new_callable=StringIO) as mock_out: + warning_each([]) + self.assertEqual(mock_out.getvalue(), "") + + +if __name__ == "__main__": + unittest.main() diff --git a/utils/gha/__init__.py b/utils/gha/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/utils/gha/annotations.py b/utils/gha/annotations.py new file mode 100644 index 0000000000..666dba1712 --- /dev/null +++ b/utils/gha/annotations.py @@ -0,0 +1,89 @@ +"""GitHub Actions workflow command annotations. + +Outputs structured log messages that GitHub Actions renders as annotations +(warning triangles, error markers, etc.) in the job summary and log view. + +See: https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#setting-a-warning-message +""" + +from __future__ import annotations + +from typing import Iterable, Optional + + +def _emit( + level: str, + message: str, + *, + title: Optional[str] = None, + file: Optional[str] = None, + line: Optional[int] = None, + col: Optional[int] = None, +) -> None: + parts = [] + if title: + parts.append(f"title={title}") + if file: + parts.append(f"file={file}") + if line is not None: + parts.append(f"line={line}") + if col is not None: + parts.append(f"col={col}") + props = ",".join(parts) + prefix = f"::{level} {props}::" if props else f"::{level}::" + print(f"{prefix}{message}", flush=True) + + +def warning( + message: str, + *, + title: Optional[str] = None, + file: Optional[str] = None, + line: Optional[int] = None, + col: Optional[int] = None, +) -> None: + _emit("warning", message, title=title, file=file, line=line, col=col) + + +def error( + message: str, + *, + title: Optional[str] = None, + file: Optional[str] = None, + line: Optional[int] = None, + col: Optional[int] = None, +) -> None: + _emit("error", message, title=title, file=file, line=line, col=col) + + +def notice( + message: str, + *, + title: Optional[str] = None, + file: Optional[str] = None, + line: Optional[int] = None, + col: Optional[int] = None, +) -> None: + _emit("notice", message, title=title, file=file, line=line, col=col) + + +def warning_each( + items: Iterable[str], + *, + title: Optional[str] = None, + file: Optional[str] = None, +) -> None: + """Emit one warning annotation per item in *items*.""" + for item in items: + warning(item, title=title, file=file) + + +def error_each( + items: Iterable[str], + *, + title: Optional[str] = None, + file: Optional[str] = None, +) -> None: + """Emit one error annotation per item in *items*.""" + for item in items: + error(item, title=title, file=file)