15
15
'''
16
16
This version of the script is intended to produce a JSON output and used in a CI environment
17
17
like GitHub Actions or GitLab CI.
18
-
19
- Note: This Python script only functions in check mode.
20
18
'''
21
19
22
20
import argparse
23
21
import constants
24
22
import hashlib
25
23
import json
26
24
import os
27
- import re
28
25
import sys
29
- import requests
30
26
from subprocess import getoutput
31
27
from copy import copy
32
28
from tools .event2splunk import Event2Splunk
@@ -47,8 +43,6 @@ def build_args_dict(args=None):
47
43
parser = argparse .ArgumentParser ()
48
44
parser .add_argument ('--path' )
49
45
parser .add_argument ('--url' )
50
- parser .add_argument ('--mode' )
51
- parser .add_argument ('--verbose' , action = 'store_true' )
52
46
parser .add_argument ('--err_file' )
53
47
parser .add_argument ('--splunk' , action = 'store_true' )
54
48
parser .add_argument ('--h_endpoint' )
@@ -64,14 +58,6 @@ def build_args_dict(args=None):
64
58
raise Exception ('No path specified' )
65
59
if path .endswith ('/' ):
66
60
path = path [:- 1 ]
67
- if args .mode == 'check' :
68
- mode = 'check'
69
- elif args .mode == 'fix' :
70
- raise Exception (
71
- 'Fix mode for JSON output is not yet supported. Please use --mode=check or use standard output with fix mode instead.' )
72
- if not mode :
73
- raise Exception (
74
- 'Invalid mode specified. Please specify --mode=check or --mode=fix.' )
75
61
if args .err_file :
76
62
if not os .path .exists (args .err_file ):
77
63
sys .stdout .write ('%sWarning: no file "%s" for error logs found. Defaulting to "%s". %s\n ' % (
@@ -83,8 +69,6 @@ def build_args_dict(args=None):
83
69
return {
84
70
'path' : path ,
85
71
'url' : args .url or os .environ .get ('GITHUB_URL' ),
86
- 'mode' : mode ,
87
- 'is_verbose' : args .verbose ,
88
72
'splunk_flag' : args .splunk ,
89
73
'err_file' : args .err_file ,
90
74
'h_endpoint' : args .h_endpoint ,
@@ -102,7 +86,7 @@ def build_args_dict(args=None):
102
86
'''
103
87
104
88
105
- def process_word_occurrences (results , batch_info , biased_word , is_verbose , path , splunk_flag ):
89
+ def process_word_occurrences (results , batch_info , biased_word , path , splunk_flag ):
106
90
json_result , report , events = {'biased_word' : biased_word }, [], []
107
91
files , lines = [], []
108
92
@@ -147,12 +131,6 @@ def process_word_occurrences(results, batch_info, biased_word, is_verbose, path,
147
131
'fingerprint' : hashlib .md5 (string .encode ('utf-8' )).hexdigest ()
148
132
}
149
133
150
- if is_verbose :
151
- # add to json_result
152
- lines .append ({'line' : line , 'location' : location })
153
- # add to code quality report
154
- occurrence ['line' ] = line
155
- # code quality report
156
134
report .append (occurrence )
157
135
# code quality events - additional details if Splunking
158
136
if splunk_flag :
@@ -192,7 +170,7 @@ def process_biased_word_line(line, occurrences, code_quality_report, splunk_even
192
170
# the data summary entry will always be there, hence the > 1
193
171
if len (rg_results ) > 1 :
194
172
json_results , word_report , events = process_word_occurrences (
195
- rg_results , batch_info , biased_word , args ['is_verbose' ], args [ ' path' ], args ['splunk_flag' ])
173
+ rg_results , batch_info , biased_word , args ['path' ], args ['splunk_flag' ])
196
174
terms_found = True
197
175
198
176
# add to code quality output and to Splunkable events list
@@ -207,7 +185,6 @@ def process_biased_word_line(line, occurrences, code_quality_report, splunk_even
207
185
208
186
return terms_found , copy_occurrences
209
187
210
-
211
188
def main (args , logger ):
212
189
main_timer = TimeFunction ('main' , logger )
213
190
main_timer .start ()
@@ -223,82 +200,69 @@ def main(args, logger):
223
200
args ['path' ], constants .EXCLUDE_FILE , constants .RGIGNORE_FILE )
224
201
lines = open_csv ('word_list.csv' )
225
202
226
- if args ['mode' ] == 'check' :
227
- occurrences = {'biased_words' : [],
228
- 'mode' : args ['mode' ], 'verbose' : args ['is_verbose' ]}
229
- code_quality_report , splunk_events = [], []
230
- terms_found = False
231
-
232
- # Generate JSON
233
- for line in lines :
234
- terms_found , occurrences = process_biased_word_line (
235
- line , occurrences , code_quality_report , splunk_events , args , batch_info , terms_found , logger )
236
-
237
- occurrences ['terms_found' ] = terms_found
238
-
239
- # every JSON in the codeclimate array is a line found
240
- occurrences ['total_lines_matched' ] = len (code_quality_report )
241
- # dedupes the files and accounts for all words for total count
242
- all_files_matched = []
243
- occurrences ['total_words_matched' ] = 0
244
- for word in occurrences ['biased_words' ]:
245
- if word in occurrences and len (occurrences [word ]) > 0 :
246
- occurrences ['total_words_matched' ] += occurrences [word ]['num_matched_words' ]
247
- all_files_matched = list (
248
- set (all_files_matched ) | set (occurrences [word ]['files' ]))
249
- occurrences ['total_files_matched' ] = len (all_files_matched )
250
-
251
- # print output to console
252
- print (json .dumps (occurrences , indent = 2 ))
253
-
254
- write_file (constants .SUMMARY_FILENAME , occurrences )
255
- write_file (constants .CODECLIMATE_FILENAME , code_quality_report )
256
- # final error check for check mode
257
- if terms_found :
258
- error_message = '%sError: %sBiased Lang Linter%s found biased words. Replacement(s) required. 🚨\n See JSON output for details on what to replace. 🕵🏽♀️ %s\n ' % (
259
- c ['red' ], c ['lightmagenta' ], c ['red' ], c ['nc' ])
260
- sys .stderr .write (error_message )
261
- if args ['err_file' ]:
262
- with open (args ['err_file' ], 'w' ) as errfile :
263
- errfile .write (error_message )
264
- if args ['splunk_flag' ]:
265
- # Splunk the code quality report
266
- # If ran in GitHub, call endpoint to Splunk data
267
- if args ['github_repo' ]:
268
- # TODO: Call endpoint to post data to Splunk instance
269
- print ('Posting data to Splunk' )
270
- else :
271
- send_codeclimate_batch (constants .CODECLIMATE_FILENAME , splunk_events ,
272
- repo_name , source_type , event2splunk )
273
- send_codeclimate_batch (constants .CODECLIMATE_FILENAME , splunk_events ,
274
- repo_name , source_type , pz_event2splunk )
203
+ # if args['mode'] == 'check':
204
+ occurrences = {'biased_words' : []}
205
+ code_quality_report , splunk_events = [], []
206
+ terms_found = False
207
+
208
+ # Generate JSON
209
+ for line in lines :
210
+ terms_found , occurrences = process_biased_word_line (
211
+ line , occurrences , code_quality_report , splunk_events , args , batch_info , terms_found , logger )
212
+
213
+ occurrences ['terms_found' ] = terms_found
214
+
215
+ # every JSON in the codeclimate array is a line found
216
+ occurrences ['total_lines_matched' ] = len (code_quality_report )
217
+ # dedupes the files and accounts for all words for total count
218
+ all_files_matched = []
219
+ occurrences ['total_words_matched' ] = 0
220
+ for word in occurrences ['biased_words' ]:
221
+ if word in occurrences and len (occurrences [word ]) > 0 :
222
+ occurrences ['total_words_matched' ] += occurrences [word ]['num_matched_words' ]
223
+ all_files_matched = list (
224
+ set (all_files_matched ) | set (occurrences [word ]['files' ]))
225
+ occurrences ['total_files_matched' ] = len (all_files_matched )
226
+
227
+ # print output to console
228
+ print (json .dumps (occurrences , indent = 2 ))
229
+
230
+ write_file (constants .SUMMARY_FILENAME , occurrences )
231
+ write_file (constants .CODECLIMATE_FILENAME , code_quality_report )
232
+ err_file = args ['err_file' ]
233
+ # final error check for check mode
234
+ if not terms_found :
235
+ sys .stdout .write ('%sBiased Lang Linter %sfound no biased words! 🎉%s\n ' % (
236
+ c ['lightmagenta' ], c ['green' ], c ['nc' ]))
237
+ else :
238
+ error_message = '%sError: %sBiased Lang Linter%s found biased words. Replacement(s) required. 🚨\n See JSON output for details on what to replace. 🕵🏽♀️ %s\n ' % (
239
+ c ['red' ], c ['lightmagenta' ], c ['red' ], c ['nc' ])
240
+ sys .stderr .write (error_message )
241
+ if err_file :
242
+ with open (err_file , 'w' ) as errfile :
243
+ errfile .write (error_message )
275
244
276
- else :
277
- sys .stdout .write ('%sBiased Lang Linter %sfound no biased words! 🎉%s\n ' % (
278
- c ['lightmagenta' ], c ['green' ], c ['nc' ]))
279
-
280
- if args ['splunk_flag' ]:
281
- # Splunk the summarized JSON
282
- occurrences ['content' ] = constants .SUMMARY_FILENAME
283
- occurrences .update (batch_info )
284
- occurrences ['total_lines' ] = get_line_count (args ['path' ], excluded )
285
- occurrences ['run_time' ] = main_timer .stop ()
286
- # If ran in GitHub, call endpoint to Splunk data
287
- if args ['github_repo' ]:
288
- # TODO: Call endpoint to post data to Splunk instance
289
- print ('Splunking occurrences data from GitHub!' )
290
- else :
291
- event2splunk .post_event (payload = occurrences ,
292
- source = repo_name , sourcetype = source_type )
293
- event2splunk .close (filename = constants .SUMMARY_FILENAME )
294
- pz_event2splunk .post_event (
295
- payload = occurrences , source = repo_name , sourcetype = source_type )
296
- pz_event2splunk .close (filename = constants .SUMMARY_FILENAME )
297
- # For GitHub Actions to provide error annotations
298
- err_file = args ['err_file' ]
299
- if os .path .exists (err_file ) and args ['github_repo' ]:
300
- print (f'{ err_file } file found, exiting(1)' )
301
- sys .exit (1 )
245
+ if args ['splunk_flag' ]:
246
+ # Splunk the summarized JSON
247
+ occurrences ['content' ] = constants .SUMMARY_FILENAME
248
+ occurrences .update (batch_info )
249
+ occurrences ['total_lines' ] = get_line_count (args ['path' ], excluded )
250
+ occurrences ['run_time' ] = main_timer .stop ()
251
+ if not args ['github_repo' ]:
252
+ send_codeclimate_batch (constants .CODECLIMATE_FILENAME , splunk_events ,
253
+ repo_name , source_type , event2splunk )
254
+ send_codeclimate_batch (constants .CODECLIMATE_FILENAME , splunk_events ,
255
+ repo_name , source_type , pz_event2splunk )
256
+ event2splunk .post_event (payload = occurrences ,
257
+ source = repo_name , sourcetype = source_type )
258
+ event2splunk .close (filename = constants .SUMMARY_FILENAME )
259
+ pz_event2splunk .post_event (
260
+ payload = occurrences , source = repo_name , sourcetype = source_type )
261
+ pz_event2splunk .close (filename = constants .SUMMARY_FILENAME )
262
+ # For GitHub Actions to provide error annotations
263
+ if os .path .exists (err_file ) and args ['github_repo' ]:
264
+ print (f'{ err_file } file found, exiting(1)' )
265
+ sys .exit (1 )
302
266
303
267
304
268
if __name__ == '__main__' :
0 commit comments