Skip to content

Commit dc65131

Browse files
committed
Show default metrics on PR
1 parent a52cb5b commit dc65131

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

index.js

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const core = require("@actions/core");
33
const github = require('@actions/github');
44
const ejs = require('ejs');
55
const { cornflowerblue } = require("color-name");
6+
const { keyword } = require("color-convert");
67

78
const WPT_BUDGET = core.getInput('budget');
89
const WPT_OPTIONS = core.getInput('wptOptions');
@@ -11,6 +12,13 @@ const WPT_URLS = core.getInput('urls').split("\n");
1112
const WPT_LABEL = core.getInput('label');
1213
const GITHUB_TOKEN = core.getInput('GITHUB_TOKEN');
1314
const GH_EVENT_NAME = process.env.GITHUB_EVENT_NAME;
15+
const METRICS = {
16+
"TTFB": "Time to First Byte",
17+
"firstContentfulPaint": "First Contentful Paint",
18+
"TotalBlockingTime": "Total Blocking Time",
19+
"chromeUserTiming.LargestContentfulPaint": "Largest Contentful Paint",
20+
"chromeUserTiming.CumulativeLayoutShift": "Cumulative Layout Shift",
21+
}
1422

1523
const runTest = (wpt, url, options) => {
1624
// clone options object to avoid WPT wrapper issue
@@ -48,6 +56,7 @@ async function renderComment(data) {
4856
try {
4957
const octokit = github.getOctokit(GITHUB_TOKEN, {log: console});
5058
const context = github.context;
59+
5160
let markdown = await ejs.renderFile('./templates/comment.md', data);
5261
markdown
5362
.replace(/\%/g, '%25')
@@ -65,6 +74,25 @@ async function renderComment(data) {
6574
core.setFailed(`Action failed with error ${e}`);
6675
}
6776
}
77+
function collectData(results, runData) {
78+
let testData = {
79+
"url": results.data.url,
80+
"testLink": results.data.summary,
81+
"waterfall": results.data.median.firstView.images.waterfall,
82+
"metrics": []
83+
}
84+
for (const [key, value] of Object.entries(METRICS)) {
85+
core.debug(key);
86+
core.debug(value);
87+
if (results.data.median.firstView[key]) {
88+
testData.metrics.push({
89+
"name": value,
90+
"value": results.data.median.firstView[key]
91+
})
92+
}
93+
};
94+
runData["tests"].push(testData);
95+
}
6896
async function run() {
6997
const wpt = new WebPageTest('www.webpagetest.org',WPT_API_KEY);
7098

@@ -119,12 +147,8 @@ async function run() {
119147

120148
if (GH_EVENT_NAME == 'pull_request') {
121149
let testResults = await retrieveResults(wpt, result.result.testId);
122-
let testData = {
123-
"url": testResults.data.url,
124-
"testLink": testResults.data.summary,
125-
"waterfall": testResults.data.median.firstView.images.waterfall
126-
}
127-
runData["tests"].push(testData);
150+
collectData(testResults, runData);
151+
128152
}
129153
// testspecs also returns the number of assertion fails as err
130154
// > 0 means we need to fail
@@ -143,12 +167,7 @@ async function run() {
143167

144168
if (GH_EVENT_NAME == 'pull_request') {
145169
let testResults = await retrieveResults(wpt, result.result.data.id);
146-
let testData = {
147-
"url": testResults.data.url,
148-
"testLink": testResults.data.summary,
149-
"waterfall": testResults.data.median.firstView.thumbnails.waterfall
150-
}
151-
runData["tests"].push(testData);
170+
collectData(testResults, runData);
152171
}
153172
return;
154173
} else {

templates/comment.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@ Automatically triggered by [WebPageTest](https://www.webpagetest.org)'s GitHub A
55
## Page Tested:<%- test.url %>
66
**Full test results: <%- test.testLink %>/**
77

8+
| <% test.metrics.forEach((metric) => { %><%- metric.name %> | <% }); %>
9+
| <% test.metrics.forEach((metric) => { %>--- | <% }); %>
10+
| <% test.metrics.forEach((metric) => { %><%- metric.value %> | <% }); %>
11+
812
![Image of waterfall](<%- test.waterfall %>)
913
<% }); %>

0 commit comments

Comments
 (0)