Skip to content

Commit 56e86ee

Browse files
committed
Address CodeRabbit review: keyword-only param, UTC alias, guard test
- Make include_raw_events keyword-only in _collect_snapshot_data to silence Ruff FBT001/FBT002 (boolean positional arg warnings) - Replace timezone.utc with datetime.UTC alias in new tests (UP017) - Add test_raw_events_graceful_when_storage_missing_method: verifies that events.json is omitted silently when storage does not implement list_contributions, documenting the getattr/callable guard behaviour
1 parent ed94b7b commit 56e86ee

2 files changed

Lines changed: 44 additions & 18 deletions

File tree

src/ghdcbot/engine/snapshots.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ def _collect_snapshot_data(
165165
contribution_summaries: list[ContributionSummary] | None,
166166
run_id: str,
167167
generated_at: datetime,
168+
*,
168169
include_raw_events: bool = False,
169170
) -> dict[str, dict[str, Any]]:
170171
"""Collect all snapshot data into structured dictionaries."""

tests/test_snapshots.py

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Tests for GitHub snapshot writing."""
22

3-
from datetime import datetime, timezone
3+
from datetime import UTC, datetime, timezone
44
from unittest.mock import MagicMock
55

66
import pytest
@@ -367,7 +367,7 @@ def test_raw_events_excluded_when_false() -> None:
367367
github_user="alice",
368368
event_type="pr_merged",
369369
repo="org/repo",
370-
created_at=datetime(2024, 1, 15, tzinfo=timezone.utc),
370+
created_at=datetime(2024, 1, 15, tzinfo=UTC),
371371
payload={"pr_number": 1},
372372
)
373373
]
@@ -377,11 +377,11 @@ def test_raw_events_excluded_when_false() -> None:
377377
identity_mappings=[],
378378
scores=[],
379379
member_roles={},
380-
period_start=datetime(2024, 1, 1, tzinfo=timezone.utc),
381-
period_end=datetime(2024, 1, 31, tzinfo=timezone.utc),
380+
period_start=datetime(2024, 1, 1, tzinfo=UTC),
381+
period_end=datetime(2024, 1, 31, tzinfo=UTC),
382382
contribution_summaries=None,
383383
run_id="test-run",
384-
generated_at=datetime(2024, 1, 31, 12, 0, 0, tzinfo=timezone.utc),
384+
generated_at=datetime(2024, 1, 31, 12, 0, 0, tzinfo=UTC),
385385
include_raw_events=False,
386386
)
387387
assert "events.json" not in snapshots
@@ -395,14 +395,14 @@ def test_raw_events_included_when_enabled() -> None:
395395
github_user="alice",
396396
event_type="pr_merged",
397397
repo="org/repo",
398-
created_at=datetime(2024, 1, 15, tzinfo=timezone.utc),
398+
created_at=datetime(2024, 1, 15, tzinfo=UTC),
399399
payload={"pr_number": 42},
400400
),
401401
ContributionEvent(
402402
github_user="bob",
403403
event_type="issue_opened",
404404
repo="org/repo",
405-
created_at=datetime(2024, 1, 20, tzinfo=timezone.utc),
405+
created_at=datetime(2024, 1, 20, tzinfo=UTC),
406406
payload={"issue_number": 7},
407407
),
408408
]
@@ -412,11 +412,11 @@ def test_raw_events_included_when_enabled() -> None:
412412
identity_mappings=[],
413413
scores=[],
414414
member_roles={},
415-
period_start=datetime(2024, 1, 1, tzinfo=timezone.utc),
416-
period_end=datetime(2024, 1, 31, tzinfo=timezone.utc),
415+
period_start=datetime(2024, 1, 1, tzinfo=UTC),
416+
period_end=datetime(2024, 1, 31, tzinfo=UTC),
417417
contribution_summaries=None,
418418
run_id="test-run",
419-
generated_at=datetime(2024, 1, 31, 12, 0, 0, tzinfo=timezone.utc),
419+
generated_at=datetime(2024, 1, 31, 12, 0, 0, tzinfo=UTC),
420420
include_raw_events=True,
421421
)
422422
assert "events.json" in snapshots
@@ -433,20 +433,20 @@ def test_raw_events_included_when_enabled() -> None:
433433
def test_raw_events_respects_period_start() -> None:
434434
"""Only events at or after period_start are included in events.json."""
435435
storage = MockStorage()
436-
period_start = datetime(2024, 1, 10, tzinfo=timezone.utc)
436+
period_start = datetime(2024, 1, 10, tzinfo=UTC)
437437
storage.contributions = [
438438
ContributionEvent(
439439
github_user="alice",
440440
event_type="pr_merged",
441441
repo="org/repo",
442-
created_at=datetime(2024, 1, 5, tzinfo=timezone.utc), # before period_start
442+
created_at=datetime(2024, 1, 5, tzinfo=UTC), # before period_start
443443
payload={},
444444
),
445445
ContributionEvent(
446446
github_user="bob",
447447
event_type="pr_merged",
448448
repo="org/repo",
449-
created_at=datetime(2024, 1, 15, tzinfo=timezone.utc), # within period
449+
created_at=datetime(2024, 1, 15, tzinfo=UTC), # within period
450450
payload={},
451451
),
452452
]
@@ -457,10 +457,10 @@ def test_raw_events_respects_period_start() -> None:
457457
scores=[],
458458
member_roles={},
459459
period_start=period_start,
460-
period_end=datetime(2024, 1, 31, tzinfo=timezone.utc),
460+
period_end=datetime(2024, 1, 31, tzinfo=UTC),
461461
contribution_summaries=None,
462462
run_id="test-run",
463-
generated_at=datetime(2024, 1, 31, 12, 0, 0, tzinfo=timezone.utc),
463+
generated_at=datetime(2024, 1, 31, 12, 0, 0, tzinfo=UTC),
464464
include_raw_events=True,
465465
)
466466
events_data = snapshots["events.json"]["data"]
@@ -476,7 +476,7 @@ def test_write_snapshots_raw_events_via_config() -> None:
476476
github_user="alice",
477477
event_type="pr_merged",
478478
repo="org/repo",
479-
created_at=datetime(2024, 1, 15, tzinfo=timezone.utc),
479+
created_at=datetime(2024, 1, 15, tzinfo=UTC),
480480
payload={"pr_number": 1},
481481
)
482482
]
@@ -490,9 +490,34 @@ def test_write_snapshots_raw_events_via_config() -> None:
490490
identity_mappings=[],
491491
scores=[],
492492
member_roles={},
493-
period_start=datetime(2024, 1, 1, tzinfo=timezone.utc),
494-
period_end=datetime(2024, 1, 31, tzinfo=timezone.utc),
493+
period_start=datetime(2024, 1, 1, tzinfo=UTC),
494+
period_end=datetime(2024, 1, 31, tzinfo=UTC),
495495
)
496496

497497
written_paths = [path for _, _, path, _ in github_writer.files_written]
498498
assert any("events.json" in p for p in written_paths)
499+
500+
501+
def test_raw_events_graceful_when_storage_missing_method() -> None:
502+
"""events.json is omitted gracefully if storage lacks list_contributions."""
503+
class MinimalStorage:
504+
def list_recent_notifications(self, limit: int = 1000) -> list[dict]:
505+
return []
506+
def list_pending_issue_requests(self) -> list[dict]:
507+
return []
508+
509+
storage = MinimalStorage()
510+
snapshots = _collect_snapshot_data(
511+
storage=storage,
512+
config=_make_config(),
513+
identity_mappings=[],
514+
scores=[],
515+
member_roles={},
516+
period_start=datetime(2024, 1, 1, tzinfo=UTC),
517+
period_end=datetime(2024, 1, 31, tzinfo=UTC),
518+
contribution_summaries=None,
519+
run_id="test-run",
520+
generated_at=datetime(2024, 1, 31, 12, 0, 0, tzinfo=UTC),
521+
include_raw_events=True,
522+
)
523+
assert "events.json" not in snapshots

0 commit comments

Comments
 (0)