-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfilter.js
More file actions
139 lines (115 loc) · 3.81 KB
/
filter.js
File metadata and controls
139 lines (115 loc) · 3.81 KB
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
async function fetchPRFiles(octokit, repo, prNumber) {
try {
const { data: files } = await octokit.rest.pulls.listFiles({
...repo,
pull_number: prNumber,
});
return files;
} catch (error) {
console.error("Error fetching PR files:", error);
return [];
}
}
function parseChangedLines(prFiles) {
const changedFiles = {};
prFiles.forEach((file) => {
const changedLines = [];
// Skip files without patches (like binary files or deleted files)
if (!file.patch) {
changedFiles[file.filename] = changedLines;
return;
}
// Parse each hunk in the patch to find added/modified lines
const hunkRegex = /@@\s+-\d+(?:,\d+)?\s+\+(\d+)(?:,(\d+))?\s+@@/g;
let match;
while ((match = hunkRegex.exec(file.patch)) !== null) {
const start = parseInt(match[1]);
const count = match[2] ? parseInt(match[2]) : 1;
// Only include lines that were added/modified (count > 0)
if (count > 0) {
for (let i = start; i < start + count; i++) {
changedLines.push(i);
}
}
}
// Remove duplicates and sort
changedFiles[file.filename] = [...new Set(changedLines)].sort(
(a, b) => a - b
);
});
return changedFiles;
}
function filterResultsByChangedFiles(results, changedFiles) {
console.log("Filtering results for diff-aware reporting...");
const filteredResults = JSON.parse(JSON.stringify(results)); // Deep clone
const filteredQueries = [];
// Filter findings based on changed files and lines
for (const query of results.queries) {
const filteredFiles = [];
for (const file of query.files) {
const fileName = file.file_name;
const fileLine = file.line;
// Check if this file was changed in the PR
if (changedFiles.hasOwnProperty(fileName)) {
const changedLines = changedFiles[fileName];
// If no specific lines changed (e.g., new file), include all findings
// Otherwise, only include findings on changed lines
if (changedLines.length === 0 || changedLines.includes(fileLine)) {
filteredFiles.push(file);
}
}
}
// If this query has findings in changed files/lines, include it
if (filteredFiles.length > 0) {
const filteredQuery = { ...query, files: filteredFiles };
filteredQueries.push(filteredQuery);
}
}
// Update the filtered results
filteredResults.queries = filteredQueries;
// Recalculate counters based on filtered findings
const newSeverityCounters = {};
let totalCounter = 0;
// Initialize severity counters
for (const severity of [
"CRITICAL",
"HIGH",
"MEDIUM",
"LOW",
"INFO",
"TRACE",
]) {
newSeverityCounters[severity] = 0;
}
// Count findings by severity
filteredQueries.forEach((query) => {
const severity = query.severity.toUpperCase();
const findingCount = query.files.length;
if (newSeverityCounters.hasOwnProperty(severity)) {
newSeverityCounters[severity] += findingCount;
}
totalCounter += findingCount;
});
filteredResults.severity_counters = newSeverityCounters;
filteredResults.total_counter = totalCounter;
console.log(
`Filtered results: ${totalCounter} findings in changed files (originally ${results.total_counter})`
);
return filteredResults;
}
async function applyDiffAwareFiltering(parsedResults, octokit, repo, prNumber) {
console.log("Diff-aware reporting enabled for PR #" + prNumber);
const prFiles = await fetchPRFiles(octokit, repo, prNumber);
if (prFiles.length > 0) {
const changedFiles = parseChangedLines(prFiles);
return filterResultsByChangedFiles(parsedResults, changedFiles);
} else {
console.log(
"No PR files found or error fetching files, using original results"
);
return parsedResults;
}
}
module.exports = {
applyDiffAwareFiltering,
};