forked from spartez/eslint-formatter-bitbucket-reports
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
224 lines (193 loc) · 5.85 KB
/
index.ts
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import path from "path";
import stylish from "eslint-formatter-stylish";
import got, { Response } from "got";
import { CLIEngine } from "@typescript-eslint/utils/dist/ts-eslint";
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.extend({
prefixUrl: `https://api.bitbucket.org/2.0`,
responseType: "json" as const,
headers: {
Authorization: `Bearer ${BITBUCKET_API_AUTH}`,
"Content-Type": "application/json",
Accept: "application/json",
},
});
enum SEVERITIES {
MEDIUM = "MEDIUM",
HIGH = "HIGH",
}
type BBAnnotationItem = {
external_id: string;
line: number;
path: string;
summary: string;
annotation_type: "BUG";
severity: SEVERITIES;
};
type BBReportData = {
title: string;
reporter: string;
report_type: string;
details: string;
result: "FAILED" | "PASSED";
};
function generateReport(results: CLIEngine.LintResult[]): BBReportData {
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: CLIEngine.LintResult[],
reportId: string
): BBAnnotationItem[] {
const result = results.reduce((acc, result) => {
const relativePath = path.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}`;
const ruleID = ruleId || "";
// summary max length is 450
const messageSize = 440 - ruleID.substring(100).length;
const summary = `${message.substring(messageSize)} (${ruleID})`;
console.log(summary, summary.length);
const result: BBAnnotationItem = {
external_id,
line,
path: relativePath,
summary,
annotation_type: "BUG",
severity: severity === 1 ? SEVERITIES.MEDIUM : SEVERITIES.HIGH,
};
return result;
}),
];
}, [] as BBAnnotationItem[]);
return result;
}
async function deleteReport(reportId: string) {
return httpClient.delete(
`repositories/${BITBUCKET_WORKSPACE}/${BITBUCKET_REPO_SLUG}/commit/${BITBUCKET_COMMIT}/reports/${reportId}`
);
}
async function createReport(reportId: string, reportData: BBReportData) {
return httpClient.put(
`repositories/${BITBUCKET_WORKSPACE}/${BITBUCKET_REPO_SLUG}/commit/${BITBUCKET_COMMIT}/reports/${reportId}`,
{
json: reportData,
responseType: "json",
}
);
}
async function createAnnotations(
reportId: string,
annotations: BBAnnotationItem[]
): Promise<Response<unknown>> {
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: CLIEngine.LintResult[]) {
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: any) {
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: any) {
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: any) {
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: string) {
const test = process.env[key];
if (!test) {
throw new Error(`Missing ENV var: [${key}]`);
}
return test;
}
module.exports = function (results: CLIEngine.LintResult[]) {
processResults(results);
// @ts-expect-error wrong 3rd party type
return stylish(results);
};