Skip to content

Commit b9717fb

Browse files
authored
Merge pull request #4 from productboardlabs/pr-comment-trigger
Adds the ability to report based on PR comments
2 parents 582880b + 138d363 commit b9717fb

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

index.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const METRICS = {
2121
"chromeUserTiming.CumulativeLayoutShift": "Cumulative Layout Shift",
2222
}
2323

24+
const isReportSupported = () => GH_EVENT_NAME == 'pull_request' || GH_EVENT_NAME == 'issue_comment';
25+
2426
const runTest = (wpt, url, options) => {
2527
// clone options object to avoid WPT wrapper issue
2628
let tempOptions = JSON.parse(JSON.stringify(options));
@@ -57,18 +59,26 @@ async function renderComment(data) {
5759
try {
5860
const octokit = github.getOctokit(GITHUB_TOKEN, {log: console});
5961
const context = github.context;
60-
62+
6163
let markdown = await ejs.renderFile(`${__dirname}/templates/comment.md`, data);
6264
markdown
6365
.replace(/\%/g, '%25')
6466
.replace(/\n/g, '%0A')
6567
.replace(/\r/g, '%0D')
6668

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+
6777
//submit a comment
6878
await octokit.issues.createComment({
6979
owner: context.repo.owner,
7080
repo: context.repo.repo,
71-
issue_number: context.payload.pull_request.number,
81+
issue_number: prNumber,
7282
body: markdown
7383
});
7484
} catch (e) {
@@ -147,7 +157,7 @@ async function run() {
147157
+ url +'. Full results at https://'
148158
+ wpt.config.hostname + '/result/' + result.result.testId);
149159

150-
if (GH_EVENT_NAME == 'pull_request') {
160+
if (isReportSupported()) {
151161
let testResults = await retrieveResults(wpt, result.result.testId);
152162
collectData(testResults, runData);
153163

@@ -167,7 +177,7 @@ async function run() {
167177
core.info('Tests successfully completed for ' + url
168178
+'. Full results at ' + result.result.data.summary);
169179

170-
if (GH_EVENT_NAME == 'pull_request') {
180+
if (isReportSupported()) {
171181
let testResults = await retrieveResults(wpt, result.result.data.id);
172182
collectData(testResults, runData);
173183
}
@@ -184,12 +194,12 @@ async function run() {
184194
core.setFailed(`Action failed with error ${e}`);
185195
}
186196
})).then(() => {
187-
if (GH_EVENT_NAME == 'pull_request') {
197+
if (isReportSupported()) {
188198
renderComment(runData);
189199
}
190200
});
191201

192202
return;
193203
}
194204

195-
run();
205+
run();

0 commit comments

Comments
 (0)