Skip to content

Commit 6429a56

Browse files
authored
Merge pull request #18 from veracode/autoRewritePath
Auto rewrite path
2 parents 14b6429 + d744d69 commit 6429a56

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ Archive.zip
99
package-lock.json
1010
.DS_Store
1111
Archive.zip
12-
.DS_Store
12+

pipeline.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const { request } = require('@octokit/request');
66
const label = require('./label');
77
const addVeracodeIssue = require('./issue').addVeracodeIssue;
88
const addVeracodeIssueComment = require('./issue_comment').addVeracodeIssueComment;
9+
const fs = require('fs');
10+
const path = require('path');
911

1012
/* Map of files that contain flaws
1113
* each entry is a struct of {CWE, line_number}
@@ -219,6 +221,49 @@ async function processPipelineFlaws(options, flawData) {
219221
continue;
220222
}
221223

224+
// new autorewrite file path
225+
function searchFile(dir, filename) {
226+
//console.log('Inside search: Directory: '+dir+' - Filename: '+filename)
227+
let result = null;
228+
const files = fs.readdirSync(dir);
229+
230+
for (const file of files) {
231+
if (file === '.git') continue;
232+
const fullPath = path.join(dir, file);
233+
const stat = fs.statSync(fullPath);
234+
235+
if (stat.isDirectory()) {
236+
result = searchFile(fullPath, filename);
237+
if (result) break;
238+
} else if (file === filename) {
239+
console.log('File found: '+fullPath)
240+
result = fullPath;
241+
break;
242+
}
243+
}
244+
//console.log('Result: '+result)
245+
return result;
246+
}
247+
248+
// Search for the file starting from the current directory
249+
var filename = flaw.files.source_file.file
250+
const currentDir = process.cwd();
251+
console.log('Current Directory: ' + currentDir);
252+
console.log('Filename: ' + filename);
253+
const foundFilePath = searchFile(currentDir, path.basename(filename));
254+
255+
if (foundFilePath) {
256+
//filepath = foundFilePath;
257+
filepath = foundFilePath.replace(process.cwd(), '')
258+
console.log('Adjusted Filepath: ' + filepath);
259+
} else {
260+
filepath = filename;
261+
console.log('File not found in the current directory or its subdirectories.');
262+
}
263+
264+
265+
/* old rewrite path
266+
222267
223268
//rewrite path
224269
function replacePath (rewrite, path){
@@ -253,6 +298,9 @@ async function processPipelineFlaws(options, flawData) {
253298
console.log('Filepath:'+filepath);
254299
}
255300
301+
old rewrite path */
302+
303+
256304
linestart = eval(flaw.files.source_file.line-5)
257305
linened = eval(flaw.files.source_file.line+5)
258306

policy.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const label = require('./label');
77
const addVeracodeIssue = require('./issue').addVeracodeIssue;
88
const addVeracodeIssueComment = require('./issue_comment').addVeracodeIssueComment;
99
const core = require('@actions/core');
10+
const fs = require('fs');
11+
const path = require('path');
1012

1113
// sparse array, element = true if the flaw exists, undefined otherwise
1214
var existingFlaws = [];
@@ -190,6 +192,52 @@ async function processPolicyFlaws(options, flawData) {
190192
continue;
191193
}
192194

195+
// new auto rewrite path
196+
// new autorewrite file path
197+
function searchFile(dir, filename) {
198+
//console.log('Inside search: Directory: '+dir+' - Filename: '+filename)
199+
let result = null;
200+
const files = fs.readdirSync(dir);
201+
202+
for (const file of files) {
203+
if (file === '.git') continue;
204+
const fullPath = path.join(dir, file);
205+
const stat = fs.statSync(fullPath);
206+
207+
if (stat.isDirectory()) {
208+
result = searchFile(fullPath, filename);
209+
if (result) break;
210+
} else if (file === filename) {
211+
console.log('File found: '+fullPath)
212+
result = fullPath;
213+
break;
214+
}
215+
}
216+
//console.log('Result: '+result)
217+
return result;
218+
}
219+
220+
// Search for the file starting from the current directory
221+
var filename = flaw.finding_details.file_path
222+
const currentDir = process.cwd();
223+
console.log('Current Directory: ' + currentDir);
224+
console.log('Filename: ' + filename);
225+
const foundFilePath = searchFile(currentDir, path.basename(filename));
226+
227+
if (foundFilePath) {
228+
//filepath = foundFilePath;
229+
filepath = foundFilePath.replace(process.cwd(), '')
230+
console.log('Adjusted Filepath: ' + filepath);
231+
} else {
232+
filepath = filename;
233+
console.log('File not found in the current directory or its subdirectories.');
234+
}
235+
236+
237+
238+
239+
240+
/* old rewrite path
193241
//rewrite path
194242
function replacePath (rewrite, path){
195243
replaceValues = rewrite.split(":")
@@ -236,6 +284,8 @@ async function processPolicyFlaws(options, flawData) {
236284
filepath = filename
237285
}
238286
287+
old rewrite path */
288+
239289
linestart = eval(flaw.finding_details.file_line_number-5)
240290
linened = eval(flaw.finding_details.file_line_number+5)
241291

0 commit comments

Comments
 (0)