-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
235 lines (235 loc) · 7.39 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
'use strict';
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const github_1 = __importDefault(require("@actions/github"));
const process_1 = __importDefault(require("process"));
/**
* Coerces values into JavaScript object types
* @function coerce
* @param {any} value
* @returns {any}
* @throws {!SyntaxError}
* @example
* coerce('1');
* //> 1
*
* coerce('stringy');
* //> "stringy"
*
* coerce('{"key": "value"}');
* //> {key: "value"}
*/
const coerce = (value) => {
try {
return JSON.parse(value);
}
catch (e) {
if (!(e instanceof SyntaxError)) {
throw e;
}
if (['undefined', undefined].includes(value)) {
return undefined;
}
else {
return value;
}
}
};
/**
* Get Action Input or Environment variable by name
* @param {string} name
* @param {boolean} coerce_types
* @return {string|any}
* @example
* const verbose = get_gha_input('verbose');
* if (verbose === 'true') {
* console.log(verbose);
* //> "true"
* }
*/
const get_gha_input = (name, coerce_types = false) => {
const value = process_1.default.env[`INPUT_${name.toUpperCase()}`];
if (coerce_types === true) {
return coerce(value);
}
return value;
};
/**
* Set Action Output or Environment variable by name to `JSON.stringify(value)`
* @param {string} name
* @param {any} value
* @example
* set_gha_output('result') = 'nifty'
* console.log(process.env.OUTPUT_RESULT);
* //> "nifty"
*/
const set_gha_output = (name, value) => {
process_1.default.env[`OUTPUT_${name.toUpperCase()}`] = JSON.stringify(value);
};
/**
* General callback for unhandled promise rejection errors and warnings
* @callback unhandled_promise_rejection__callback
* @param {Error} err
* @param {Promise} _promise
* @throws {UnhandledPromiseRejection | UnhandledPromiseRejectionWarning}
*/
const unhandled_promise_rejection__callback = (err, _promise) => {
setTimeout(() => {
if (_promise) {
console.error(`Unhandled rejection at: ${_promise}`);
}
console.error(err.message);
console.dir(err.stack);
throw err;
});
};
/**
* General callback to inspect and capture warnings
* @callback warning__callback
* @param {Error} warning
*/
const warning__callback = (warning) => {
console.warn(warning.name);
console.warn(warning.message);
console.warn(warning.stack);
};
process_1.default.on('unhandledRejection', unhandled_promise_rejection__callback);
process_1.default.on('warning', warning__callback);
const actor = process_1.default.env.GITHUB_ACTOR;
const repo = process_1.default.env.GITHUB_REPOSITORY.split('/')[1];
if (actor === undefined || repo === undefined) {
const error_message = [
'Environment variable `GITHUB_ACTOR` or `GITHUB_REPOSITORY` is undefined',
'Please ensure that you are testing with something like...',
' GITHUB_ACTOR=your-name node index.js',
'',
'... hint if errors about undefined Inputs then pop, try...',
' GITHUB_ACTOR=your-name\\',
' GITHUB_REPOSITORY=owner/fancy-project\\',
' INPUT_PULL_REQUEST_TOKEN=...\\',
' node index.js',
];
throw new ReferenceError(error_message.join('\n'));
}
const error_message__base = [
'Please check that your Workflow file looks similar to...',
];
const gha_example = [
' - name: Initialize Pull Request',
' uses: gha-utilities/init-pull-request',
' with:',
];
const required_inputs__private = [
{
gha_input: 'pull_request_token',
gha_example: [' pull_request_token: ${{ secrets.PULL_REQUEST_TOKEN }}'],
},
];
const required_inputs__public = [
{
gha_input: 'head',
gha_example: [' head: feature-branch'],
},
{
gha_input: 'base',
gha_example: [' base: master'],
},
{
gha_input: 'title',
gha_example: [' title: Adds awesome feature'],
},
{
gha_input: 'body',
gha_example: [
' body: >',
' Some thing can now be done that could',
' not be done before.',
],
},
];
required_inputs__private.forEach((obj) => {
gha_example.push(...obj.gha_example);
if (get_gha_input(obj.gha_input) === undefined) {
const error_message = [`Required Input \`${obj.gha_input}\` for GitHub Action was undefined`,
...error_message__base,
...gha_example,
];
throw new ReferenceError(error_message.join('\n'));
}
});
required_inputs__public.forEach((obj) => {
gha_example.push(...obj.gha_example);
if (get_gha_input(obj.gha_input) === undefined) {
const error_message = [`Required Input \`${obj.gha_input}\` for GitHub Action was undefined`,
...error_message__base,
...gha_example,
];
throw new ReferenceError(error_message.join('\n'));
}
});
const octokit = new github_1.default.GitHub(get_gha_input('pull_request_token'));
const head = get_gha_input('head');
const base = get_gha_input('base');
const title = get_gha_input('title');
const body = get_gha_input('body');
let maintainer_can_modify = get_gha_input('maintainer_can_modify', true);
if (maintainer_can_modify === undefined) {
// Assume that maintainers do want the option to modify Pull Requests
maintainer_can_modify = true;
}
let draft = get_gha_input('draft', true);
if (draft === undefined) {
draft = false;
}
/**
* @throws {Error}
*/
octokit.pulls.create({
'title': title,
'body': body,
'owner': actor,
'repo': repo,
'head': head,
'base': base,
'maintainer_can_modify': maintainer_can_modify,
'draft': draft,
}).then((response) => {
if (get_gha_input('verbose') === true || get_gha_input('verbose') === 'true') {
const verbose_results = [
`Rate Limit Remaining -> ${response['headers']['x-ratelimit-remaining']}`,
`Rate Limit Reset -> ${response['headers']['x-ratelimit-reset']}`,
`Pull Request HTML URL -> ${response['data']['html_url']}`,
`Pull Request Number -> ${response['data']['number']}`,
`Pull Request State -> ${response['data']['state']}`
];
console.log(verbose_results.join('\n'));
}
if (get_gha_input('debug') === true || get_gha_input('debug') === 'true') {
console.log('--- DEBUG START ---');
console.log('Response headers...');
console.log(JSON.stringify(response['headers']));
console.log('Response data...');
console.log(JSON.stringify(response['data']));
console.log('--- DEBUG END ---');
}
if (response['status'] !== 201) {
const error_message = ['Response status was not 201 (created), please check',
'- configurations for your Action',
'- authentication for repository (write permissions)'
];
throw new Error(error_message.join('\n'));
}
set_gha_output('html_url', response['data']['html_url']);
set_gha_output('number', response['data']['number']);
// return response;
}).catch((e) => {
const error_message = ['Failed to initialize Pull Request',
...error_message__base,
...gha_example,
];
console.error(error_message.join('\n'));
throw e;
});
//# sourceMappingURL=index.js.map