-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathmain.ts
More file actions
62 lines (55 loc) · 1.7 KB
/
main.ts
File metadata and controls
62 lines (55 loc) · 1.7 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
import {createAppAuth} from '@octokit/auth-app';
import {Octokit} from '@octokit/rest';
import {Endpoints} from '@octokit/types';
import * as core from '@actions/core';
import ProxyAgent from 'proxy-agent';
type listInstallationsResponse =
Endpoints['GET /app/installations']['response'];
async function run(): Promise<void> {
try {
const privateKey: string = core.getInput('private_key');
const appId: string = core.getInput('app_id');
const scope: string = core.getInput('scope');
const appOctokit = new Octokit({
authStrategy: createAppAuth,
request: {
agent: new ProxyAgent(),
},
auth: {
appId,
privateKey,
},
baseUrl: process.env.GITHUB_API_URL || 'https://api.github.com',
});
const installations: listInstallationsResponse =
await appOctokit.apps.listInstallations();
let installationId = installations.data[0].id;
if (scope !== '') {
const scopedData = installations.data.find(
(item) => item.account?.login === scope
);
if (scopedData === undefined) {
throw new Error(`set scope is ${scope}, but installation is not found`);
}
installationId = scopedData.id;
}
// This is untyped
// See: https://github.com/octokit/core.js/blob/master/src/index.ts#L182-L183
const resp = await appOctokit.auth({
type: 'installation',
installationId,
});
if (!resp) {
throw new Error('Unable to authenticate');
}
// @ts-expect-error
core.setSecret(resp.token);
// @ts-expect-error
core.setOutput('token', resp.token);
} catch (error) {
if (error instanceof Error) {
core.setFailed(error.message);
}
}
}
run();