-
Notifications
You must be signed in to change notification settings - Fork 86
/
sarif-with-rules.js
177 lines (143 loc) · 5.25 KB
/
sarif-with-rules.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* @fileoverview SARIF v2.0 formatter
* @author Microsoft
*/
"use strict";
const lodash = require("lodash");
const fs = require("fs");
const utf8 = require("utf8");
const jschardet = require("jschardet");
//------------------------------------------------------------------------------
// Helper Functions
//------------------------------------------------------------------------------
/**
* Returns the severity of warning or error
* @param {Object} message message object to examine
* @returns {string} severity level
* @private
*/
function getResultLevel(message) {
if (message.fatal || message.severity === 2) {
return "error";
}
return "warning";
}
//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
module.exports = function (results, data) {
const rulesMetdata = lodash.get(data, "rulesMetdata", null);
const sarifLog = {
version: "2.0.0",
$schema: "http://json.schemastore.org/sarif-2.0.0",
runs: [
{
files: [],
tool: {
name: "ESLint"
}
}
]
};
const sarifFiles = {};
const sarifRules = {};
const sarifResults = [];
const embedFileContents = process.env.SARIF_ESLINT_EMBED === "true";
results.forEach(result => {
if (typeof sarifFiles[result.filePath] === "undefined") {
let contentsUtf8;
// Create a new entry in the files dictionary.
sarifFiles[result.filePath] = {
fileLocation: {
uri: result.filePath
}
};
if (embedFileContents) {
try {
// Try to get the file contents and encoding.
const contents = fs.readFileSync(result.filePath);
const encoding = jschardet.detect(contents);
// Encoding will be null if it could not be determined.
if (encoding) {
// Convert the content bytes to a UTF-8 string.
contentsUtf8 = utf8.encode(contents.toString(encoding.encoding));
sarifFiles[result.filePath].contents = {
text: contentsUtf8
};
sarifFiles[result.filePath].encoding = encoding.encoding;
}
}
catch (err) {
console.log(err);
}
}
}
const messages = result.messages;
messages.forEach(message => {
const sarifResult = {
level: getResultLevel(message),
message: {
text: message.message
},
locations: [
{
physicalLocation: {
fileLocation: {
uri: result.filePath
}
}
}
]
};
if (message.ruleId) {
sarifResult.ruleId = message.ruleId;
if (rulesMetdata && typeof sarifRules[message.ruleId] === "undefined") {
const meta = rulesMetdata[message.ruleId];
// An unknown ruleId will return null. This check prevents unit test failure.
if (meta) {
// Create a new entry in the rules dictionary.
sarifRules[message.ruleId] = {
id: message.ruleId,
shortDescription: {
text: meta.docs.description
},
helpUri: meta.docs.url,
tags: {
category: meta.docs.category
}
};
}
}
}
if (message.line > 0 && message.column > 0) {
sarifResult.locations[0].physicalLocation.region = {
startLine: message.line,
startColumn: message.column
};
}
if (message.source) {
// Create an empty region if we don't already have one from the line / column block above.
sarifResult.locations[0].physicalLocation.region = sarifResult.locations[0].physicalLocation.region || {};
sarifResult.locations[0].physicalLocation.region.snippet = {
text: message.source
};
}
sarifResults.push(sarifResult);
});
});
Object.keys(sarifFiles).forEach(function (path) {
sarifLog.runs[0].files.push(sarifFiles[path]);
});
if (sarifResults.length > 0) {
sarifLog.runs[0].results = sarifResults;
}
if (Object.keys(sarifRules).length > 0) {
sarifLog.runs[0].resources = {
rules: sarifRules
};
}
return JSON.stringify(sarifLog,
null, // replacer function
2 // # of spaces for indents
);
};