Skip to content

Commit 65c0b0e

Browse files
committed
Refactor runner state types to use Endpoints for improved type safety and clarity
1 parent 9f59abe commit 65c0b0e

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

lambdas/functions/control-plane/src/scale-runners/scale-down.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Octokit } from '@octokit/rest';
2+
import { Endpoints } from '@octokit/types';
23
import { createChildLogger } from '@aws-github-runner/aws-powertools-util';
34
import moment from 'moment';
45

@@ -9,19 +10,12 @@ import { GhRunners, githubCache } from './cache';
910
import { ScalingDownConfig, getEvictionStrategy, getIdleRunnerCount } from './scale-down-config';
1011
import { metricGitHubAppRateLimit } from '../github/rate-limit';
1112
import { getGitHubEnterpriseApiUrl } from './scale-up';
12-
import { GetResponseDataTypeFromEndpointMethod } from '@octokit/types/dist-types/GetResponseTypeFromEndpointMethod';
1313

1414
const logger = createChildLogger('scale-down');
1515

16-
type OrgRunnerList = GetResponseDataTypeFromEndpointMethod<
17-
typeof Octokit.prototype.actions.listSelfHostedRunnersForOrg
18-
>;
19-
20-
type RepoRunnerList = GetResponseDataTypeFromEndpointMethod<
21-
typeof Octokit.prototype.actions.listSelfHostedRunnersForRepo
22-
>;
23-
24-
type RunnerState = (OrgRunnerList | RepoRunnerList)['runners'][number];
16+
type OrgRunnerList = Endpoints["GET /orgs/{org}/actions/runners"]["response"]["data"]["runners"];
17+
type RepoRunnerList = Endpoints["GET /repos/{owner}/{repo}/actions/runners"]["response"]["data"]["runners"];
18+
type RunnerState = OrgRunnerList[number] | RepoRunnerList[number];
2519

2620
async function getOrCreateOctokit(runner: RunnerInfo): Promise<Octokit> {
2721
const key = runner.owner;
@@ -73,20 +67,23 @@ async function getGitHubSelfHostedRunnerState(
7367
owner: ec2runner.owner.split('/')[0],
7468
repo: ec2runner.owner.split('/')[1],
7569
});
70+
metricGitHubAppRateLimit(state.headers);
7671

7772
return {
7873
id: state.data.id,
7974
name: state.data.name,
8075
busy: state.data.busy,
8176
status: state.data.status,
82-
headers: state.headers,
77+
os: state.data.os,
78+
labels: state.data.labels,
79+
runner_group_id: state.data.runner_group_id,
80+
ephemeral: state.data.ephemeral,
8381
};
8482
}
8583

8684
async function getGitHubRunnerBusyState(client: Octokit, ec2runner: RunnerInfo, runnerId: number): Promise<boolean> {
8785
const state = await getGitHubSelfHostedRunnerState(client, ec2runner, runnerId);
8886
logger.info(`Runner '${ec2runner.instanceId}' - GitHub Runner ID '${runnerId}' - Busy: ${state.busy}`);
89-
metricGitHubAppRateLimit(state.headers);
9087
return state.busy;
9188
}
9289

@@ -226,7 +223,7 @@ async function markOrphan(instanceId: string): Promise<void> {
226223

227224
async function lastChanceCheckOrphanRunner(runner: RunnerList): Promise<void> {
228225
const client = await getOrCreateOctokit(runner as RunnerInfo);
229-
const runnerId = parseInt(runner.runnerId);
226+
const runnerId = parseInt(runner.runnerId || '0');
230227
const ec2Instance = runner as RunnerInfo;
231228
const state = await getGitHubSelfHostedRunnerState(client, ec2Instance, runnerId);
232229
logger.debug(`Runner is currently '${runner.instanceId}' state: ${JSON.stringify(state)}`);

0 commit comments

Comments
 (0)