Skip to content

Commit 027a459

Browse files
committed
refactor: corrected the way filepaths are geting extracted from diff
1 parent 8b257a2 commit 027a459

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed

src/proxy/processors/push-action/checkSensitiveData.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,39 @@ const parseFile = async (filePath) => {
108108
}
109109
};
110110
// Async exec function to handle actions
111+
// Function to parse file paths from git diff content
112+
const extractFilePathsFromDiff = (diffContent) => {
113+
const filePaths = [];
114+
const lines = diffContent.split('\n');
115+
116+
lines.forEach(line => {
117+
const match = line.match(/^diff --git a\/(.+?) b\/(.+?)$/);
118+
if (match) {
119+
filePaths.push(match[1]); // Extract the file path from "a/" in the diff line
120+
}
121+
});
122+
123+
return filePaths;
124+
};
125+
111126
const exec = async (req, action) => {
112-
// getDiffExec(req, action); // Call to getDiffExec if necessary
113127
const diffStep = action.steps.find((s) => s.stepName === 'diff');
128+
114129
if (diffStep && diffStep.content) {
115130
console.log('Diff content:', diffStep.content);
116-
const filePaths = diffStep.content.filePaths || [];
131+
132+
// Use the parsing function to get file paths
133+
const filePaths = extractFilePathsFromDiff(diffStep.content);
134+
117135
if (filePaths.length > 0) {
118136
// Check for sensitive data in all files
119137
const sensitiveDataFound = await Promise.all(filePaths.map(parseFile));
120-
const anySensitiveDataDetected = sensitiveDataFound.some(found => found); // Check if any file reported sensitive data
138+
const anySensitiveDataDetected = sensitiveDataFound.some(found => found);
139+
121140
if (anySensitiveDataDetected) {
122-
action.pushBlocked = true; // Block the push
123-
action.error = true; // Set error flag
124-
action.errorMessage = 'Your push has been blocked due to sensitive data detection.'; // Set error message
141+
action.pushBlocked = true;
142+
action.error = true;
143+
action.errorMessage = 'Your push has been blocked due to sensitive data detection.';
125144
console.log(action.errorMessage);
126145
}
127146
} else {
@@ -130,7 +149,11 @@ const exec = async (req, action) => {
130149
} else {
131150
console.log('No diff content available.');
132151
}
152+
133153
return action; // Returning action for testing purposes
134154
};
155+
156+
157+
135158
exec.displayName = 'logFileChanges.exec';
136159
exports.exec = exec;

test/CheckSensitive.test.js

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const path = require('path');
1+
// const path = require('path');
22
const { exec } = require('../src/proxy/processors/push-action/checkSensitiveData.js'); // Adjust path as necessary
33
const sinon = require('sinon');
44

@@ -13,13 +13,16 @@ describe('Sensitive Data Detection', () => {
1313
logStub.restore(); // Restore console.log after each test
1414
});
1515

16+
const createDiffContent = (filePaths) => {
17+
// Format file paths in diff format
18+
return filePaths.map(filePath => `diff --git a/${filePath} b/${filePath}`).join('\n');
19+
};
20+
1621
it('should detect sensitive data in CSV file and block execution', async () => {
1722
const action = {
1823
steps: [{
1924
stepName: 'diff',
20-
content: {
21-
filePaths: [path.join(__dirname, 'test_data/sensitive_data.csv')] // Ensure this path is correct
22-
}
25+
content: createDiffContent(['test/test_data/sensitive_data.csv']) // Ensure this path is correct
2326
}]
2427
};
2528
await exec(null, action);
@@ -30,9 +33,7 @@ describe('Sensitive Data Detection', () => {
3033
const action = {
3134
steps: [{
3235
stepName: 'diff',
33-
content: {
34-
filePaths: [path.join(__dirname, 'test_data/sensitive_data2.xlsx')] // Ensure this path is correct
35-
}
36+
content: createDiffContent(['test/test_data/sensitive_data2.xlsx']) // Ensure this path is correct
3637
}]
3738
};
3839
await exec(null, action);
@@ -43,9 +44,7 @@ describe('Sensitive Data Detection', () => {
4344
const action = {
4445
steps: [{
4546
stepName: 'diff',
46-
content: {
47-
filePaths: [path.join(__dirname, 'test_data/sensitive_data3.log')] // Ensure this path is correct
48-
}
47+
content: createDiffContent(['test/test_data/sensitive_data3.log']) // Ensure this path is correct
4948
}]
5049
};
5150
await exec(null, action);
@@ -56,9 +55,7 @@ describe('Sensitive Data Detection', () => {
5655
const action = {
5756
steps: [{
5857
stepName: 'diff',
59-
content: {
60-
filePaths: [path.join(__dirname, 'test_data/sensitive_data4.json')] // Ensure this path is correct
61-
}
58+
content: createDiffContent(['test/test_data/sensitive_data4.json']) // Ensure this path is correct
6259
}]
6360
};
6461
await exec(null, action);
@@ -69,9 +66,7 @@ describe('Sensitive Data Detection', () => {
6966
const action = {
7067
steps: [{
7168
stepName: 'diff',
72-
content: {
73-
filePaths: [path.join(__dirname, 'test_data/no_sensitive_data.txt')] // Ensure this path is correct
74-
}
69+
content: createDiffContent(['test_data/no_sensitive_data.txt']) // Ensure this path is correct
7570
}]
7671
};
7772
await exec(null, action);
@@ -82,9 +77,7 @@ describe('Sensitive Data Detection', () => {
8277
const action = {
8378
steps: [{
8479
stepName: 'diff',
85-
content: {
86-
filePaths: [path.join(__dirname, 'test_data/empty_file.txt')] // Ensure this path is correct
87-
}
80+
content: createDiffContent(['test_data/empty_file.txt']) // Ensure this path is correct
8881
}]
8982
};
9083
await exec(null, action);
@@ -95,9 +88,7 @@ describe('Sensitive Data Detection', () => {
9588
const action = {
9689
steps: [{
9790
stepName: 'diff',
98-
content: {
99-
filePaths: [path.join(__dirname, 'test_data/non_existent_file.txt')] // Ensure this path is correct
100-
}
91+
content: createDiffContent(['test_data/non_existent_file.txt']) // Ensure this path is correct
10192
}]
10293
};
10394
try {

0 commit comments

Comments
 (0)