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 <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Sonnet 4.6 committed 2026-04-08 10:59:46 +02:00
1 parent 312233aa42
commit 5e970ff5bd
6 files changed
+160

No files matched your search

@@ -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
+11
View File
@@ -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)
View File
Whitespace-only changes.
+59
View File
@@ -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()
View File
Whitespace-only changes.
+89
View File
@@ -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)