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
14 changes: 8 additions & 6 deletions src/github_repo_auditor/portfolio_truth_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,39 @@
from github_repo_auditor.portfolio_cohort_plan_contract import (
COHORT_TRANSITION_PROTOCOL,
)
from github_repo_auditor.portfolio_truth_lineage import resolve_notion_origin
from github_repo_auditor.portfolio_truth_reconcile import (
CohortR3Error,
CohortTransitionOutcome,
build_portfolio_truth_snapshot,
load_prior_notion_context,
normalize_report_clock,
)
from github_repo_auditor.portfolio_truth_render import (
render_portfolio_report_markdown,
render_registry_markdown,
)
from github_repo_auditor.portfolio_truth_lineage import resolve_notion_origin
from github_repo_auditor.portfolio_truth_sources import (
checkout_collision_paths,
workspace_exclusion_reason,
)
from github_repo_auditor.portfolio_truth_types import truth_latest_path
from github_repo_auditor.producer_preflight import (
ProducerEvidence,
verify_evidence_still_current,
)
from github_repo_auditor.portfolio_truth_validate import (
canonicalize_prior_security_truth_payload,
validate_portfolio_report_markdown,
validate_publish_targets,
validate_registry_markdown,
validate_truth_snapshot,
)
from github_repo_auditor.producer_preflight import (
ProducerEvidence,
verify_evidence_still_current,
)
from github_repo_auditor.project_registry import (
build_project_registry,
load_source_paths,
)


_PORTFOLIO_TRUTH_IN_PROCESS_LOCK = threading.Lock()


Expand Down Expand Up @@ -548,6 +548,8 @@ def publish_portfolio_truth(
require_producer_evidence: bool = False,
now: datetime | None = None,
) -> PortfolioTruthPublishResult:
if now is not None:
now = normalize_report_clock(now)
if (
security_coverage_metadata is not None or security_receipt_binding is not None
) and now is None:
Expand Down
8 changes: 7 additions & 1 deletion src/github_repo_auditor/portfolio_truth_reconcile.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,12 @@ def _projects_at_clock(
return result


def normalize_report_clock(now: datetime) -> datetime:
if now.utcoffset() is None:
raise ValueError("The report clock must be timezone-aware.")
return now.astimezone(timezone.utc)


def build_materialization_context(
*,
workspace_root: Path,
Expand All @@ -909,7 +915,7 @@ def build_materialization_context(
prior_notion_generated_at: str | None = None,
) -> MaterializationContext:
"""Load catalog, registry, Notion context, and workspace discovery once."""
now = now or datetime.now(timezone.utc)
now = normalize_report_clock(now or datetime.now(timezone.utc))
catalog_data = load_portfolio_catalog(catalog_path)
legacy_rows = load_legacy_registry_rows(legacy_registry_path)
notion_context = load_safe_notion_project_context() if include_notion else {}
Expand Down
44 changes: 44 additions & 0 deletions tests/test_snapshot_clock_normalization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from datetime import datetime, timedelta, timezone

import pytest

from github_repo_auditor import portfolio_truth_publish as publish
from github_repo_auditor.portfolio_truth_reconcile import build_portfolio_truth_snapshot


def test_snapshot_clock_is_normalized_before_publication(tmp_path):
clock = datetime(2025, 2, 1, 4, 15, tzinfo=timezone(timedelta(hours=4)))
snapshot = build_portfolio_truth_snapshot(
workspace_root=tmp_path,
catalog_path=tmp_path / "missing.yaml",
legacy_registry_path=tmp_path / "missing.md",
include_notion=False,
now=clock,
).snapshot
assert snapshot.to_dict()["generated_at"] == "2025-02-01T00:15:00+00:00"


def test_snapshot_clock_requires_an_offset_before_loading_inputs(tmp_path, monkeypatch):
monkeypatch.setattr(
"github_repo_auditor.portfolio_truth_reconcile.load_portfolio_catalog",
lambda _: pytest.fail("loaded catalog before validating report clock"),
)
with pytest.raises(ValueError, match="timezone-aware"):
build_portfolio_truth_snapshot(
workspace_root=tmp_path, now=datetime(2025, 2, 1)
)


def test_publication_rejects_naive_clock_before_touching_sources(tmp_path, monkeypatch):
def unexpected_target_read(**kwargs):
pytest.fail("publication reached target validation before clock validation")

monkeypatch.setattr(publish, "validate_publish_targets", unexpected_target_read)
with pytest.raises(ValueError, match="timezone-aware"):
publish.publish_portfolio_truth(
workspace_root=tmp_path / "workspace",
output_dir=tmp_path / "output",
registry_output=tmp_path / "workspace" / "registry.md",
portfolio_report_output=tmp_path / "workspace" / "report.md",
now=datetime(2025, 2, 1),
)
Loading