forked from spartez/eslint-formatter-bitbucket-reports
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
163 lines (163 loc) · 6 KB
/
index.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
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
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const eslint_formatter_stylish_1 = __importDefault(require("eslint-formatter-stylish"));
const got_1 = __importDefault(require("got"));
const BITBUCKET_WORKSPACE = getEnv("BITBUCKET_WORKSPACE"); //"curalie";
const BITBUCKET_REPO_SLUG = getEnv("BITBUCKET_REPO_SLUG"); //"tnp-chameleon";
const BITBUCKET_COMMIT = getEnv("BITBUCKET_COMMIT"); //"919db18";
const BITBUCKET_API_AUTH = getEnv("BITBUCKET_API_AUTH");
const MAX_ANNOTATIONS_PER_REQUEST = 100;
const MAX_TOTAL_ANNOTATIONS = 1000;
const httpClient = got_1.default.extend({
prefixUrl: `https://api.bitbucket.org/2.0`,
responseType: "json",
headers: {
Authorization: `Bearer ${BITBUCKET_API_AUTH}`,
"Content-Type": "application/json",
Accept: "application/json",
},
});
var SEVERITIES;
(function (SEVERITIES) {
SEVERITIES["MEDIUM"] = "MEDIUM";
SEVERITIES["HIGH"] = "HIGH";
})(SEVERITIES || (SEVERITIES = {}));
function generateReport(results) {
const summary = results.reduce((acc, current) => {
acc.errorCount += current.errorCount;
acc.warningCount += current.warningCount;
return acc;
}, { errorCount: 0, warningCount: 0 });
const { errorCount, warningCount } = summary;
const problemCount = errorCount + warningCount;
const details = `${problemCount} problem${problemCount !== 1 ? "s" : ""} (${errorCount} error${errorCount !== 1 ? "s" : ""}, ${warningCount} warning${warningCount !== 1 ? "s" : ""})`;
const result = errorCount > 0 ? "FAILED" : "PASSED";
return {
title: "ESLint Bitbucket reporter",
reporter: "ESLint",
report_type: "TEST",
details,
result,
};
}
function generateAnnotations(results, reportId) {
const result = results.reduce((acc, result) => {
const relativePath = path_1.default.relative(process.cwd(), result.filePath);
return [
...acc,
...result.messages.map((messageObject, i) => {
const { line, message, severity, ruleId } = messageObject;
const external_id = `${reportId}-${relativePath}-${line}-${ruleId}-${i}`;
// summary max length is 450
const messageSize = 440 - (ruleId ? ruleId.length : 1);
const summary = `${message.substring(messageSize)} (${ruleId})`.substring(440);
console.log(summary, summary.length);
const result = {
external_id,
line,
path: relativePath,
summary,
annotation_type: "BUG",
severity: severity === 1 ? SEVERITIES.MEDIUM : SEVERITIES.HIGH,
};
return result;
}),
];
}, []);
return result;
}
async function deleteReport(reportId) {
return httpClient.delete(`repositories/${BITBUCKET_WORKSPACE}/${BITBUCKET_REPO_SLUG}/commit/${BITBUCKET_COMMIT}/reports/${reportId}`);
}
async function createReport(reportId, reportData) {
return httpClient.put(`repositories/${BITBUCKET_WORKSPACE}/${BITBUCKET_REPO_SLUG}/commit/${BITBUCKET_COMMIT}/reports/${reportId}`, {
json: reportData,
responseType: "json",
});
}
async function createAnnotations(reportId, annotations) {
const chunk = annotations.slice(0, MAX_ANNOTATIONS_PER_REQUEST);
const response = await httpClient.post(`repositories/${BITBUCKET_WORKSPACE}/${BITBUCKET_REPO_SLUG}/commit/${BITBUCKET_COMMIT}/reports/${reportId}/annotations`, {
json: chunk,
responseType: "json",
});
if (annotations.length > MAX_ANNOTATIONS_PER_REQUEST) {
return createAnnotations(reportId, annotations.slice(MAX_ANNOTATIONS_PER_REQUEST));
}
return response;
}
async function processResults(results) {
const reportId = `eslint-${BITBUCKET_COMMIT}`;
const report = generateReport(results);
const annotations = generateAnnotations(results, reportId);
try {
console.log("✍🏼 Deleting previous report...");
await deleteReport(reportId);
console.log("✅ Previous report deleted!");
}
catch (error) {
console.log("❌ Report deletion failed!");
if (error.response) {
console.error(error.message, error.response.body);
}
else {
console.error(error);
}
throw error;
}
try {
console.log("✍🏼 Creating a new report...");
await createReport(reportId, report);
console.log("✅ New report created");
}
catch (error) {
console.log("❌ Report creation failed");
if (error.response) {
console.error(error.message, error.response.body);
}
else {
console.error(error);
}
throw error;
}
try {
if (annotations.length > 0) {
console.log("✍🏼 Adding new annotations...");
await createAnnotations(reportId, annotations.slice(0, MAX_TOTAL_ANNOTATIONS));
console.log("✅ Annotations added!");
}
else {
console.log("⚠️ no annotations found!");
}
}
catch (error) {
console.log("❌ Annotations adding failed!");
// if (error.request) {
// console.log(error.request.options);
// }
if (error.response) {
console.error(error.message, error.response.body);
}
else {
console.error(error);
}
throw error;
}
}
function getEnv(key) {
const test = process.env[key];
if (!test) {
throw new Error(`Missing ENV var: [${key}]`);
}
return test;
}
module.exports = function (results) {
processResults(results);
// @ts-expect-error wrong 3rd party type
return (0, eslint_formatter_stylish_1.default)(results);
};
//# sourceMappingURL=index.js.map