-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
132 lines (115 loc) · 4.13 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
const path = require('path');
const stylish = require('eslint/lib/cli-engine/formatters/stylish');
const got = require('got');
const { BITBUCKET_WORKSPACE, BITBUCKET_REPO_SLUG, BITBUCKET_COMMIT, BITBUCKET_API_AUTH } = process.env;
const BITBUCKET_API_HOST = 'api.bitbucket.org';
const MAX_ANNOTATIONS_PER_REQUEST = 100;
const httpClientConfig = BITBUCKET_API_AUTH ? {
prefixUrl: `https://${BITBUCKET_API_HOST}`,
responseType: 'json',
headers: {
'Authorization': BITBUCKET_API_AUTH
}
} : {
prefixUrl: `http://localhost:29418`,
responseType: 'json',
headers: {
'Host': 'api.bitbucket.org'
},
hooks: {
beforeRequest: [
options => {
options.path = `http://${BITBUCKET_API_HOST}${options.url.pathname}`;
}
]
}
}
const httpClient = got.extend(httpClientConfig);
const SEVERITIES = {
1: 'MEDIUM',
2: 'HIGH'
};
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 report',
reporter: 'ESLint',
report_type: 'TEST',
details,
result
};
}
function generateAnnotations(results, reportId) {
return 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}`;
return {
external_id,
line,
path: relativePath,
summary: `${message} (${ruleId})`,
annotation_type: 'BUG',
severity: SEVERITIES[severity]
};
})];
}, []);
}
async function deleteReport(reportId) {
return httpClient.delete(`2.0/repositories/${BITBUCKET_WORKSPACE}/${BITBUCKET_REPO_SLUG}/commit/${BITBUCKET_COMMIT}/reports/${reportId}`);
}
async function createReport(reportId, report) {
return httpClient.put(`2.0/repositories/${BITBUCKET_WORKSPACE}/${BITBUCKET_REPO_SLUG}/commit/${BITBUCKET_COMMIT}/reports/${reportId}`, {
json: report,
responseType: 'json'
});
}
async function createAnnotations(reportId, annotations) {
const chunk = annotations.slice(0, MAX_ANNOTATIONS_PER_REQUEST);
const response = await httpClient.post(`2.0/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 {
await deleteReport(reportId);
console.log('Previous report deleted');
await createReport(reportId, report);
console.log('New report created');
await createAnnotations(reportId, annotations);
console.log('Annotations added');
} catch (error) {
if (error.request) {
console.log(error.request.options);
}
if (error.response) {
console.error(error.message, error.response.body)
} else {
console.error(error);
}
}
}
module.exports = function(results) {
processResults(results);
return stylish(results);
};