fix(codeql): clear quality, Codacy and CodeRabbit findings of PR 22

quality clean.clones in test_editor.py: one _edit helper returns what the editor was handed and the kept entry; _buffer reuses it. Codacy D205/D415 vs quality clean.comment on secret_service_token: its only caller is _forge_access, so it becomes private _secret_service_token without a docstring, as in PR 14. CodeRabbit: select_autoescape matches the full suffix and the renderer passes names ending in .j2, so html.j2, htm.j2 and xml.j2 are enabled; new test renders them escaped and README.md.j2 raw.

| 🌿 Branch | 📊 Diff stat | 🎯 Fix | 💥 Break | 💸 Tokens | ⏱️ Velocity | 🛡️ Security | 🏆 Score | Gate |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| fix/codeql-alerts | 4 files +58/-27 | 93 % | 2 % | – | – | 100 % | 91 | ✅ 90/5, tokens and velocity <= 100, security 100 % |

Evidence: tests/unit + tests/integration with PYTHONPATH=src 881 passed; 2 known gpg-in-sandbox failures in test_publish_integration. quality check --staged 0 findings, ruff clean, MEDUSA 100 %, critical 0. Hub review 41be5d7a0b4eec30.

Refs https://github.com/kevinveenbirkenbach/package-manager/pull/22

Created-with: https://s.veen.world/skills
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kevin Veen-BirkenbachandClaude Opus 5.5 committed 2026-11-30 22:57:30 +00:00
1 parent 0f6583ca48
commit 52f0d804f6
4 files changed
+58 -27

No files matched your search

+2 -9
View File
@@ -54,14 +54,7 @@ class SecretServiceUnavailable(Exception):
pass
def secret_service_token() -> str:
"""
Returns:
The GitHub token from the Secret Service, read without unlocking it.
Raises:
SecretServiceUnavailable: names why no token is available.
"""
def _secret_service_token() -> str:
try:
import secretstorage
except ImportError as exc:
@@ -89,7 +82,7 @@ def secret_service_token() -> str:
def _forge_access() -> tuple[str | None, str | None]:
try:
return secret_service_token(), None
return _secret_service_token(), None
except SecretServiceUnavailable as exc:
return None, str(exc)
@@ -47,7 +47,14 @@ class TemplateRenderer:
loader=FileSystemLoader(self.templates_dir),
undefined=StrictUndefined,
autoescape=select_autoescape(
enabled_extensions=("html", "htm", "xml"),
enabled_extensions=(
"html",
"htm",
"xml",
"html.j2",
"htm.j2",
"xml.j2",
),
default_for_string=False,
default=False,
),
@@ -6,8 +6,8 @@ from pkgmgr.actions.release.files.editor import _open_editor_for_changelog
class TestOpenEditorForChangelog(unittest.TestCase):
def _buffer(self, **kwargs: object) -> str:
"""Returns: what the editor was handed, with the editor itself a no-op."""
def _edit(self, **kwargs: object) -> tuple[str, str]:
"""Returns: what the editor was handed and the kept entry, editor a no-op."""
seen: dict[str, str] = {}
def record(argv: list[str]) -> int:
@@ -18,8 +18,11 @@ class TestOpenEditorForChangelog(unittest.TestCase):
with unittest.mock.patch(
"pkgmgr.actions.release.files.editor.subprocess.call", side_effect=record
):
_open_editor_for_changelog(**kwargs)
return seen["text"]
kept = _open_editor_for_changelog(**kwargs)
return seen["text"], kept
def _buffer(self, **kwargs: object) -> str:
return self._edit(**kwargs)[0]
def test_the_rejection_reaches_the_editor_the_entry_is_fixed_in(self) -> None:
text = self._buffer(
@@ -36,19 +39,7 @@ class TestOpenEditorForChangelog(unittest.TestCase):
self.assertTrue(line.startswith(";"), line)
def test_a_finding_never_lands_in_the_entry(self) -> None:
seen: dict[str, str] = {}
def rewrite(argv: list[str]) -> int:
with open(argv[1], encoding="utf-8") as handle:
seen["text"] = handle.read()
return 0
with unittest.mock.patch(
"pkgmgr.actions.release.files.editor.subprocess.call", side_effect=rewrite
):
kept = _open_editor_for_changelog(
initial_message="- a bullet", findings=["MD036 somewhere"]
)
_, kept = self._edit(initial_message="- a bullet", findings=["MD036 somewhere"])
self.assertEqual(kept, "- a bullet")
def test_without_findings_the_header_stays_as_it_was(self) -> None:
@@ -0,0 +1,40 @@
from __future__ import annotations
import os
import tempfile
import unittest
from unittest.mock import patch
from pkgmgr.actions.repository.create.templates import TemplateRenderer
class TestTemplateAutoescape(unittest.TestCase):
def _render(self, names: list[str]) -> dict[str, str]:
with tempfile.TemporaryDirectory() as tpl, tempfile.TemporaryDirectory() as out:
for name in names:
with open(os.path.join(tpl, name), "w", encoding="utf-8") as fh:
fh.write("{{ value }}")
with patch.object(
TemplateRenderer, "_resolve_templates_dir", return_value=tpl
):
TemplateRenderer().render(
repo_dir=out, context={"value": "<b>"}, preview=False
)
rendered = {}
for name in names:
with open(os.path.join(out, name[:-3]), encoding="utf-8") as fh:
rendered[name[:-3]] = fh.read()
return rendered
def test_markup_templates_escape_and_others_stay_raw(self) -> None:
rendered = self._render(
["page.html.j2", "page.htm.j2", "feed.xml.j2", "README.md.j2"]
)
self.assertEqual(rendered["page.html"], "&lt;b&gt;")
self.assertEqual(rendered["page.htm"], "&lt;b&gt;")
self.assertEqual(rendered["feed.xml"], "&lt;b&gt;")
self.assertEqual(rendered["README.md"], "<b>")
if __name__ == "__main__":
unittest.main()