-
Notifications
You must be signed in to change notification settings - Fork 722
Expand file tree
/
Copy pathoctokit.ts
More file actions
65 lines (59 loc) · 2.03 KB
/
octokit.ts
File metadata and controls
65 lines (59 loc) · 2.03 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
import { Octokit } from '@octokit/rest';
import { ActionRequestMessage } from '../scale-runners/scale-up';
import {
createGithubAppAuth,
createGithubInstallationAuth,
createOctokitClient,
getAppCount,
getStoredInstallationId,
} from './auth';
export async function getInstallationId(
ghesApiUrl: string,
enableOrgLevel: boolean,
payload: ActionRequestMessage,
appIndex?: number,
): Promise<number> {
// Use pre-stored installation ID when available (avoids an API call)
if (appIndex !== undefined) {
const storedId = await getStoredInstallationId(appIndex);
if (storedId !== undefined) return storedId;
}
const multiApp = (await getAppCount()) > 1;
if (!multiApp && payload.installationId !== 0) {
return payload.installationId;
}
const ghAuth = await createGithubAppAuth(undefined, ghesApiUrl, appIndex);
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> {
// Select one app for this entire auth flow
const ghAuth = await createGithubAppAuth(undefined, ghesApiUrl);
const appIdx = ghAuth.appIndex;
const installationId = await getInstallationId(ghesApiUrl, enableOrgLevel, payload, appIdx);
const installationAuth = await createGithubInstallationAuth(installationId, ghesApiUrl, appIdx);
return await createOctokitClient(installationAuth.token, ghesApiUrl);
}