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' ;
4
4
5
- const commitConfig = config . getCommitConfig ( ) ;
6
- const privateOrganizations = config . getPrivateOrganizations ( ) ;
5
+ const commitConfig = getCommitConfig ( ) ;
6
+ const privateOrganizations = getPrivateOrganizations ( ) ;
7
7
8
8
const BLOCK_TYPE = {
9
9
LITERAL : 'Offending Literal' ,
10
10
PATTERN : 'Offending Pattern' ,
11
11
PROVIDER : 'PROVIDER'
12
12
}
13
13
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
+ }
14
34
15
- const getDiffViolations = ( diff , organization ) => {
35
+ const getDiffViolations = ( diff : string , organization : string ) : Match [ ] | string | null => {
16
36
// Commit diff is empty, i.e. '', null or undefined
17
37
if ( ! diff ) {
18
38
console . log ( 'No commit diff...' ) ;
@@ -28,7 +48,6 @@ const getDiffViolations = (diff, organization) => {
28
48
const parsedDiff = parseDiff ( diff ) ;
29
49
const combinedMatches = combineMatches ( organization ) ;
30
50
31
-
32
51
const res = collectMatches ( parsedDiff , combinedMatches ) ;
33
52
// Diff matches configured block pattern(s)
34
53
if ( res . length > 0 ) {
@@ -40,16 +59,15 @@ const getDiffViolations = (diff, organization) => {
40
59
return null ;
41
60
} ;
42
61
43
- const combineMatches = ( organization ) => {
44
-
62
+ const combineMatches = ( organization : string ) => {
45
63
// Configured blocked literals
46
- const blockedLiterals = commitConfig . diff . block . literals ;
64
+ const blockedLiterals : string [ ] = commitConfig . diff . block . literals ;
47
65
48
66
// Configured blocked patterns
49
- const blockedPatterns = commitConfig . diff . block . patterns ;
67
+ const blockedPatterns : string [ ] = commitConfig . diff . block . patterns ;
50
68
51
69
// Configured blocked providers
52
- const blockedProviders = organization && privateOrganizations . includes ( organization ) ? [ ] :
70
+ const blockedProviders : [ string , string ] [ ] = organization && privateOrganizations . includes ( organization ) ? [ ] :
53
71
Object . entries ( commitConfig . diff . block . providers ) ;
54
72
55
73
// Combine all matches (literals, paterns)
@@ -70,15 +88,19 @@ const combineMatches = (organization) => {
70
88
return combinedMatches ;
71
89
}
72
90
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 > = { } ;
75
96
parsedDiff . forEach ( file => {
76
97
const fileName = file . to || file . from ;
77
98
console . log ( "CHANGE" , file . chunks )
78
99
79
100
file . chunks . forEach ( chunk => {
80
101
chunk . changes . forEach ( change => {
81
- if ( change . add ) {
102
+ console . log ( "CHANGE" , change )
103
+ if ( change . type === 'add' ) {
82
104
// store line number
83
105
const lineNumber = change . ln ;
84
106
// Iterate through each match types - literal, patterns, providers
@@ -117,11 +139,10 @@ const collectMatches = (parsedDiff, combinedMatches) => {
117
139
lines : match . lines . join ( ',' ) // join the line numbers into a comma-separated string
118
140
} ) )
119
141
120
- console . log ( "RESULT" , result )
121
142
return result ;
122
143
}
123
144
124
- const formatMatches = ( matches ) => {
145
+ const formatMatches = ( matches : Match [ ] ) => {
125
146
return matches . map ( ( match , index ) => {
126
147
return `---------------------------------- #${ index + 1 } ${ match . type } ------------------------------
127
148
Policy Exception Type: ${ match . type }
@@ -131,7 +152,7 @@ const formatMatches = (matches) => {
131
152
} ) ;
132
153
}
133
154
134
- const exec = async ( req , action ) => {
155
+ const exec = async ( req : any , action : Action ) : Promise < Action > => {
135
156
const step = new Step ( 'scanDiff' ) ;
136
157
137
158
const { steps, commitFrom, commitTo } = action ;
@@ -158,7 +179,6 @@ const exec = async (req, action) => {
158
179
errorMsg . join ( '\n' )
159
180
) ;
160
181
161
-
162
182
action . addStep ( step ) ;
163
183
return action ;
164
184
}
@@ -168,4 +188,5 @@ const exec = async (req, action) => {
168
188
} ;
169
189
170
190
exec . displayName = 'scanDiff.exec' ;
171
- exports . exec = exec ;
191
+
192
+ export { exec } ;
0 commit comments