-
Notifications
You must be signed in to change notification settings - Fork 141
/
api.js
87 lines (80 loc) · 2.78 KB
/
api.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
const core = require("@actions/core");
const { name: actionName } = require("../../package.json");
const request = require("../utils/request");
const { capitalizeFirstLetter } = require("../utils/string");
/** @typedef {import('./context').GithubContext} GithubContext */
/** @typedef {import('../utils/lint-result').LintResult} LintResult */
/**
* Creates a new check on GitHub which annotates the relevant commit with linting errors
* @param {string} linterName - Name of the linter for which a check should be created
* @param {string} sha - SHA of the commit which should be annotated
* @param {GithubContext} context - Information about the GitHub repository and
* action trigger event
* @param {LintResult} lintResult - Parsed lint result
* @param {boolean} neutralCheckOnWarning - Whether the check run should conclude as neutral if
* there are only warnings
* @param {string} summary - Summary for the GitHub check
*/
async function createCheck(linterName, sha, context, lintResult, neutralCheckOnWarning, summary) {
let annotations = [];
for (const level of ["warning", "error"]) {
annotations = [
...annotations,
...lintResult[level].map((result) => ({
path: result.path,
start_line: result.firstLine,
end_line: result.lastLine,
annotation_level: level === "warning" ? "warning" : "failure",
message: result.message,
})),
];
}
// Only use the first 50 annotations (limit for a single API request)
if (annotations.length > 50) {
core.info(
`There are more than 50 errors/warnings from ${linterName}. Annotations are created for the first 50 issues only.`,
);
annotations = annotations.slice(0, 50);
}
let conclusion;
if (lintResult.isSuccess) {
if (annotations.length > 0 && neutralCheckOnWarning) {
conclusion = "neutral";
} else {
conclusion = "success";
}
} else {
conclusion = "failure";
}
const body = {
name: linterName,
head_sha: sha,
conclusion,
output: {
title: capitalizeFirstLetter(summary),
summary: `${linterName} found ${summary}`,
annotations,
},
};
try {
core.info(
`Creating GitHub check with ${conclusion} conclusion and ${annotations.length} annotations for ${linterName}…`,
);
await request(`${process.env.GITHUB_API_URL}/repos/${context.repository.repoName}/check-runs`, {
method: "POST",
headers: {
"Content-Type": "application/json",
// "Accept" header is required to access Checks API during preview period
Accept: "application/vnd.github.antiope-preview+json",
Authorization: `Bearer ${context.token}`,
"User-Agent": actionName,
},
body,
});
core.info(`${linterName} check created successfully`);
} catch (err) {
core.error(err);
throw new Error(`Error trying to create GitHub check for ${linterName}: ${err.message}`);
}
}
module.exports = { createCheck };