test-infra icon indicating copy to clipboard operation
test-infra copied to clipboard

Fix pagination bug and type safety issues in GitHub runners API

Open Copilot opened this issue 6 months ago • 3 comments

This PR addresses the critical issues identified in PR #7027 reviews, particularly @ZainRizvi's report of incorrect runner statistics and type safety concerns.

Issues Fixed

Pagination Bug (Zain's Review)

The API was only fetching the first 100 runners from GitHub's API but returning the total count from the API response, leading to inconsistent statistics like "3189 total runners, 96 online, 46 busy" where the math clearly doesn't add up.

Root Cause: The original implementation used a single API call with per_page: 100, but GitHub's runners API paginates results. For organizations with 3000+ runners, this meant:

  • total_count: 3189 (from GitHub API)
  • runners.length: 100 (only first page fetched)
  • onlineRunners: 96 (calculated from incomplete data)
  • busyRunners: 46 (calculated from incomplete data)

Solution: Implemented proper pagination with fetchAllRunners() function that:

  • Fetches all pages until no more runners remain
  • Uses actual fetched runner count for total_count
  • Ensures statistics are calculated from complete dataset

Type Safety Issues (Copilot Review)

The code used any types for GitHub API responses, defeating TypeScript's type safety benefits.

Solution:

  • Added proper TypeScript interfaces (GitHubRunnerLabel)
  • Implemented type-safe mapping with validation
  • Added fallback handling for unknown status/type values
  • Maintained backwards compatibility

Technical Changes

  • torchci/pages/api/runners/[org].ts: Added pagination logic and proper TypeScript interfaces
  • torchci/test/runners-api.test.ts: Added comprehensive test coverage

Behavior Changes

Before: API returned mathematically inconsistent numbers

{
  "total_count": 3189,
  "runners": [/* only 100 runners */]
}

After: API returns accurate, consistent numbers

{
  "total_count": 3189,
  "runners": [/* all 3189 runners */] 
}

The fix ensures that for any organization, the runner statistics will be mathematically consistent and calculated from the complete dataset.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot avatar Aug 19 '25 23:08 Copilot