-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
213 lines (196 loc) · 6.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
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
const WebPageTest = require("webpagetest");
const core = require("@actions/core");
const github = require("@actions/github");
const ejs = require("ejs");
const { cornflowerblue } = require("color-name");
const { keyword } = require("color-convert");
const WPT_BUDGET = core.getInput("budget");
const WPT_OPTIONS = core.getInput("wptOptions");
const WPT_API_KEY = core.getInput("apiKey");
const WPT_URLS = core.getInput("urls").split("\n");
const WPT_LABEL = core.getInput("label");
const GITHUB_TOKEN = core.getInput("GITHUB_TOKEN");
const DIRECTORY = process.env.GITHUB_WORKSPACE;
const GH_EVENT_NAME = process.env.GITHUB_EVENT_NAME;
const METRICS = {
TTFB: "Time to First Byte",
firstContentfulPaint: "First Contentful Paint",
TotalBlockingTime: "Total Blocking Time",
"chromeUserTiming.LargestContentfulPaint": "Largest Contentful Paint",
"chromeUserTiming.CumulativeLayoutShift": "Cumulative Layout Shift",
};
const isReportSupported = () => GH_EVENT_NAME == "pull_request" || GH_EVENT_NAME == "issue_comment";
const runTest = (wpt, url, options) => {
// clone options object to avoid WPT wrapper issue
let tempOptions = JSON.parse(JSON.stringify(options));
return new Promise((resolve, reject) => {
core.info(`Submitting test for ${url} ...`);
wpt.runTest(url, tempOptions, async (err, result) => {
try {
if (result) {
core.debug(result);
return resolve({ result: result, err: err });
} else {
return reject(err);
}
} catch (e) {
core.info(e.statusText || JSON.stringify(e));
}
});
});
};
const retrieveResults = (wpt, testId) => {
return new Promise((resolve, reject) => {
wpt.getTestResults(testId, (err, data) => {
if (data) {
return resolve(data);
} else {
return reject(err);
}
});
});
};
async function renderComment(data) {
try {
const octokit = github.getOctokit(GITHUB_TOKEN, { log: console });
const context = github.context;
let markdown = await ejs.renderFile(`${__dirname}/templates/comment.md`, data);
markdown.replace(/\%/g, "%25").replace(/\n/g, "%0A").replace(/\r/g, "%0D");
const prNumber =
GH_EVENT_NAME == "pull_request"
? context.payload.pull_request.number
: GH_EVENT_NAME == "issue_comment"
? context.payload.issue.number
: null;
if (!prNumber) throw new Error('Incompatible event "' + GH_EVENT_NAME + '"');
//submit a comment
await octokit.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: markdown,
});
} catch (e) {
console.log(e);
core.setFailed(`Action failed with error: ${e.statusText || JSON.stringify(e)}`);
}
}
function collectData(results, runData) {
let testData = {
url: results.data.url,
testLink: results.data.summary,
waterfall: results.data.median.firstView.images.waterfall,
metrics: [],
};
for (const [key, value] of Object.entries(METRICS)) {
core.debug(key);
core.debug(value);
if (results.data.median.firstView[key]) {
testData.metrics.push({
name: value,
value: results.data.median.firstView[key],
});
}
}
runData["tests"].push(testData);
}
async function run() {
const wpt = new WebPageTest("www.webpagetest.org", WPT_API_KEY);
//TODO: make this configurable
let options = {
firstViewOnly: true,
runs: 3,
location: "Dulles:Chrome",
connectivity: "4G",
pollResults: 5,
timeout: 240,
emulateMobile: true,
};
if (WPT_OPTIONS) {
let settings = require(`${DIRECTORY}/${WPT_OPTIONS}`);
if (typeof settings === "object" && settings !== null) {
core.debug(settings);
options = {
...options,
...settings,
};
} else {
core.setFailed("The specified WebPageTest settings aren't a valid JavaScript object");
}
}
if (WPT_BUDGET) {
options.specs = require(`${DIRECTORY}/${WPT_BUDGET}`);
}
if (WPT_LABEL) {
options.label = WPT_LABEL;
}
core.startGroup("WebPageTest Configuration");
core.info(`WebPageTest settings: ${JSON.stringify(options, null, " ")}`);
core.endGroup();
core.startGroup(`Testing urls in WebPageTest..`);
//for our commit
let runData = {};
runData["tests"] = [];
Promise.all(
WPT_URLS.map(async (url) => {
try {
await runTest(wpt, url, options).then(async (result) => {
try {
if (result.result.testId) {
//test submitted with specs
core.info(
"Tests successfully completed for " +
url +
". Full results at https://" +
wpt.config.hostname +
"/result/" +
result.result.testId
);
if (isReportSupported()) {
let testResults = await retrieveResults(wpt, result.result.testId);
collectData(testResults, runData);
}
// testspecs also returns the number of assertion fails as err
// > 0 means we need to fail
if (result.err && result.err > 0) {
if (result.err == 1) {
core.setFailed("One performance budget not met.");
} else {
core.setFailed(result.err + " performance budgets not met.");
}
}
return;
} else if (result.result.data) {
//test was submitted without testspecs
core.info(
"Tests successfully completed for " +
url +
". Full results at " +
result.result.data.summary
);
if (isReportSupported()) {
let testResults = await retrieveResults(wpt, result.result.data.id);
collectData(testResults, runData);
}
return;
} else {
return;
}
} catch (e) {
console.log(e);
core.setFailed(`Action failed with error: ${e.statusText || JSON.stringify(e)}`);
}
});
} catch (e) {
console.log(e);
core.setFailed(`Action failed with error: ${e.statusText || JSON.stringify(e)}`);
}
})
).then(() => {
if (isReportSupported()) {
renderComment(runData);
}
});
return;
}
run();