|
1 | 1 | const WebPageTest = require("webpagetest"); |
2 | 2 | const core = require("@actions/core"); |
3 | | -const github = require('@actions/github'); |
4 | | -const ejs = require('ejs'); |
| 3 | +const github = require("@actions/github"); |
| 4 | +const ejs = require("ejs"); |
5 | 5 | const { cornflowerblue } = require("color-name"); |
6 | 6 | const { keyword } = require("color-convert"); |
7 | 7 |
|
8 | | -const WPT_BUDGET = core.getInput('budget'); |
9 | | -const WPT_OPTIONS = core.getInput('wptOptions'); |
10 | | -const WPT_API_KEY = core.getInput('apiKey'); |
11 | | -const WPT_URLS = core.getInput('urls').split("\n"); |
12 | | -const WPT_LABEL = core.getInput('label'); |
13 | | -const GITHUB_TOKEN = core.getInput('GITHUB_TOKEN'); |
| 8 | +const WPT_BUDGET = core.getInput("budget"); |
| 9 | +const WPT_OPTIONS = core.getInput("wptOptions"); |
| 10 | +const WPT_API_KEY = core.getInput("apiKey"); |
| 11 | +const WPT_URLS = core.getInput("urls").split("\n"); |
| 12 | +const WPT_LABEL = core.getInput("label"); |
| 13 | +const GITHUB_TOKEN = core.getInput("GITHUB_TOKEN"); |
14 | 14 | const DIRECTORY = process.env.GITHUB_WORKSPACE; |
15 | 15 | const GH_EVENT_NAME = process.env.GITHUB_EVENT_NAME; |
16 | 16 | const METRICS = { |
17 | | - "TTFB": "Time to First Byte", |
18 | | - "firstContentfulPaint": "First Contentful Paint", |
19 | | - "TotalBlockingTime": "Total Blocking Time", |
20 | | - "chromeUserTiming.LargestContentfulPaint": "Largest Contentful Paint", |
21 | | - "chromeUserTiming.CumulativeLayoutShift": "Cumulative Layout Shift", |
22 | | -} |
| 17 | + TTFB: "Time to First Byte", |
| 18 | + firstContentfulPaint: "First Contentful Paint", |
| 19 | + TotalBlockingTime: "Total Blocking Time", |
| 20 | + "chromeUserTiming.LargestContentfulPaint": "Largest Contentful Paint", |
| 21 | + "chromeUserTiming.CumulativeLayoutShift": "Cumulative Layout Shift", |
| 22 | +}; |
23 | 23 |
|
24 | | -const isReportSupported = () => GH_EVENT_NAME == 'pull_request' || GH_EVENT_NAME == 'issue_comment'; |
| 24 | +const isReportSupported = () => GH_EVENT_NAME == "pull_request" || GH_EVENT_NAME == "issue_comment"; |
25 | 25 |
|
26 | 26 | const runTest = (wpt, url, options) => { |
27 | | - // clone options object to avoid WPT wrapper issue |
28 | | - let tempOptions = JSON.parse(JSON.stringify(options)); |
29 | | - |
30 | | - return new Promise((resolve, reject) => { |
31 | | - core.info(`Submitting test for ${url} ...`); |
32 | | - wpt.runTest(url, tempOptions, async(err, result) => { |
33 | | - try { |
34 | | - if (result) { |
35 | | - core.debug(result); |
36 | | - return resolve({'result':result,'err':err}); |
37 | | - } else { |
38 | | - return reject(err); |
39 | | - } |
40 | | - } catch (e) { |
41 | | - core.info(e.statusText || JSON.stringify(e)); |
42 | | - } |
43 | | - }) |
| 27 | + // clone options object to avoid WPT wrapper issue |
| 28 | + let tempOptions = JSON.parse(JSON.stringify(options)); |
| 29 | + |
| 30 | + return new Promise((resolve, reject) => { |
| 31 | + core.info(`Submitting test for ${url} ...`); |
| 32 | + wpt.runTest(url, tempOptions, async (err, result) => { |
| 33 | + try { |
| 34 | + if (result) { |
| 35 | + core.debug(result); |
| 36 | + return resolve({ result: result, err: err }); |
| 37 | + } else { |
| 38 | + return reject(err); |
| 39 | + } |
| 40 | + } catch (e) { |
| 41 | + core.info(e.statusText || JSON.stringify(e)); |
| 42 | + } |
44 | 43 | }); |
45 | | -} |
| 44 | + }); |
| 45 | +}; |
46 | 46 |
|
47 | 47 | const retrieveResults = (wpt, testId) => { |
48 | | - return new Promise((resolve, reject) => { |
49 | | - wpt.getTestResults(testId, (err, data) => { |
50 | | - if (data) { |
51 | | - return resolve(data); |
52 | | - } else { |
53 | | - return reject(err); |
54 | | - } |
55 | | - }); |
| 48 | + return new Promise((resolve, reject) => { |
| 49 | + wpt.getTestResults(testId, (err, data) => { |
| 50 | + if (data) { |
| 51 | + return resolve(data); |
| 52 | + } else { |
| 53 | + return reject(err); |
| 54 | + } |
56 | 55 | }); |
57 | | -} |
| 56 | + }); |
| 57 | +}; |
58 | 58 | async function renderComment(data) { |
59 | | - try { |
60 | | - const octokit = github.getOctokit(GITHUB_TOKEN, {log: console}); |
61 | | - const context = github.context; |
62 | | - |
63 | | - let markdown = await ejs.renderFile(`${__dirname}/templates/comment.md`, data); |
64 | | - markdown |
65 | | - .replace(/\%/g, '%25') |
66 | | - .replace(/\n/g, '%0A') |
67 | | - .replace(/\r/g, '%0D') |
68 | | - |
69 | | - const prNumber = GH_EVENT_NAME == 'pull_request' |
70 | | - ? context.payload.pull_request.number |
71 | | - : GH_EVENT_NAME == 'issue_comment' ? |
72 | | - context.payload.issue.number : null; |
73 | | - |
74 | | - if (!prNumber) |
75 | | - throw new Error('Incompatible event "' + GH_EVENT_NAME + '"'); |
76 | | - |
77 | | - //submit a comment |
78 | | - await octokit.issues.createComment({ |
79 | | - owner: context.repo.owner, |
80 | | - repo: context.repo.repo, |
81 | | - issue_number: prNumber, |
82 | | - body: markdown |
83 | | - }); |
84 | | - } catch (e) { |
85 | | - core.setFailed(`Action failed with error: ${e.statusText || JSON.stringify(e)}`); |
86 | | - } |
| 59 | + try { |
| 60 | + const octokit = github.getOctokit(GITHUB_TOKEN, { log: console }); |
| 61 | + const context = github.context; |
| 62 | + |
| 63 | + let markdown = await ejs.renderFile(`${__dirname}/templates/comment.md`, data); |
| 64 | + markdown.replace(/\%/g, "%25").replace(/\n/g, "%0A").replace(/\r/g, "%0D"); |
| 65 | + |
| 66 | + const prNumber = |
| 67 | + GH_EVENT_NAME == "pull_request" |
| 68 | + ? context.payload.pull_request.number |
| 69 | + : GH_EVENT_NAME == "issue_comment" |
| 70 | + ? context.payload.issue.number |
| 71 | + : null; |
| 72 | + |
| 73 | + if (!prNumber) throw new Error('Incompatible event "' + GH_EVENT_NAME + '"'); |
| 74 | + |
| 75 | + //submit a comment |
| 76 | + await octokit.rest.issues.createComment({ |
| 77 | + owner: context.repo.owner, |
| 78 | + repo: context.repo.repo, |
| 79 | + issue_number: prNumber, |
| 80 | + body: markdown, |
| 81 | + }); |
| 82 | + } catch (e) { |
| 83 | + console.log(e); |
| 84 | + core.setFailed(`Action failed with error: ${e.statusText || JSON.stringify(e)}`); |
| 85 | + } |
87 | 86 | } |
88 | 87 | function collectData(results, runData) { |
89 | | - let testData = { |
90 | | - "url": results.data.url, |
91 | | - "testLink": results.data.summary, |
92 | | - "waterfall": results.data.median.firstView.images.waterfall, |
93 | | - "metrics": [] |
| 88 | + let testData = { |
| 89 | + url: results.data.url, |
| 90 | + testLink: results.data.summary, |
| 91 | + waterfall: results.data.median.firstView.images.waterfall, |
| 92 | + metrics: [], |
| 93 | + }; |
| 94 | + for (const [key, value] of Object.entries(METRICS)) { |
| 95 | + core.debug(key); |
| 96 | + core.debug(value); |
| 97 | + if (results.data.median.firstView[key]) { |
| 98 | + testData.metrics.push({ |
| 99 | + name: value, |
| 100 | + value: results.data.median.firstView[key], |
| 101 | + }); |
94 | 102 | } |
95 | | - for (const [key, value] of Object.entries(METRICS)) { |
96 | | - core.debug(key); |
97 | | - core.debug(value); |
98 | | - if (results.data.median.firstView[key]) { |
99 | | - testData.metrics.push({ |
100 | | - "name": value, |
101 | | - "value": results.data.median.firstView[key] |
102 | | - }) |
103 | | - } |
104 | | - }; |
105 | | - runData["tests"].push(testData); |
| 103 | + } |
| 104 | + runData["tests"].push(testData); |
106 | 105 | } |
107 | 106 | async function run() { |
108 | | - const wpt = new WebPageTest('www.webpagetest.org',WPT_API_KEY); |
109 | | - |
110 | | - //TODO: make this configurable |
111 | | - let options = { |
112 | | - "firstViewOnly": true, |
113 | | - "runs": 3, |
114 | | - "location": 'Dulles:Chrome', |
115 | | - "connectivity": '4G', |
116 | | - "pollResults": 5, |
117 | | - "timeout": 240, |
118 | | - "emulateMobile": true |
119 | | - } |
120 | | - if (WPT_OPTIONS) { |
121 | | - let settings = require(`${DIRECTORY}/${WPT_OPTIONS}`); |
122 | | - if (typeof settings === 'object' && settings !== null) { |
123 | | - core.debug(settings); |
124 | | - options = { |
125 | | - ...options, |
126 | | - ...settings |
127 | | - } |
128 | | - } else { |
129 | | - core.setFailed("The specified WebPageTest settings aren't a valid JavaScript object"); |
130 | | - } |
| 107 | + const wpt = new WebPageTest("www.webpagetest.org", WPT_API_KEY); |
131 | 108 |
|
| 109 | + //TODO: make this configurable |
| 110 | + let options = { |
| 111 | + firstViewOnly: true, |
| 112 | + runs: 3, |
| 113 | + location: "Dulles:Chrome", |
| 114 | + connectivity: "4G", |
| 115 | + pollResults: 5, |
| 116 | + timeout: 240, |
| 117 | + emulateMobile: true, |
| 118 | + }; |
| 119 | + if (WPT_OPTIONS) { |
| 120 | + let settings = require(`${DIRECTORY}/${WPT_OPTIONS}`); |
| 121 | + if (typeof settings === "object" && settings !== null) { |
| 122 | + core.debug(settings); |
| 123 | + options = { |
| 124 | + ...options, |
| 125 | + ...settings, |
| 126 | + }; |
| 127 | + } else { |
| 128 | + core.setFailed("The specified WebPageTest settings aren't a valid JavaScript object"); |
132 | 129 | } |
133 | | - if (WPT_BUDGET) { |
134 | | - options.specs = require(`${DIRECTORY}/${WPT_BUDGET}`); |
135 | | - } |
136 | | - if (WPT_LABEL) { |
137 | | - options.label = WPT_LABEL; |
138 | | - } |
| 130 | + } |
| 131 | + if (WPT_BUDGET) { |
| 132 | + options.specs = require(`${DIRECTORY}/${WPT_BUDGET}`); |
| 133 | + } |
| 134 | + if (WPT_LABEL) { |
| 135 | + options.label = WPT_LABEL; |
| 136 | + } |
| 137 | + |
| 138 | + core.startGroup("WebPageTest Configuration"); |
| 139 | + core.info(`WebPageTest settings: ${JSON.stringify(options, null, " ")}`); |
| 140 | + core.endGroup(); |
| 141 | + |
| 142 | + core.startGroup(`Testing urls in WebPageTest..`); |
| 143 | + //for our commit |
| 144 | + let runData = {}; |
| 145 | + runData["tests"] = []; |
| 146 | + |
| 147 | + Promise.all( |
| 148 | + WPT_URLS.map(async (url) => { |
| 149 | + try { |
| 150 | + await runTest(wpt, url, options).then(async (result) => { |
| 151 | + try { |
| 152 | + if (result.result.testId) { |
| 153 | + //test submitted with specs |
| 154 | + core.info( |
| 155 | + "Tests successfully completed for " + |
| 156 | + url + |
| 157 | + ". Full results at https://" + |
| 158 | + wpt.config.hostname + |
| 159 | + "/result/" + |
| 160 | + result.result.testId |
| 161 | + ); |
139 | 162 |
|
140 | | - core.startGroup('WebPageTest Configuration'); |
141 | | - core.info(`WebPageTest settings: ${JSON.stringify(options, null, ' ')}`) |
142 | | - core.endGroup(); |
143 | | - |
144 | | - core.startGroup(`Testing urls in WebPageTest..`); |
145 | | - //for our commit |
146 | | - let runData = {}; |
147 | | - runData["tests"] = []; |
148 | | - |
149 | | - Promise.all(WPT_URLS.map(async url=> { |
150 | | - try { |
151 | | - await runTest(wpt, url, options) |
152 | | - .then(async result => { |
153 | | - try { |
154 | | - if (result.result.testId) { |
155 | | - //test submitted with specs |
156 | | - core.info('Tests successfully completed for ' |
157 | | - + url +'. Full results at https://' |
158 | | - + wpt.config.hostname + '/result/' + result.result.testId); |
159 | | - |
160 | | - if (isReportSupported()) { |
161 | | - let testResults = await retrieveResults(wpt, result.result.testId); |
162 | | - collectData(testResults, runData); |
163 | | - |
164 | | - } |
165 | | - // testspecs also returns the number of assertion fails as err |
166 | | - // > 0 means we need to fail |
167 | | - if (result.err && result.err > 0) { |
168 | | - if (result.err == 1) { |
169 | | - core.setFailed('One performance budget not met.') |
170 | | - } else { |
171 | | - core.setFailed(result.err + ' performance budgets not met.') |
172 | | - } |
173 | | - } |
174 | | - return; |
175 | | - } else if (result.result.data) { |
176 | | - //test was submitted without testspecs |
177 | | - core.info('Tests successfully completed for ' + url |
178 | | - +'. Full results at ' + result.result.data.summary); |
179 | | - |
180 | | - if (isReportSupported()) { |
181 | | - let testResults = await retrieveResults(wpt, result.result.data.id); |
182 | | - collectData(testResults, runData); |
183 | | - } |
184 | | - return; |
185 | | - } else { |
186 | | - return; |
187 | | - } |
188 | | - } catch (e) { |
189 | | - core.setFailed(`Action failed with error: ${e.statusText || JSON.stringify(e)}`); |
190 | | - } |
191 | | - |
192 | | - }); |
193 | | - } catch (e) { |
194 | | - core.setFailed(`Action failed with error: ${e.statusText || JSON.stringify(e)}`); |
| 163 | + if (isReportSupported()) { |
| 164 | + let testResults = await retrieveResults(wpt, result.result.testId); |
| 165 | + collectData(testResults, runData); |
| 166 | + } |
| 167 | + // testspecs also returns the number of assertion fails as err |
| 168 | + // > 0 means we need to fail |
| 169 | + if (result.err && result.err > 0) { |
| 170 | + if (result.err == 1) { |
| 171 | + core.setFailed("One performance budget not met."); |
| 172 | + } else { |
| 173 | + core.setFailed(result.err + " performance budgets not met."); |
| 174 | + } |
| 175 | + } |
| 176 | + return; |
| 177 | + } else if (result.result.data) { |
| 178 | + //test was submitted without testspecs |
| 179 | + core.info( |
| 180 | + "Tests successfully completed for " + |
| 181 | + url + |
| 182 | + ". Full results at " + |
| 183 | + result.result.data.summary |
| 184 | + ); |
| 185 | + |
| 186 | + if (isReportSupported()) { |
| 187 | + let testResults = await retrieveResults(wpt, result.result.data.id); |
| 188 | + collectData(testResults, runData); |
| 189 | + } |
| 190 | + return; |
| 191 | + } else { |
| 192 | + return; |
195 | 193 | } |
196 | | - })).then(() => { |
197 | | - if (isReportSupported()) { |
198 | | - renderComment(runData); |
199 | | - } |
200 | | - }); |
201 | | - |
202 | | - return; |
| 194 | + } catch (e) { |
| 195 | + console.log(e); |
| 196 | + core.setFailed(`Action failed with error: ${e.statusText || JSON.stringify(e)}`); |
| 197 | + } |
| 198 | + }); |
| 199 | + } catch (e) { |
| 200 | + console.log(e); |
| 201 | + core.setFailed(`Action failed with error: ${e.statusText || JSON.stringify(e)}`); |
| 202 | + } |
| 203 | + }) |
| 204 | + ).then(() => { |
| 205 | + if (isReportSupported()) { |
| 206 | + renderComment(runData); |
| 207 | + } |
| 208 | + }); |
| 209 | + |
| 210 | + return; |
203 | 211 | } |
204 | 212 |
|
205 | 213 | run(); |
0 commit comments