70 for checked_file
in similarities.keys():
71 markdown +=
"<details><summary>%s</summary>\n\n" % checked_file
74 table_header = [
"File",
"Similarity (%)"]
77 for (f, s)
in similarities[checked_file].items()
81 reverse=
True, key=
lambda row: float(row[1].replace(WARNING_SUFFIX,
""))
83 entire_table = [[]
for _
in range(len(table_contents) + 1)]
84 entire_table[0] = table_header
85 for i
in range(1, len(table_contents) + 1):
86 entire_table[i] = table_contents[i - 1]
89 markdown +=
"</details>\n"
103 parser = argparse.ArgumentParser(
104 description=
"Duplicate code detection action runner"
110 help=
"The latest commit hash or branch",
113 "--pull-request-id", type=str, required=
True, help=
"The pull request id"
115 args = parser.parse_args()
117 fail_threshold = os.environ.get(
"INPUT_FAIL_ABOVE")
118 directories = os.environ.get(
"INPUT_DIRECTORIES")
119 ignore_directories = os.environ.get(
"INPUT_IGNORE_DIRECTORIES")
120 project_root_dir = os.environ.get(
"INPUT_PROJECT_ROOT_DIR")
121 file_extensions = os.environ.get(
"INPUT_FILE_EXTENSIONS")
122 ignore_threshold = os.environ.get(
"INPUT_IGNORE_BELOW")
123 only_code = os.environ.get(
"INPUT_ONLY_CODE")
127 ignore_directories_list = (
128 split_and_trim(ignore_directories)
if ignore_directories !=
"" else list()
132 project_root_dir = os.path.abspath(project_root_dir)
135 ignore_files_list =
None
144 ignore_directories_list,
148 file_extensions_list,
149 int(ignore_threshold),
155 if detection_result == duplicate_code_detection.ReturnCode.BAD_INPUT:
156 print(
"Action aborted due to bad user input")
157 return detection_result.value
158 elif detection_result == duplicate_code_detection.ReturnCode.THRESHOLD_EXCEEDED:
160 "Action failed due to maximum similarity threshold exceeded, check the report"
163 repo = os.environ.get(
"GITHUB_REPOSITORY")
164 files_url_prefix =
"https://github.com/%s/blob/%s/" % (repo, args.latest_head)
165 warn_threshold = os.environ.get(
"INPUT_WARN_ABOVE")
167 header_message_start = os.environ.get(
"INPUT_HEADER_MESSAGE_START") +
"\n"
168 message = header_message_start
169 message +=
"The [tool](https://github.com/platisd/duplicate-code-detection-tool)"
170 message +=
" analyzed your source code and found the following degree of"
171 message +=
" similarity between the files:\n"
173 code_similarity, files_url_prefix, warn_threshold
176 github_token = os.environ.get(
"INPUT_GITHUB_TOKEN")
177 github_api_url = os.environ.get(
"GITHUB_API_URL")
179 request_url =
"%s/repos/%s/issues/%s/comments" % (
182 args.pull_request_id,
186 "Authorization":
"token %s" % github_token,
188 report = {
"body": message}
190 update_existing_comment = os.environ.get(
"INPUT_ONE_COMMENT",
"false").lower()
in (
194 comment_updated =
False
195 if update_existing_comment:
197 pr_comments = requests.get(request_url, headers=headers).json()
198 for pr_comment
in pr_comments[::-1]:
199 if pr_comment[
"body"].startswith(header_message_start):
200 update_result = requests.patch(
205 if update_result.status_code != 200:
207 "Updating existing comment failed with code: "
208 + str(update_result.status_code)
210 print(update_result.text)
211 print(
"Attempting to post a new comment instead")
213 comment_updated =
True
216 if not comment_updated:
217 post_result = requests.post(
223 if post_result.status_code != 201:
225 "Posting results to GitHub failed with code: "
226 + str(post_result.status_code)
228 print(post_result.text)
230 with open(
"message.md",
"w")
as f:
233 return detection_result.value
run(fail_threshold, directories, files, ignore_directories, ignore_files, json_output, project_root_dir, file_extensions, ignore_threshold, only_code, csv_output, show_loc)