Skip to content

Commit 263f3cb

Browse files
committed
refactor(ts): refactor scanDiff into TS
1 parent e3ee26a commit 263f3cb

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

src/proxy/processors/push-action/scanDiff.js renamed to src/proxy/processors/push-action/scanDiff.ts

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
1-
const Step = require('../../actions').Step;
2-
const config = require('../../../config');
3-
const parseDiff = require('parse-diff')
1+
import { Action, Step } from '../../actions';
2+
import { getCommitConfig, getPrivateOrganizations } from '../../../config';
3+
import parseDiff, { AddChange, Change, File } from 'parse-diff';
44

5-
const commitConfig = config.getCommitConfig();
6-
const privateOrganizations = config.getPrivateOrganizations();
5+
const commitConfig = getCommitConfig();
6+
const privateOrganizations = getPrivateOrganizations();
77

88
const BLOCK_TYPE = {
99
LITERAL: 'Offending Literal',
1010
PATTERN: 'Offending Pattern',
1111
PROVIDER: 'PROVIDER'
1212
}
1313

14+
type CombinedMatch = {
15+
type: string;
16+
match: RegExp;
17+
}
18+
19+
type RawMatch = {
20+
type: string;
21+
literal: string;
22+
file?: string;
23+
lines: number[];
24+
content: string;
25+
}
26+
27+
type Match = {
28+
type: string;
29+
literal: string;
30+
file?: string;
31+
lines: string;
32+
content: string;
33+
}
1434

15-
const getDiffViolations = (diff, organization) => {
35+
const getDiffViolations = (diff: string, organization: string): Match[] | string | null => {
1636
// Commit diff is empty, i.e. '', null or undefined
1737
if (!diff) {
1838
console.log('No commit diff...');
@@ -28,7 +48,6 @@ const getDiffViolations = (diff, organization) => {
2848
const parsedDiff = parseDiff(diff);
2949
const combinedMatches = combineMatches(organization);
3050

31-
3251
const res = collectMatches(parsedDiff, combinedMatches);
3352
// Diff matches configured block pattern(s)
3453
if (res.length > 0) {
@@ -40,16 +59,15 @@ const getDiffViolations = (diff, organization) => {
4059
return null;
4160
};
4261

43-
const combineMatches = (organization) => {
44-
62+
const combineMatches = (organization: string) => {
4563
// Configured blocked literals
46-
const blockedLiterals = commitConfig.diff.block.literals;
64+
const blockedLiterals: string[] = commitConfig.diff.block.literals;
4765

4866
// Configured blocked patterns
49-
const blockedPatterns = commitConfig.diff.block.patterns;
67+
const blockedPatterns: string[] = commitConfig.diff.block.patterns;
5068

5169
// Configured blocked providers
52-
const blockedProviders = organization && privateOrganizations.includes(organization) ? [] :
70+
const blockedProviders: [string, string][] = organization && privateOrganizations.includes(organization) ? [] :
5371
Object.entries(commitConfig.diff.block.providers);
5472

5573
// Combine all matches (literals, paterns)
@@ -70,15 +88,19 @@ const combineMatches = (organization) => {
7088
return combinedMatches;
7189
}
7290

73-
const collectMatches = (parsedDiff, combinedMatches) => {
74-
const allMatches = {};
91+
const collectMatches = (
92+
parsedDiff: File[],
93+
combinedMatches: CombinedMatch[]
94+
): Match[] => {
95+
const allMatches: Record<string, RawMatch> = {};
7596
parsedDiff.forEach(file => {
7697
const fileName = file.to || file.from;
7798
console.log("CHANGE", file.chunks)
7899

79100
file.chunks.forEach(chunk => {
80101
chunk.changes.forEach(change => {
81-
if (change.add) {
102+
console.log("CHANGE", change)
103+
if (change.type === 'add') {
82104
// store line number
83105
const lineNumber = change.ln;
84106
// Iterate through each match types - literal, patterns, providers
@@ -117,11 +139,10 @@ const collectMatches = (parsedDiff, combinedMatches) => {
117139
lines: match.lines.join(',') // join the line numbers into a comma-separated string
118140
}))
119141

120-
console.log("RESULT", result)
121142
return result;
122143
}
123144

124-
const formatMatches = (matches) => {
145+
const formatMatches = (matches: Match[]) => {
125146
return matches.map((match, index) => {
126147
return `---------------------------------- #${index + 1} ${match.type} ------------------------------
127148
Policy Exception Type: ${match.type}
@@ -131,7 +152,7 @@ const formatMatches = (matches) => {
131152
});
132153
}
133154

134-
const exec = async (req, action) => {
155+
const exec = async (req: any, action: Action): Promise<Action> => {
135156
const step = new Step('scanDiff');
136157

137158
const { steps, commitFrom, commitTo } = action;
@@ -158,7 +179,6 @@ const exec = async (req, action) => {
158179
errorMsg.join('\n')
159180
);
160181

161-
162182
action.addStep(step);
163183
return action;
164184
}
@@ -168,4 +188,5 @@ const exec = async (req, action) => {
168188
};
169189

170190
exec.displayName = 'scanDiff.exec';
171-
exports.exec = exec;
191+
192+
export { exec };

0 commit comments

Comments
 (0)