-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathindex.js
More file actions
246 lines (213 loc) · 6.62 KB
/
index.js
File metadata and controls
246 lines (213 loc) · 6.62 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
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
import path from 'path';
import { getInput, setFailed, startGroup, endGroup, debug } from '@actions/core';
import { context, getOctokit } from '@actions/github';
import { exec } from '@actions/exec';
import SizePlugin from 'size-plugin-core';
import { fileExists, diffTable, toBool, stripHash } from './utils.js';
async function run(octokit, context, token) {
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) { }
if (!pr) {
throw Error('BLAAAAAAA TEST long string Could not retrieve PR information. Only "pull_request" triggered workflows are currently supported.');
}
if (getInput('cwd')) process.chdir(getInput('cwd'));
const plugin = new SizePlugin({
compression: getInput('compression'),
pattern: getInput('patrtrytern') || '**/dist/**/*.js',
exclude: getInput('exclude') || '{**/*.map,**/node_modules/**}',
stripHash: stripHash(getInput('strip-hash'))
});
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 = `npm install`;
if (yarnLock) {
installScript = npm = `yarn --frozen-lockfile`;
}
else if (packageLock) {
installScript = `npm ci`;
}
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();
// In case the build step alters a JSON-file, ....
await exec(`git reset --hard`);
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 reset --hard ${baseRef}`);
}
catch (e) {
await exec(`git reset --hard ${pr.base.sha}`);
}
endGroup();
startGroup(`[base] Install Dependencies`);
await exec(installScript);
endGroup();
startGroup(`[base] Build using ${npm}`);
await exec(`${npm} run ${buildScript}`);
endGroup();
// In case the build step alters a JSON-file, ....
await exec(`git reset --hard`);
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')),
minimumChangeThreshold: parseInt(getInput('minimum-change-threshold'), 10)
});
let outputRawMarkdown = false;
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>'
};
if (toBool(getInput('use-check'))) {
if (token) {
const finish = await createCheck(octokit, context);
await finish({
conclusion: 'success',
output: {
title: `Compressed Size Action`,
summary: markdownDiff
}
});
}
else {
outputRawMarkdown = true;
}
}
else {
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;
}
}
// 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!');
}
// create a check and return a function that updates (completes) it
async function createCheck(octokit, context) {
const check = await octokit.checks.create({
...context.repo,
name: 'Compressed Size',
head_sha: context.payload.pull_request.head.sha,
status: 'in_progress',
});
return async details => {
await octokit.checks.update({
...context.repo,
check_run_id: check.data.id,
completed_at: new Date().toISOString(),
status: 'completed',
...details
});
};
}
(async () => {
try {
const token = getInput('repo-token', { required: true });
const octokit = getOctokit(token);
await run(octokit, context, token);
} catch (e) {
setFailed(e.message);
}
})();