Skip to content
Draft
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
71 changes: 52 additions & 19 deletions torchci/pages/api/runners/[org].ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { createAppAuth } from "@octokit/auth-app";
import { App, Octokit } from "octokit";
import type { NextApiRequest, NextApiResponse } from "next";

// GitHub API response types
interface GitHubRunnerLabel {
id?: number;
name: string;
type?: "read-only" | "custom";
}

// Our application response types
export interface RunnerData {
id: number;
name: string;
Expand All @@ -20,6 +28,47 @@ export interface RunnersApiResponse {
runners: RunnerData[];
}

// Fetch all runners with proper pagination
async function fetchAllRunners(octokit: Octokit, org: string): Promise<RunnerData[]> {
const allRunners: RunnerData[] = [];
let page = 1;
const perPage = 100; // GitHub API maximum per page

while (true) {
const response = await octokit.request("GET /orgs/{org}/actions/runners", {
org,
per_page: perPage,
page,
});

const runnersPage = response.data;

// Map GitHub API response to our format with proper type safety
const mappedRunners: RunnerData[] = runnersPage.runners.map((runner: any) => ({
id: runner.id,
name: runner.name,
os: runner.os,
status: (runner.status === "online" || runner.status === "offline") ? runner.status : "offline",
busy: runner.busy,
labels: runner.labels.map((label: any) => ({
id: label.id,
name: label.name,
type: (label.type === "read-only" || label.type === "custom") ? label.type : "custom",
})),
}));

allRunners.push(...mappedRunners);

// Check if we've fetched all runners
if (runnersPage.runners.length < perPage) {
break;
}

page++;
}

return allRunners;
}
// Get Octokit instance authenticated for organization-level access
async function getOctokitForOrg(org: string): Promise<Octokit> {
let privateKey = process.env.PRIVATE_KEY as string;
Expand Down Expand Up @@ -63,27 +112,11 @@ export default async function handler(
try {
const octokit = await getOctokitForOrg(org);

// Fetch runners from GitHub API
const response = await octokit.request("GET /orgs/{org}/actions/runners", {
org,
per_page: 100, // GitHub API default/max
});

const runners: RunnerData[] = response.data.runners.map((runner: any) => ({
id: runner.id,
name: runner.name,
os: runner.os,
status: runner.status,
busy: runner.busy,
labels: runner.labels.map((label: any) => ({
id: label.id,
name: label.name,
type: label.type,
})),
}));
// Fetch all runners with proper pagination
const runners = await fetchAllRunners(octokit, org);

return res.status(200).json({
total_count: response.data.total_count,
total_count: runners.length,
runners,
});
} catch (error: any) {
Expand Down
86 changes: 86 additions & 0 deletions torchci/test/runners-api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { NextApiRequest, NextApiResponse } from "next";
import handler, { RunnerData, RunnersApiResponse } from "../pages/api/runners/[org]";

// Mock the octokit modules
jest.mock("@octokit/auth-app", () => ({
createAppAuth: jest.fn(),
}));

jest.mock("octokit", () => ({
App: jest.fn().mockImplementation(() => ({
octokit: {
request: jest.fn().mockResolvedValue({
data: { id: 123 },
}),
},
})),
Octokit: jest.fn().mockImplementation(() => ({
request: jest.fn(),
})),
}));

describe("/api/runners/[org]", () => {
let req: Partial<NextApiRequest>;
let res: Partial<NextApiResponse>;

beforeEach(() => {
req = {
method: "GET",
query: { org: "test-org" },
};
res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
};

// Set up environment variables
process.env.APP_ID = "123";
process.env.PRIVATE_KEY = Buffer.from("fake-key").toString("base64");
});

afterEach(() => {
jest.clearAllMocks();
});

test("should return error for non-GET requests", async () => {
req.method = "POST";

await handler(req as NextApiRequest, res as NextApiResponse);

expect(res.status).toHaveBeenCalledWith(405);
expect(res.json).toHaveBeenCalledWith({ error: "Method not allowed" });
});

test("should return error for missing org parameter", async () => {
req.query = {};

await handler(req as NextApiRequest, res as NextApiResponse);

expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({ error: "Organization parameter is required" });
});

test("should validate response structure", () => {
// Test that our interfaces are correct
const mockRunner: RunnerData = {
id: 1,
name: "test-runner",
os: "linux",
status: "online",
busy: false,
labels: [
{ id: 1, name: "self-hosted", type: "read-only" },
{ id: 2, name: "custom", type: "custom" },
],
};

const mockResponse: RunnersApiResponse = {
total_count: 1,
runners: [mockRunner],
};

expect(mockResponse.runners).toHaveLength(1);
expect(mockResponse.runners[0].status).toMatch(/^(online|offline)$/);
expect(mockResponse.total_count).toBe(mockResponse.runners.length);
});
});
Loading