/
github.ts
65 lines (52 loc) · 1.58 KB
/
github.ts
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
/* eslint-disable import/no-extraneous-dependencies */
import { createAppAuth } from '@octokit/auth-app';
import { Octokit } from '@octokit/core';
import * as AWS from 'aws-sdk';
const sm = new AWS.SecretsManager();
export function baseUrlFromDomain(domain: string): string {
if (domain == 'github.com') {
return 'https://api.github.com';
}
return `https://${domain}/api/v3`;
}
export async function getOctokit(installationId?: string) {
if (!process.env.GITHUB_SECRET_ARN || !process.env.GITHUB_PRIVATE_KEY_SECRET_ARN) {
throw new Error('Missing environment variables');
}
const secret = await sm.getSecretValue({
SecretId: process.env.GITHUB_SECRET_ARN,
}).promise();
if (!secret.SecretString) {
throw new Error(`No secret string in ${process.env.GITHUB_SECRET_ARN}`);
}
const githubSecrets = JSON.parse(secret.SecretString);
let baseUrl = baseUrlFromDomain(githubSecrets.domain);
let token;
if (githubSecrets.personalAuthToken) {
token = githubSecrets.personalAuthToken;
} else {
const privateKey = (await sm.getSecretValue({
SecretId: process.env.GITHUB_PRIVATE_KEY_SECRET_ARN,
}).promise()).SecretString;
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,
});
return {
githubSecrets,
octokit,
};
}