This repository was archived by the owner on Jul 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathlighthouse-ci.js
More file actions
186 lines (161 loc) · 5.97 KB
/
lighthouse-ci.js
File metadata and controls
186 lines (161 loc) · 5.97 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
/**
* Copyright 2017 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const fetch = require('node-fetch'); // polyfill
const Github = require('@octokit/rest');
const URL = require('url').URL;
const URLSearchParams = require('url').URLSearchParams;
const LIGHTHOUSE_BUILDER_HOST = process.env.LIGHTHOUSE_BUILDER_HOST || 'https://builder-dot-lighthouse-ci.appspot.com';
class LighthouseCI {
/**
* @param {!string} token Github OAuth token that has repo:status access.
*/
constructor(token) {
this.github = new Github({debug: false});
this.github.authenticate({type: 'oauth', token});
}
handleError(err, prInfo) {
console.error(err);
this.updateGithubStatus(Object.assign({
state: 'error',
description: `Error. ${err.message}`
}, prInfo));
}
testOnHeadlessChrome(body, headers) {
headers = Object.assign(headers, {
'Content-Type': 'application/json'
});
// POST https://builder-dot-lighthouse-ci.appspot.com/ci
// '{"output": "json", "url": <testUrl>}"'
return fetch(`${LIGHTHOUSE_BUILDER_HOST}/ci`, {
method: 'POST',
body: JSON.stringify(body),
headers
}).then(resp => resp.json())
.catch(err => {
throw err;
});
}
/**
* Uses WebPageTest's Rest API to run Lighthouse and score a URL.
* See https://sites.google.com/a/webpagetest.org/docs/advanced-features/webpagetest-restful-apis
* @param {!string} apiKey
* @param {!string} testUrl URL to audit.
* @param {!string} pingback URL for WPT to ping when result is ready.
* @return {!Promise} json response from starting a WPT run.
*/
startOnWebpageTest(apiKey, testUrl, pingback) {
const params = new URLSearchParams();
params.set('k', apiKey);
params.set('f', 'json');
params.set('pingback', pingback); // The pingback is passed an "id" parameter of the test.
// TODO: match emulation to LH settings.
params.set('location', 'Dulles_Nexus5:Nexus 5 - Chrome Beta.3G_EM');
// For native: Dulles_Linux:Chrome.Native
// params.set('location', 'Dulles_MotoG4:Moto G4 - Chrome Beta.3GFast');
params.set('mobile', 1); // Emulate mobile (for desktop cases).
params.set('type', 'lighthouse'); // LH-only run.
params.set('lighthouse', 1);
params.set('url', testUrl);
const wptUrl = new URL('https://www.webpagetest.org/runtest.php');
wptUrl.search = params;
return fetch(wptUrl.toString())
.then(resp => resp.json())
.catch(err => {
throw err;
});
}
/**
* Returns the scores for each category.
* @param {!Object} lhr Lighthouse results object.
* @return {!Object<string, number>>}
*/
static getOverallScores(lhr) {
const cats = Object.keys(lhr.categories);
const obj = {};
for (const cat of cats) {
obj[cat] = lhr.categories[cat].score * 100;
}
return obj;
}
/**
* Updates associated PR status.
* @param {!Object=} opts Options to set the status with.
* @return {!Promise<Object>} Status object from Github API.
*/
updateGithubStatus(opts={}) {
const statusObj = Object.assign({context: 'Lighthouse'}, opts);
return this.github.repos.createStatus(statusObj).then(status => {
console.log(status.data.description);
return status;
});
}
/**
* Updates pass/fail state of PR.
* @param {!Object} lhr Lighthouse results object.
* @param {!Object<string, number>} thresholds Minimum scores per category.
* @param {!Object} opts Options to set the status with.
* @return {!Promise<number>} Overall lighthouse score.
*/
assignPassFailToPR(lhr, thresholds, opts) {
const scores = LighthouseCI.getOverallScores(lhr);
let passing = true;
const scoresStr = [];
for (const [cat, score] of Object.entries(scores)) {
if (cat in thresholds) {
const minScore = thresholds[cat];
if (minScore > Math.round(score)) {
passing = false;
}
scoresStr.push(`${cat}:${minScore}`);
}
}
let description = `Failed. Required scores: ${scoresStr.join(',')}.`;
if (passing) {
description = 'Passed. Lighthouse scores meet thresholds.';
}
const status = Object.assign({
description,
state: passing ? 'success' : 'failure'
}, opts);
// eslint-disable-next-line no-unused-vars
return this.updateGithubStatus(status).then(status => scores);
}
/**
* Posts a comment to the PR with the latest Lighthouse scores.
* @param {!{owner: string, repo: string, number: number}} prInfo
* @param {!Object} lhr Lighthouse results object.
* @param {!Object<string, number>} thresholds Minimum scores per category.
* @return {!Promise<!Object<string, number>} Lighthouse scores.
*/
postLighthouseComment(prInfo, lhr, thresholds) {
let rows = '';
Object.values(lhr.categories).forEach(cat => {
const threshold = thresholds[cat.id] || '-';
rows += `| ${cat.title} | ${cat.score * 100} | ${threshold} \n`;
});
const body = `
Updated [Lighthouse](https://developers.google.com/web/tools/lighthouse/) report for the changes in this PR:
| Category | New score | Required threshold |
| ------------- | ------------- | ------------- |
${rows}
_Tested with Lighthouse version: ${lhr.lighthouseVersion}_`;
const scores = LighthouseCI.getOverallScores(lhr);
// eslint-disable-next-line no-unused-vars
return this.github.issues.createComment(Object.assign({body}, prInfo)).then(status => scores);
}
}
module.exports = LighthouseCI;