Skip to content

Commit

Permalink
Merge pull request #18 from veracode/autoRewritePath
Browse files Browse the repository at this point in the history
Auto rewrite path
  • Loading branch information
julz0815 authored Dec 12, 2024
2 parents 14b6429 + d744d69 commit 6429a56
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ Archive.zip
package-lock.json
.DS_Store
Archive.zip
.DS_Store

48 changes: 48 additions & 0 deletions pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const { request } = require('@octokit/request');
const label = require('./label');
const addVeracodeIssue = require('./issue').addVeracodeIssue;
const addVeracodeIssueComment = require('./issue_comment').addVeracodeIssueComment;
const fs = require('fs');
const path = require('path');

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

// new autorewrite file path
function searchFile(dir, filename) {
//console.log('Inside search: Directory: '+dir+' - Filename: '+filename)
let result = null;
const files = fs.readdirSync(dir);

for (const file of files) {
if (file === '.git') continue;
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);

if (stat.isDirectory()) {
result = searchFile(fullPath, filename);
if (result) break;
} else if (file === filename) {
console.log('File found: '+fullPath)
result = fullPath;
break;
}
}
//console.log('Result: '+result)
return result;
}

// Search for the file starting from the current directory
var filename = flaw.files.source_file.file
const currentDir = process.cwd();
console.log('Current Directory: ' + currentDir);
console.log('Filename: ' + filename);
const foundFilePath = searchFile(currentDir, path.basename(filename));

if (foundFilePath) {
//filepath = foundFilePath;
filepath = foundFilePath.replace(process.cwd(), '')
console.log('Adjusted Filepath: ' + filepath);
} else {
filepath = filename;
console.log('File not found in the current directory or its subdirectories.');
}


/* old rewrite path
//rewrite path
function replacePath (rewrite, path){
Expand Down Expand Up @@ -253,6 +298,9 @@ async function processPipelineFlaws(options, flawData) {
console.log('Filepath:'+filepath);
}
old rewrite path */


linestart = eval(flaw.files.source_file.line-5)
linened = eval(flaw.files.source_file.line+5)

Expand Down
50 changes: 50 additions & 0 deletions policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const label = require('./label');
const addVeracodeIssue = require('./issue').addVeracodeIssue;
const addVeracodeIssueComment = require('./issue_comment').addVeracodeIssueComment;
const core = require('@actions/core');
const fs = require('fs');
const path = require('path');

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

// new auto rewrite path
// new autorewrite file path
function searchFile(dir, filename) {
//console.log('Inside search: Directory: '+dir+' - Filename: '+filename)
let result = null;
const files = fs.readdirSync(dir);

for (const file of files) {
if (file === '.git') continue;
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);

if (stat.isDirectory()) {
result = searchFile(fullPath, filename);
if (result) break;
} else if (file === filename) {
console.log('File found: '+fullPath)
result = fullPath;
break;
}
}
//console.log('Result: '+result)
return result;
}

// Search for the file starting from the current directory
var filename = flaw.finding_details.file_path
const currentDir = process.cwd();
console.log('Current Directory: ' + currentDir);
console.log('Filename: ' + filename);
const foundFilePath = searchFile(currentDir, path.basename(filename));

if (foundFilePath) {
//filepath = foundFilePath;
filepath = foundFilePath.replace(process.cwd(), '')
console.log('Adjusted Filepath: ' + filepath);
} else {
filepath = filename;
console.log('File not found in the current directory or its subdirectories.');
}





/* old rewrite path
//rewrite path
function replacePath (rewrite, path){
replaceValues = rewrite.split(":")
Expand Down Expand Up @@ -236,6 +284,8 @@ async function processPolicyFlaws(options, flawData) {
filepath = filename
}
old rewrite path */

linestart = eval(flaw.finding_details.file_line_number-5)
linened = eval(flaw.finding_details.file_line_number+5)

Expand Down

0 comments on commit 6429a56

Please sign in to comment.