-
Notifications
You must be signed in to change notification settings - Fork 655
/
Copy pathoctokit.ts
46 lines (43 loc) · 1.5 KB
/
octokit.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
import { Octokit } from '@octokit/rest';
import { ActionRequestMessage } from '../scale-runners/scale-up';
import { createGithubAppAuth, createGithubInstallationAuth, createOctokitClient } from './auth';
export async function getInstallationId(
ghesApiUrl: string,
enableOrgLevel: boolean,
payload: ActionRequestMessage,
): Promise<number> {
if (payload.installationId !== 0) {
return payload.installationId;
}
const ghAuth = await createGithubAppAuth(undefined, ghesApiUrl);
const githubClient = await createOctokitClient(ghAuth.token, ghesApiUrl);
return enableOrgLevel
? (
await githubClient.apps.getOrgInstallation({
org: payload.repositoryOwner,
})
).data.id
: (
await githubClient.apps.getRepoInstallation({
owner: payload.repositoryOwner,
repo: payload.repositoryName,
})
).data.id;
}
/**
*
* Util method to get an octokit client based on provided installation id. This method should
* phase out the usages of methods in gh-auth.ts outside of this module. Main purpose to make
* mocking of the octokit client easier.
*
* @returns ockokit client
*/
export async function getOctokit(
ghesApiUrl: string,
enableOrgLevel: boolean,
payload: ActionRequestMessage,
): Promise<Octokit> {
const installationId = await getInstallationId(ghesApiUrl, enableOrgLevel, payload);
const ghAuth = await createGithubInstallationAuth(installationId, ghesApiUrl);
return await createOctokitClient(ghAuth.token, ghesApiUrl);
}