π OpenHands: AI-Driven Development
PLTF-2899: store GitHub PR timestamps as naive UTC (asyncpg DataError) (#14657)
aivong-openhandsf2f77a666c92
diff --git a/enterprise/integrations/github/data_collector.py b/enterprise/integrations/github/data_collector.pyindex d6ba6f8182..e26459e4da 100644--- a/enterprise/integrations/github/data_collector.py+++ b/enterprise/integrations/github/data_collector.py@@ -2,7 +2,7 @@ import base64import jsonimport osimport re-from datetime import datetime+from datetime import datetime, timezonefrom enum import Enumfrom typing import Any@@ -28,6 +28,25 @@ from openhands.app_server.utils.logger import openhands_logger as loggerfile_store = get_global_config().file_store+def _github_ts_to_naive_utc(value: str | None) -> datetime | None:+ """Normalize a GitHub ISO-8601 timestamp to a naive UTC datetime.++ GitHub sends timestamps like ``"2025-06-19T21:19:36Z"``. Parsing the+ trailing ``Z`` produces a timezone-aware datetime, which asyncpg refuses to+ bind to the ``TIMESTAMP WITHOUT TIME ZONE`` columns on ``openhands_prs``+ (``DataError: can't subtract offset-naive and offset-aware datetimes``).+ Converting to UTC and dropping ``tzinfo`` keeps the stored value consistent+ with how naive UTC timestamps are already persisted on that table.+ """+ if not value:+ return None+ return (+ datetime.fromisoformat(value.replace('Z', '+00:00'))+ .astimezone(timezone.utc)+ .replace(tzinfo=None)+ )++COLLECT_GITHUB_INTERACTIONS = (os.getenv('COLLECT_GITHUB_INTERACTIONS', 'false') == 'true')@@ -636,12 +655,13 @@ class GitHubDataCollector:num_deletions = pr_data.get('deletions', 0)merged = pr_data.get('merged', False)- # Extract closed_at timestamp- # Example: "closed_at":"2025-06-19T21:19:36Z"- closed_at_str = pr_data.get('closed_at')- created_at = pr_data.get('created_at')-- closed_at = datetime.fromisoformat(closed_at_str.replace('Z', '+00:00'))+ # Extract timestamps. Example: "closed_at":"2025-06-19T21:19:36Z".+ # Both are normalized to naive UTC so they can be bound to the naive+ # TIMESTAMP columns on openhands_prs (see _github_ts_to_naive_utc).+ # Previously created_at was passed as a raw string; it is now+ # consistently a naive-UTC datetime like closed_at.+ closed_at = _github_ts_to_naive_utc(pr_data.get('closed_at'))+ created_at = _github_ts_to_naive_utc(pr_data.get('created_at'))# Determine status based on whether it was mergedstatus = PRStatus.MERGED if merged else PRStatus.CLOSEDdiff --git a/enterprise/tests/unit/integrations/github/test_data_collector_timestamps.py b/enterprise/tests/unit/integrations/github/test_data_collector_timestamps.pynew file mode 100644index 0000000000..14a73a95e8--- /dev/null+++ b/enterprise/tests/unit/integrations/github/test_data_collector_timestamps.py@@ -0,0 +1,24 @@+from datetime import datetime++import pytest+from integrations.github.data_collector import _github_ts_to_naive_utc+++def test_z_suffix_parsed_as_naive_utc():+ result = _github_ts_to_naive_utc('2025-06-19T21:19:36Z')+ # Must be naive (tzinfo stripped) so it can bind to a+ # TIMESTAMP WITHOUT TIME ZONE column via asyncpg.+ assert result == datetime(2025, 6, 19, 21, 19, 36)+ assert result.tzinfo is None+++def test_non_utc_offset_converted_to_utc():+ # 21:19:36+02:00 == 19:19:36 UTC+ result = _github_ts_to_naive_utc('2025-06-19T21:19:36+02:00')+ assert result == datetime(2025, 6, 19, 19, 19, 36)+ assert result.tzinfo is None++[email protected]('value', [None, ''])+def test_missing_values_return_none(value):+ assert _github_ts_to_naive_utc(value) is None