forked from peter-murray/workflow-application-token-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (83 loc) · 3.35 KB
/
index.js
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
const core = require('@actions/core')
, githubApplication = require('./lib/github-application')
;
async function run() {
let app;
try {
const privateKey = getRequiredInputValue('application_private_key')
, applicationId = getRequiredInputValue('application_id')
, githubApiBaseUrl = core.getInput('github_api_base_url')
, httpsProxy = core.getInput('https_proxy')
;
app = await githubApplication.create(privateKey, applicationId, githubApiBaseUrl, null, httpsProxy);
} catch(err) {
fail(err, 'Failed to initialize GitHub Application connection using provided id and private key');
}
if (app) {
core.info(`Found GitHub Application: ${app.name}`);
try {
const userSpecifiedOrganization = core.getInput('organization')
, repository = process.env['GITHUB_REPOSITORY']
, repoParts = repository.split('/')
;
let installationId;
if (userSpecifiedOrganization) {
core.info(`Obtaining application installation for organization: ${userSpecifiedOrganization}`);
// use the organization specified to get the installation
const installation = await app.getOrganizationInstallation(userSpecifiedOrganization);
if (installation && installation.id) {
installationId = installation.id;
} else {
fail(null, `GitHub Application is not installed on the specified organization: ${userSpecifiedOrganization}`);
}
} else {
core.info(`Obtaining application installation for repository: ${repository}`);
// fallback to getting a repository installation
const installation = await app.getRepositoryInstallation(repoParts[0], repoParts[1]);
if (installation && installation.id) {
installationId = installation.id;
} else {
fail(null, `GitHub Application is not installed on repository: ${repository}`);
}
}
if (installationId) {
const permissions = {};
// Build up the list of requested permissions
let permissionInput = core.getInput("permissions");
if (permissionInput) {
for (let p of permissionInput.split(",")){
let [pName, pLevel] = p.split(":", 2);
permissions[pName.trim()] = pLevel.trim();
}
core.info(`Requesting limitation on GitHub Application permissions to only: ${JSON.stringify(permissions)}`);
}
const accessToken = await app.getInstallationAccessToken(installationId, permissions);
// Register the secret to mask it in the output
core.setSecret(accessToken.token);
core.setOutput('token', accessToken.token);
core.info(JSON.stringify(accessToken));
core.info(`Successfully generated an access token for application.`)
if (core.getBooleanInput('revoke_token')) {
// Store the token for post state invalidation of it once the job is complete
core.saveState('token', accessToken.token);
}
} else {
fail('No installation of the specified GitHub application was able to be retrieved.');
}
} catch (err) {
fail(err);
}
}
}
run();
function fail(err, message) {
core.error(err);
if (message) {
core.setFailed(message);
} else {
core.setFailed(err.message);
}
}
function getRequiredInputValue(key) {
return core.getInput(key, {required: true});
}