forked from CloudSnorkel/cdk-github-runners
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda-github.ts
More file actions
134 lines (114 loc) · 3.6 KB
/
Copy pathlambda-github.ts
File metadata and controls
134 lines (114 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { createAppAuth } from '@octokit/auth-app';
import { Octokit } from '@octokit/rest';
import { getSecretValue, getSecretJsonValue } from './lambda-helpers';
export function baseUrlFromDomain(domain: string): string {
if (domain == 'github.com') {
return 'https://api.github.com';
}
return `https://${domain}/api/v3`;
}
type RunnerLevel = 'repo' | 'org' | undefined; // undefined is for backwards compatibility and should be treated as 'repo'
export interface GitHubSecrets {
domain: string;
appId: number;
personalAuthToken: string;
runnerLevel: RunnerLevel;
}
const octokitCache: {
installationId?: number;
secrets?: GitHubSecrets;
octokit?: Octokit;
} = {};
export async function getOctokit(installationId?: number): Promise<{ octokit: Octokit; githubSecrets: GitHubSecrets }> {
if (!process.env.GITHUB_SECRET_ARN || !process.env.GITHUB_PRIVATE_KEY_SECRET_ARN) {
throw new Error('Missing environment variables');
}
const githubSecrets: GitHubSecrets = await getSecretJsonValue(process.env.GITHUB_SECRET_ARN);
if (octokitCache.octokit && octokitCache.installationId == installationId && octokitCache.secrets &&
octokitCache.secrets.domain == githubSecrets.domain && octokitCache.secrets.appId == githubSecrets.appId &&
octokitCache.secrets.personalAuthToken == githubSecrets.personalAuthToken) {
// test and use cache
try {
await octokitCache.octokit.rest.meta.getOctocat();
console.log('Using cached octokit');
return {
octokit: octokitCache.octokit,
githubSecrets: octokitCache.secrets,
};
} catch (e) {
console.log('Octokit cache is invalid', e);
octokitCache.octokit = undefined;
}
}
let baseUrl = baseUrlFromDomain(githubSecrets.domain);
let token;
if (githubSecrets.personalAuthToken) {
token = githubSecrets.personalAuthToken;
} else {
const privateKey = await getSecretValue(process.env.GITHUB_PRIVATE_KEY_SECRET_ARN);
const appOctokit = new Octokit({
baseUrl,
authStrategy: createAppAuth,
auth: {
appId: githubSecrets.appId,
privateKey: privateKey,
},
});
token = (await appOctokit.auth({
type: 'installation',
installationId: installationId,
}) as any).token;
}
const octokit = new Octokit({
baseUrl,
auth: token,
});
octokitCache.octokit = octokit;
octokitCache.installationId = installationId;
octokitCache.secrets = githubSecrets;
return {
octokit,
githubSecrets,
};
}
export async function getRunner(octokit: Octokit, runnerLevel: RunnerLevel, owner: string, repo: string, name: string) {
let page = 1;
while (true) {
let runners;
if ((runnerLevel ?? 'repo') === 'repo') {
runners = await octokit.rest.actions.listSelfHostedRunnersForRepo({
page: page,
owner: owner,
repo: repo,
});
} else {
runners = await octokit.rest.actions.listSelfHostedRunnersForOrg({
page: page,
org: owner,
});
}
if (runners.data.runners.length == 0) {
return;
}
for (const runner of runners.data.runners) {
if (runner.name == name) {
return runner;
}
}
page++;
}
}
export async function deleteRunner(octokit: Octokit, runnerLevel: RunnerLevel, owner: string, repo: string, runnerId: number) {
if ((runnerLevel ?? 'repo') === 'repo') {
await octokit.rest.actions.deleteSelfHostedRunnerFromRepo({
owner: owner,
repo: repo,
runner_id: runnerId,
});
} else {
await octokit.rest.actions.deleteSelfHostedRunnerFromOrg({
org: owner,
runner_id: runnerId,
});
}
}