Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions src/github_repo_auditor/portfolio_truth_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@
def load_release_count_by_name(
*, output_dir: Path, username: str
) -> dict[str, int] | None:
audit_files = sorted(
output_dir.glob(f"audit-report-{username}-*.json"),
key=lambda path: path.stat().st_mtime,
)
if not audit_files:
audit_path = latest_audit_report_path(output_dir=output_dir, username=username)
if audit_path is None:
logging.getLogger(__name__).warning(
"--portfolio-truth-include-release-count requires a prior audit run; "
"no audit-report-%s-*.json found in %s — skipping release_count overlay",
Expand All @@ -34,12 +31,12 @@ def load_release_count_by_name(
)
return None
try:
with audit_files[-1].open() as fh:
with audit_path.open() as fh:
data = json.load(fh)
except Exception as exc: # noqa: BLE001
logging.getLogger(__name__).warning(
"--portfolio-truth-include-release-count: could not read %s: %s — skipping",
audit_files[-1],
audit_path,
exc,
)
return None
Expand All @@ -62,11 +59,8 @@ def load_release_count_by_name(
def load_degraded_dimensions_by_name(
*, output_dir: Path, username: str
) -> dict[str, list[str]] | None:
audit_files = sorted(
output_dir.glob(f"audit-report-{username}-*.json"),
key=lambda path: path.stat().st_mtime,
)
if not audit_files:
audit_path = latest_audit_report_path(output_dir=output_dir, username=username)
if audit_path is None:
logging.getLogger(__name__).warning(
"--portfolio-truth-include-degraded-dimensions requires a prior audit run; "
"no audit-report-%s-*.json found in %s — skipping degraded_dimensions overlay",
Expand All @@ -75,12 +69,12 @@ def load_degraded_dimensions_by_name(
)
return None
try:
with audit_files[-1].open() as fh:
with audit_path.open() as fh:
data = json.load(fh)
except Exception as exc: # noqa: BLE001
logging.getLogger(__name__).warning(
"--portfolio-truth-include-degraded-dimensions: could not read %s: %s — skipping",
audit_files[-1],
audit_path,
exc,
)
return None
Expand All @@ -100,11 +94,11 @@ def load_degraded_dimensions_by_name(


def latest_audit_report_path(*, output_dir: Path, username: str) -> Path | None:
audit_files = sorted(
return max(
output_dir.glob(f"audit-report-{username}-*.json"),
key=lambda path: path.stat().st_mtime,
key=lambda path: (path.stat().st_mtime_ns, path.name),
default=None,
)
return audit_files[-1] if audit_files else None


def _repo_status_entries_from_metadata(
Expand Down
36 changes: 36 additions & 0 deletions tests/test_tied_audit_reports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import json
import os

from github_repo_auditor.portfolio_truth_status import (
latest_audit_report_path,
load_degraded_dimensions_by_name,
load_release_count_by_name,
load_repo_status_from_audit_by_name,
)


def test_tied_audit_reports_choose_the_same_path(tmp_path, monkeypatch):
older, newer = [
tmp_path / f"audit-report-fixture-{day}.json" for day in ("01", "02")
]
for path, count, degraded in ((older, 1, []), (newer, 7, ["testing"])):
audit = {
"metadata": {"name": "example", "archived": path == newer},
"degraded_dimensions": degraded,
"analyzer_results": [
{"dimension": "activity", "details": {"release_count": count}}
],
}
path.write_text(json.dumps({"audits": [audit]}))
os.utime(path, ns=(1000000000, 1000000000))
for order in ([older, newer], [newer, older]):
monkeypatch.setattr(type(tmp_path), "glob", lambda self, pattern: iter(order))
assert (
latest_audit_report_path(output_dir=tmp_path, username="fixture") == newer
)
args = {"output_dir": tmp_path, "username": "fixture"}
assert (
load_repo_status_from_audit_by_name(**args)["example"]["archived"] is True
)
assert load_release_count_by_name(**args) == {"example": 7}
assert load_degraded_dimensions_by_name(**args) == {"example": ["testing"]}
Loading