-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpostPrComment.js
122 lines (107 loc) · 3.25 KB
/
postPrComment.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
import fetch from './fetch';
import getLighthouseScoreColor from './helpers/getLighthouseScoreColor';
import lighthouseAuditTitles from './lighthouseAuditTitles';
import LighthouseCheckError from './LighthouseCheckError';
import { ERROR_UNEXPECTED_RESPONSE } from './errorCodes';
import { NAME } from './constants';
const getBadge = ({ title, score }) =>
`![](https://img.shields.io/badge/${title}-${score}-${getLighthouseScoreColor(
{
isHex: false,
score
}
)}?style=flat-square) `;
export default async ({
prCommentAccessToken,
prCommentSaveOld,
prCommentUrl,
results,
verbose
}) => {
try {
let markdown = '';
results.forEach((result, index) => {
// badges
Object.keys(result.scores).forEach(current => {
markdown += getBadge({
title: lighthouseAuditTitles[current].replace(/ /g, '%20'),
score: result.scores[current]
});
});
// the emulatedformfactor
markdown += `\n\n Device: **${result.emulatedFormFactor}**`;
// the url
markdown += `\n\n${result.url}`;
// if we have a URL for the full report
if (result.report) {
markdown += `\n\n[Full report](${result.report})`;
}
// add a horizontal line
if (index + 1 < results.length) {
markdown += `\n\n<hr />\n\n`;
}
});
// create an identifier within the comment when searching comments
// in the future
const commentIdentifier = '<!-- generated by lighthouse-check -->';
markdown += commentIdentifier;
// establish existing comment
let existingComment;
// if we aren't saving old comments
if (!prCommentSaveOld) {
// get existing comments to see if we've already posted scores
const existingCommentsResult = await fetch(prCommentUrl, {
method: 'get',
headers: {
'content-type': 'application/json',
authorization: `token ${prCommentAccessToken}`
}
});
const existingCommentsJsonResult = await existingCommentsResult.json();
if (
Array.isArray(existingCommentsJsonResult) &&
existingCommentsJsonResult.length
) {
existingComment = existingCommentsJsonResult.find(current =>
current.body.includes(commentIdentifier)
);
}
}
// create or update the comment with scores
const shouldUpdate = existingComment && existingComment.id;
const url = !shouldUpdate
? prCommentUrl
: `${prCommentUrl}/${existingComment.id}`;
const result = await fetch(url, {
method: !shouldUpdate ? 'post' : 'put',
body: JSON.stringify({
...(shouldUpdate
? {}
: {
event: 'COMMENT'
}),
body: markdown
}),
headers: {
'content-type': 'application/json',
authorization: `token ${prCommentAccessToken}`
}
});
const jsonResult = await result.json();
if (!jsonResult.id) {
throw new LighthouseCheckError(
jsonResult.message || 'something went wrong',
{
code: ERROR_UNEXPECTED_RESPONSE,
data: jsonResult
}
);
}
} catch (error) {
if (verbose) {
console.log(`${NAME}:`, error);
}
// we still need to kill the process
throw error;
}
};