Skip to content

Commit 149f89a

Browse files
committed
Add unit test for scale down
1 parent 9dcb671 commit 149f89a

File tree

1 file changed

+32
-50
lines changed
  • modules/runners/lambdas/scale-runners/src/scale-runners

1 file changed

+32
-50
lines changed

modules/runners/lambdas/scale-runners/src/scale-runners/scale-down.ts

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,37 @@ import { Octokit } from '@octokit/rest';
33
import { AppAuth } from '@octokit/auth-app/dist-types/types';
44
import { listRunners, terminateRunner, RunnerInfo } from './runners';
55
import { createGithubAppAuth, createInstallationClient } from './scale-up';
6+
import yn from 'yn';
67

7-
// function createGithubAppAuth(installationId: number | undefined): AppAuth {
8-
// const privateKey = Buffer.from(process.env.GITHUB_APP_KEY_BASE64 as string, 'base64').toString();
9-
// const appId: number = parseInt(process.env.GITHUB_APP_ID as string);
10-
// const clientId = process.env.GITHUB_APP_CLIENT_ID as string;
11-
// const clientSecret = process.env.GITHUB_APP_CLIENT_SECRET as string;
12-
13-
// return createAppAuth({
14-
// id: appId,
15-
// privateKey: privateKey,
16-
// installationId: installationId,
17-
// clientId: clientId,
18-
// clientSecret: clientSecret,
19-
// });
20-
// }
21-
22-
// async function createInstallationClient(githubAppAuth: AppAuth): Promise<Octokit> {
23-
// const auth = await githubAppAuth({ type: 'installation' });
24-
// return new Octokit({ auth: auth.token });
25-
// }
26-
27-
// specific to scale down
288
async function createAppClient(githubAppAuth: AppAuth): Promise<Octokit> {
299
const auth = await githubAppAuth({ type: 'app' });
3010
return new Octokit({ auth: auth.token });
3111
}
3212

3313
interface Repo {
34-
isOrg: boolean;
3514
repoName: string;
3615
repoOwner: string;
3716
}
3817

39-
function getRepo(runner: RunnerInfo): Repo {
40-
if (runner.repo) {
18+
function getRepo(runner: RunnerInfo, orgLevel: boolean): Repo {
19+
if (orgLevel) {
4120
return {
42-
repoOwner: runner.repo?.split('/')[0] as string,
43-
repoName: runner.repo?.split('/')[1] as string,
44-
isOrg: false,
21+
repoOwner: runner.org as string,
22+
repoName: '',
4523
};
4624
} else {
4725
return {
48-
repoOwner: runner.org as string,
49-
repoName: '',
50-
isOrg: true,
26+
repoOwner: runner.repo?.split('/')[0] as string,
27+
repoName: runner.repo?.split('/')[1] as string,
5128
};
5229
}
5330
}
5431

55-
async function createGitHubClientForRunner(runner: RunnerInfo): Promise<Octokit> {
32+
async function createGitHubClientForRunner(runner: RunnerInfo, orgLevel: boolean): Promise<Octokit> {
5633
const githubClient = await createAppClient(createGithubAppAuth(undefined));
57-
const repo = getRepo(runner);
34+
const repo = getRepo(runner, orgLevel);
5835

59-
const repoInstallationId = repo.isOrg
36+
const installationId = orgLevel
6037
? (
6138
await githubClient.apps.getOrgInstallation({
6239
org: repo.repoOwner,
@@ -69,10 +46,11 @@ async function createGitHubClientForRunner(runner: RunnerInfo): Promise<Octokit>
6946
})
7047
).data.id;
7148

72-
return createInstallationClient(createGithubAppAuth(repoInstallationId));
49+
return createInstallationClient(createGithubAppAuth(installationId));
7350
}
7451

7552
export async function scaleDown(): Promise<void> {
53+
const enableOrgLevel = yn(process.env.ENABLE_ORGANIZATION_RUNNERS, { default: true });
7654
const environment = process.env.ENVIRONMENT as string;
7755
const runners = await listRunners({
7856
environment: environment,
@@ -82,42 +60,46 @@ export async function scaleDown(): Promise<void> {
8260
console.debug(`No active runners found for environment: '${environment}'`);
8361
return;
8462
}
63+
console.log(runners);
8564

86-
runners.forEach(async (r) => {
87-
const githubAppClient = await createGitHubClientForRunner(r);
65+
for (const r of runners) {
66+
const githubAppClient = await createGitHubClientForRunner(r, enableOrgLevel);
8867

89-
const repo = getRepo(r);
90-
const registered = await githubAppClient.actions.listSelfHostedRunnersForRepo({
91-
owner: repo.repoOwner,
92-
repo: repo.repoName,
93-
});
68+
const repo = getRepo(r, enableOrgLevel);
69+
console.log(repo);
70+
const registered = enableOrgLevel
71+
? await githubAppClient.actions.listSelfHostedRunnersForOrg({
72+
org: repo.repoOwner,
73+
})
74+
: await githubAppClient.actions.listSelfHostedRunnersForRepo({
75+
owner: repo.repoOwner,
76+
repo: repo.repoName,
77+
});
78+
console.log(registered);
9479

9580
console.log(registered.data.runners);
96-
registered.data.runners.forEach(async (a: any) => {
81+
for (const a of registered.data.runners) {
9782
const runnerName = a.name as string;
9883
if (runnerName === r.instanceId) {
99-
console.log(r.instanceId);
10084
try {
101-
const result = repo.isOrg
85+
const result = enableOrgLevel
10286
? await githubAppClient.actions.deleteSelfHostedRunnerFromOrg({ runner_id: a.id, org: repo.repoOwner })
10387
: await githubAppClient.actions.deleteSelfHostedRunnerFromRepo({
10488
runner_id: a.id,
10589
owner: repo.repoOwner,
10690
repo: repo.repoName,
10791
});
92+
10893
if (result?.status == 204) {
109-
terminateRunner(r);
94+
await terminateRunner(r);
11095
console.info(
11196
`AWS runner instance '${r.instanceId}' is terminated and GitHub runner '${runnerName}' is de-registered.`,
11297
);
11398
}
114-
console.info(
115-
`AWS runner instance '${r.instanceId}' is terminated and GitHub runner '${runnerName}' is de-registered.`,
116-
);
11799
} catch (e) {
118100
console.debug(`Runner '${runnerName}' cannot be de-registered, most likely the runner is active.`);
119101
}
120102
}
121-
});
122-
});
103+
}
104+
}
123105
}

0 commit comments

Comments
 (0)