forked from preactjs/compressed-size-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.js
290 lines (246 loc) · 7.5 KB
/
action.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import path from 'path';
import fs from 'fs';
import { getInput, setFailed, startGroup, endGroup, debug } from '@actions/core';
import { GitHub, context } from '@actions/github';
import { exec } from '@actions/exec';
import SizePlugin from 'size-plugin-core';
import prettyBytes from 'pretty-bytes';
async function fileExists(filename) {
try {
await fs.promises.access(filename, fs.constants.F_OK);
return true;
} catch (e) {}
return false;
}
async function run(octokit, context) {
const { owner, repo, number: pull_number } = context.issue;
// const pr = (await octokit.pulls.get({ owner, repo, pull_number })).data;
const pr = context.payload.pull_request;
try {
debug('pr' + JSON.stringify(pr, null, 2));
} catch (e) {}
const plugin = new SizePlugin({
compression: getInput('compression'),
pattern: getInput('pattern') || '**/dist/*.js',
exclude: getInput('exclude') || '{**/*.map,**/node_modules/**}'
});
console.log(`PR #${pull_number} is targetted at ${pr.base.ref} (${pr.base.sha})`);
const buildScript = getInput('build-script') || 'build';
const cwd = process.cwd();
const yarnLock = await fileExists(path.resolve(cwd, 'yarn.lock'));
const packageLock = await fileExists(path.resolve(cwd, 'package-lock.json'));
let npm = `npm`;
let installScript = getInput('install-script')
if (!installScript) {
if (yarnLock) {
installScript = npm = `yarn`;
}
else if (packageLock) {
installScript = `npm ci`;
}
else {
installScript = `npm install`;
}
}
startGroup(`[current] Install Dependencies`);
console.log(`Installing using ${installScript}`)
await exec(installScript);
endGroup();
startGroup(`[current] Build using ${npm}`);
console.log(`Building using ${npm} run ${buildScript}`);
await exec(`${npm} run ${buildScript}`);
endGroup();
const newSizes = await plugin.readFromDisk(cwd);
startGroup(`[base] Checkout target branch`);
let baseRef;
try {
baseRef = context.payload.base.ref;
if (!baseRef) throw Error('missing context.payload.pull_request.base.ref');
await exec(`git fetch -n origin ${context.payload.pull_request.base.ref}`);
console.log('successfully fetched base.ref');
} catch (e) {
console.log('fetching base.ref failed', e.message);
try {
await exec(`git fetch -n origin ${pr.base.sha}`);
console.log('successfully fetched base.sha');
} catch (e) {
console.log('fetching base.sha failed', e.message);
try {
await exec(`git fetch -n`);
} catch (e) {
console.log('fetch failed', e.message);
}
}
}
console.log('checking out and building base commit');
try {
if (!baseRef) throw Error('missing context.payload.base.ref');
await exec(`git checkout ${baseRef}`);
}
catch (e) {
await exec(`git checkout ${pr.base.sha}`);
}
endGroup();
startGroup(`[base] Install Dependencies`);
await exec(installScript);
endGroup();
startGroup(`[current] Build using ${npm}`);
await exec(`${npm} run ${buildScript}`);
endGroup();
const oldSizes = await plugin.readFromDisk(cwd);
const diff = await plugin.getDiff(oldSizes, newSizes);
startGroup(`Size Differences:`);
const cliText = await plugin.printSizes(diff);
console.log(cliText);
endGroup();
const markdownDiff = diffTable(diff, {
collapseUnchanged: toBool(getInput('collapse-unchanged')),
omitUnchanged: toBool(getInput('omit-unchanged')),
showTotal: toBool(getInput('show-total'))
});
const commentInfo = {
...context.repo,
issue_number: pull_number
};
const comment = {
...commentInfo,
body: markdownDiff + '\n\n<a href="https://github.com/preactjs/compressed-size-action"><sub>compressed-size-action</sub></a>'
};
startGroup(`Updating stats PR comment`);
let commentId;
try {
const comments = (await octokit.issues.listComments(commentInfo)).data;
for (let i=comments.length; i--; ) {
const c = comments[i];
if (c.user.type === 'Bot' && /<sub>[\s\n]*(compressed|gzip)-size-action/.test(c.body)) {
commentId = c.id;
break;
}
}
}
catch (e) {
console.log('Error checking for previous comments: ' + e.message);
}
if (commentId) {
console.log(`Updating previous comment #${commentId}`)
try {
await octokit.issues.updateComment({
...context.repo,
comment_id: commentId,
body: comment.body
});
}
catch (e) {
console.log('Error editing previous comment: ' + e.message);
commentId = null;
}
}
let outputRawMarkdown = false;
// no previous or edit failed
if (!commentId) {
console.log('Creating new comment');
try {
await octokit.issues.createComment(comment);
} catch (e) {
console.log(`Error creating comment: ${e.message}`);
console.log(`Submitting a PR review comment instead...`);
try {
const issue = context.issue || pr;
await octokit.pulls.createReview({
owner: issue.owner,
repo: issue.repo,
pull_number: issue.number,
event: 'COMMENT',
body: comment.body
});
} catch (e) {
console.log('Error creating PR review.');
outputRawMarkdown = true;
}
}
}
endGroup();
if (outputRawMarkdown) {
console.log(`
Error: compressed-size-action was unable to comment on your PR.
This can happen for PR's originating from a fork without write permissions.
You can copy the size table directly into a comment using the markdown below:
\n\n${comment.body}\n\n
`.replace(/^(\t| )+/gm, ''));
}
console.log('All done!');
}
function diffTable(files, { showTotal, collapseUnchanged, omitUnchanged }) {
let out = `| Filename | Size | Change | |\n`;
out += `|:--- |:---:|:---:|:---:|\n`;
let outUnchanged = out;
let totalSize = 0;
let totalDelta = 0;
let unchanged = 0;
let changed = 0;
for (const file of files) {
const { filename, size, delta } = file;
totalSize += size;
totalDelta += delta;
const difference = (delta / size * 100) | 0;
let deltaText = getDeltaText(delta, difference);
let icon = iconForDifference(difference);
const s = `| \`${filename}\` | ${prettyBytes(size)} | ${deltaText} | ${icon} |\n`;
const isUnchanged = Math.abs(delta) < 1;
if (isUnchanged && omitUnchanged) continue;
if (isUnchanged && collapseUnchanged) {
unchanged++;
outUnchanged += s;
}
else {
changed++;
out += s;
}
}
// no changes, don't show an empty table
if (!changed) {
out = '';
}
if (unchanged) {
out += `\n<details><summary>ℹ️ <strong>View Unchanged</strong></summary>\n\n${outUnchanged}\n\n</details>\n\n`;
}
if (showTotal) {
const totalDifference = (totalDelta / totalSize * 100) | 0;
let totalDeltaText = getDeltaText(totalDelta, totalDifference);
let totalIcon = iconForDifference(totalDifference);
out = `**Total Size:** ${prettyBytes(totalSize)}\n\n${out}`;
out = `**Size Change:** ${totalDeltaText} ${totalIcon}\n\n${out}`;
}
return out;
}
function getDeltaText(delta, difference) {
let deltaText = (delta > 0 ? '+' : '') + prettyBytes(delta);
if (delta && Math.abs(delta) > 1) {
deltaText += ` (${Math.abs(difference)}%)`;
}
return deltaText;
}
function iconForDifference(difference) {
let icon = '';
if (difference >= 50) icon = '🆘';
else if (difference >= 20) icon = '🚨';
else if (difference >= 10) icon = '⚠️';
else if (difference >= 5) icon = '🔍';
else if (difference <= -50) icon = '🏆';
else if (difference <= -20) icon = '🎉';
else if (difference <= -10) icon = '👏';
else if (difference <= -5) icon = '✅';
return icon;
}
function toBool(v) {
return /^(1|true|yes)$/.test(v);
}
(async () => {
try {
const token = getInput('repo-token', { required: true });
const octokit = new GitHub(token);
await run(octokit, context);
} catch (e) {
setFailed(e.message);
}
})();