-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
123 lines (111 loc) · 3.84 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const fs = require('fs');
const { request } = require('@octokit/request');
const app = require('./lib/app');
const log = require('./lib/logging');
const getPrivateKey = path => fs.readFileSync(path);
module.exports.templateTags = [
{
name: 'jwt',
displayName: 'JSON Web Token',
description: 'Generates a JSON Web Token, allowing you to authenticate with the GitHub API as your GitHub App',
args: [
{
displayName: 'App ID',
description: 'ID of GitHub App',
type: 'number'
},
{
displayName: 'Private key file',
description: 'Path to private key file in PEM format',
type: 'string'
},
{
displayName: 'GitHub API Root',
description: 'The base URL to which to send requests',
type: 'string'
}
],
async run({ context }, ...args) {
log('jwt:context %o', context);
log('jwt:args %o', args);
// Destructure appId, path from context
let {
github_app_id: appId,
github_app_private_key_path: path,
github_api_root: baseUrl = 'https://api.github.com'
} = context;
// Allow appId, path, base URL to be overridden via args
const [tagAppId = 0, tagPath = '', tagBaseUrl = ''] = args;
appId = tagAppId > 0 ? tagAppId : appId;
path = tagPath.length > 0 ? tagPath : path;
baseUrl = tagBaseUrl.length > 0 ? tagBaseUrl : baseUrl;
log('jwt:using GitHub App %d', appId);
log('jwt:using GitHub App private key "%s"', path);
log('jwt:using baseUrl "%s"', baseUrl);
// Initialize request defaults
request.defaults({
baseUrl
});
// Return JWT
return app.getJsonWebToken({
appId,
privateKey: getPrivateKey(path),
request
});
}
},
{
name: 'installation_access_token',
displayName: 'Installation Access Token',
description:
'Generates an Installation Access Token, allowing you to authenticate with the GitHub API as an installation of your GitHub App',
args: [
{
displayName: 'Installation ID',
description: 'ID of GitHub App Installation',
type: 'number'
},
{
displayName: 'App ID',
description: 'ID of GitHub App',
type: 'number'
},
{
displayName: 'Private key file',
description: 'Path to private key file in PEM format',
type: 'string'
},
{
displayName: 'GitHub API Root',
description: 'The base URL to which to send requests',
type: 'string'
}
],
async run({ context }, ...args) {
log('installation_access_token:context %o', context);
log('installation_access_token:args %o', args);
let {
github_app_installation_id: installationId,
github_app_id: appId,
github_app_private_key_path: path,
github_api_root: baseUrl = 'https://api.github.com'
} = context;
// Allow installationId, id, path to be overridden via args
const [tagInstallationId = 0, tagAppId = 0, tagPath = '', tagBaseUrl = ''] = args;
installationId = tagInstallationId > 0 ? tagInstallationId : installationId;
appId = tagAppId > 0 ? tagAppId : appId;
path = tagPath.length > 0 ? tagPath : path;
baseUrl = tagBaseUrl.length > 0 ? tagBaseUrl : baseUrl;
log('installation_access_token:using installation %d', installationId);
log('installation_access_token:using GitHub App %d', appId);
log('installation_access_token:using GitHub App private key "%s"', path);
log('installation_access_token:using baseUrl "%s"', baseUrl);
// Initialize request defaults
request.defaults({
baseUrl
});
// Return installation access token
return app.getInstallationAccessToken({ appId, privateKey: getPrivateKey(path), installationId, request });
}
}
];