Skip to content
Merged
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
5 changes: 5 additions & 0 deletions config/pools.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ pools:
runnerGroup: synology-public
repositoryAccess: selected
allowedRepositories:
- omt-global/apw-cli
- omt-global/axiom
- omt-global/bootstrap
- omt-global/gh-attest
- omt-global/github-runner-fleet
- omt-global/home-tv-channel-list
- omt-global/Screensaver
labels:
- synology
- shell-only
Expand Down
24 changes: 16 additions & 8 deletions src/lib/drift.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { PoolConfig } from "./config.js";
import {
fetchOrganizationRunnerGroupRunners,
fetchOrganizationRunnerGroups,
fetchOrganizationRunners,
type FetchLike
Expand Down Expand Up @@ -240,10 +241,12 @@ export async function collectGitHubActualPoolState(
const poolsByOrganization = groupByOrganization(desiredPools);

for (const [organization, pools] of poolsByOrganization.entries()) {
const [groups, runners] = await Promise.all([
fetchOrganizationRunnerGroups(apiUrl, organization, token, fetchImpl),
fetchOrganizationRunners(apiUrl, organization, token, fetchImpl)
]);
const groups = await fetchOrganizationRunnerGroups(
apiUrl,
organization,
token,
fetchImpl
);

for (const pool of pools) {
const group = groups.find((entry) => entry.name === pool.runnerGroup);
Expand All @@ -254,12 +257,17 @@ export async function collectGitHubActualPoolState(
);
}

const runners = await fetchOrganizationRunnerGroupRunners(
apiUrl,
organization,
group.id,
token,
fetchImpl
);

actualPools.push({
name: pool.name,
actual: runners.filter(
(runner) =>
runner.runnerGroupId === group.id && runner.status === "online"
).length
actual: runners.filter((runner) => runner.status === "online").length
});
}
}
Expand Down
71 changes: 71 additions & 0 deletions src/lib/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,77 @@ export async function fetchOrganizationRunners(
}
}

export async function fetchOrganizationRunnerGroupRunners(
apiUrl: string,
organization: string,
runnerGroupId: number,
token: string,
fetchImpl: FetchLike = fetch as FetchLike
): Promise<GitHubRunner[]> {
const runners: GitHubRunner[] = [];

for (let page = 1; ; page += 1) {
const response = await fetchImpl(
`${trimApiUrl(apiUrl)}/orgs/${organization}/actions/runner-groups/${runnerGroupId}/runners?per_page=100&page=${page}`,
{
method: "GET",
headers: buildGitHubApiHeaders(token)
}
);

const body = await response.text();
if (!response.ok) {
throw new Error(
`GitHub runner group runner lookup failed for ${organization}/${runnerGroupId} with ${response.status}: ${body}`
);
}

const payload = JSON.parse(body) as {
runners?: Array<{
id?: number;
name?: string;
status?: string;
busy?: boolean;
runner_group_id?: number;
labels?: Array<{ name?: string }>;
}>;
};

if (!Array.isArray(payload.runners)) {
throw new Error(
`GitHub runner group runner response for ${organization}/${runnerGroupId} did not include runners`
);
}

runners.push(
...payload.runners.map((runner) => {
if (typeof runner.id !== "number" || !runner.name || !runner.status) {
throw new Error(
`GitHub runner group runner response for ${organization}/${runnerGroupId} included an invalid runner entry`
);
}

return {
id: runner.id,
name: runner.name,
status: runner.status,
busy: runner.busy,
runnerGroupId: runner.runner_group_id ?? runnerGroupId,
labels: Array.isArray(runner.labels)
? runner.labels
.map((label) => label.name)
.filter((name): name is string => typeof name === "string")
: []
};
})
);

if (payload.runners.length < 100) {
return runners;
}
}
}

export async function deleteOrganizationRunner(
apiUrl: string,
organization: string,
Expand Down
12 changes: 2 additions & 10 deletions test/drift-detect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,12 @@ describe("drift detection", () => {
{
id: 1,
name: "runner-1",
status: "online",
runner_group_id: 10
status: "online"
},
{
id: 2,
name: "runner-2",
status: "offline",
runner_group_id: 10
},
{
id: 3,
name: "runner-3",
status: "online",
runner_group_id: 11
status: "offline"
}
]
})
Expand Down
Loading