Skip to content

Commit 6272a6c

Browse files
committed
Print out reason of failure when threshold exceeded
1 parent 03d3d78 commit 6272a6c

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

duplicate_code_detection.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import gensim
1111
import tempfile
1212
import json
13+
from enum import Enum
1314
from nltk.tokenize import word_tokenize
1415
from collections import OrderedDict
1516

@@ -19,6 +20,12 @@
1920
similarity_label_length = len(similarity_column_label)
2021

2122

23+
class ReturnCode(Enum):
24+
SUCCESS = 0
25+
BAD_INPUT = 1
26+
THRESHOLD_EXCEEDED = 2
27+
28+
2229
class CliColors:
2330
HEADER = '\033[95m'
2431
OKBLUE = '\033[94m'
@@ -89,7 +96,7 @@ def run(fail_threshold, directories, files, ignore_directories, ignore_files,
8996
for directory in directories:
9097
if not os.path.isdir(directory):
9198
print("Path does not exist or is not a directory:", directory)
92-
return (1, {})
99+
return (ReturnCode.BAD_INPUT, {})
93100
source_code_files += get_all_source_code_from_directory(
94101
directory, file_extensions)
95102
for directory in ignore_directories:
@@ -98,25 +105,25 @@ def run(fail_threshold, directories, files, ignore_directories, ignore_files,
98105
else:
99106
if len(files) < 2:
100107
print("Too few files to compare, you need to supply at least 2")
101-
return (1, {})
108+
return (ReturnCode.BAD_INPUT, {})
102109
for supplied_file in files:
103110
if not os.path.isfile(supplied_file):
104111
print("Supplied file does not exist:", supplied_file)
105-
return (1, {})
112+
return (ReturnCode.BAD_INPUT, {})
106113
source_code_files = files
107114

108115
files_to_ignore += ignore_files if ignore_files else list()
109116
source_code_files = list(set(source_code_files) - set(files_to_ignore))
110117
if len(source_code_files) < 2:
111118
print("Not enough source code files found")
112-
return (1, {})
119+
return (ReturnCode.BAD_INPUT, {})
113120

114121
# Get the absolute project root directory path to remove when printing out the results
115122
if project_root_dir:
116123
if not os.path.isdir(project_root_dir):
117124
print(
118125
"The project root directory does not exist or is not a directory:", project_root_dir)
119-
return (1, {})
126+
return (ReturnCode.BAD_INPUT, {})
120127
project_root_dir = os.path.abspath(project_root_dir)
121128
project_root_dir = os.path.join(
122129
project_root_dir, '') # Add the trailing slash
@@ -138,7 +145,7 @@ def run(fail_threshold, directories, files, ignore_directories, ignore_files,
138145
num_features=len(dictionary))
139146

140147
largest_string_length = len(max(source_code_files, key=len))
141-
exit_code = 0
148+
exit_code = ReturnCode.SUCCESS
142149
code_similarity = dict()
143150
for source_file in source_code:
144151
# Check for similarities
@@ -170,12 +177,12 @@ def run(fail_threshold, directories, files, ignore_directories, ignore_files,
170177
code_similarity[short_source_file_path][short_source_path] = round(
171178
similarity_percentage, 2)
172179
if similarity_percentage > fail_threshold:
173-
exit_code = 1
180+
exit_code = ReturnCode.THRESHOLD_EXCEEDED
174181
color = CliColors.OKGREEN if similarity_percentage < 10 else (
175182
CliColors.WARNING if similarity_percentage < 20 else CliColors.FAIL)
176183
conditional_print("%s " % (short_source_path.ljust(largest_string_length)) +
177184
color + "%.2f" % (similarity_percentage) + CliColors.ENDC, json_output)
178-
if exit_code == 1:
185+
if exit_code == ReturnCode.THRESHOLD_EXCEEDED:
179186
conditional_print(
180187
"Code duplication threshold exceeded. Please consult logs.", json_output)
181188
if json_output:
@@ -187,4 +194,4 @@ def run(fail_threshold, directories, files, ignore_directories, ignore_files,
187194

188195
if __name__ == "__main__":
189196
exit_code, _ = main()
190-
sys.exit(exit_code)
197+
sys.exit(exit_code.value)

run_action.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ def main():
117117
json_output, project_root_dir, file_extensions_list,
118118
int(ignore_threshold))
119119

120+
if detection_result == duplicate_code_detection.ReturnCode.BAD_INPUT:
121+
print("Action aborted due to bad user input")
122+
return detection_result.value
123+
elif detection_result == duplicate_code_detection.ReturnCode.THRESHOLD_EXCEEDED:
124+
print(
125+
"Action failed due to maximum similarity threshold exceeded, check the report")
126+
120127
repo = os.environ.get('GITHUB_REPOSITORY')
121128
files_url_prefix = 'https://github.com/%s/blob/%s/' % (
122129
repo, args.latest_head)
@@ -142,7 +149,7 @@ def main():
142149
str(post_result.status_code))
143150
print(post_result.text)
144151

145-
return detection_result
152+
return detection_result.value
146153

147154

148155
if __name__ == "__main__":

0 commit comments

Comments
 (0)