Skip to content

Commit 1a9b8f6

Browse files
committed
Fix KeyError when queued_at field is missing from GitHub API
The GitHub API may not always return queued_at for older jobs. Changed to use .get() method and made queued_at optional in type definitions. The metrics calculation now falls back to created_at when queued_at is not available, maintaining backward compatibility.
1 parent a2bd343 commit 1a9b8f6

6 files changed

Lines changed: 20 additions & 7 deletions

File tree

.DS_Store

8 KB
Binary file not shown.

github-runner-manager/.DS_Store

6 KB
Binary file not shown.

github-runner-manager/src/github_runner_manager/github_client.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,16 @@ def _to_job_info(job: dict) -> JobInfo:
468468
# datetime strings should be in ISO 8601 format, but they can also use Z instead of +00:00,
469469
# which is not supported by datetime.fromisoformat
470470
created_at = datetime.fromisoformat(job["created_at"].replace("Z", "+00:00"))
471-
queued_at = datetime.fromisoformat(job["queued_at"].replace("Z", "+00:00"))
472-
started_at = datetime.fromisoformat(job["started_at"].replace("Z", "+00:00"))
471+
queued_at_raw = job.get("queued_at")
472+
queued_at = (
473+
datetime.fromisoformat(queued_at_raw.replace("Z", "+00:00")) if queued_at_raw else None
474+
)
475+
started_at_raw = job.get("started_at")
476+
started_at = (
477+
datetime.fromisoformat(started_at_raw.replace("Z", "+00:00"))
478+
if started_at_raw
479+
else None
480+
)
473481
# conclusion could be null or an empty dictionary per api schema, so we need to handle
474482
# that though we would assume that it should always be present, as the job should be
475483
# finished.

github-runner-manager/src/github_runner_manager/metrics/github.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ def job(
5050
except (JobNotFoundError, PlatformApiError) as exc:
5151
raise GithubMetricsError from exc
5252

53-
queue_duration = (job_info.started_at - job_info.queued_at).total_seconds()
53+
queued_or_created_at = job_info.queued_at or job_info.created_at
54+
queue_duration = (
55+
(job_info.started_at - queued_or_created_at).total_seconds()
56+
if job_info.started_at
57+
else None
58+
)
5459

5560
return GithubJobMetrics(queue_duration=queue_duration, conclusion=job_info.conclusion)

github-runner-manager/src/github_runner_manager/platform/platform_provider.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class JobInfo:
250250
"""
251251

252252
created_at: datetime
253-
queued_at: datetime
254-
started_at: datetime
253+
queued_at: datetime | None
254+
started_at: datetime | None
255255
# A str until we realise a common pattern, use a simple str
256256
conclusion: str | None

github-runner-manager/src/github_runner_manager/types_/github.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ class JobInfo(BaseModel):
127127

128128
job_id: int
129129
created_at: datetime
130-
queued_at: datetime
131-
started_at: datetime
130+
queued_at: Optional[datetime]
131+
started_at: Optional[datetime]
132132
conclusion: Optional[JobConclusion]
133133
status: JobStatus
134134

0 commit comments

Comments
 (0)