From 8c517e3fa6ebda5b8e5336cf8a6082042febbc8d Mon Sep 17 00:00:00 2001 From: Jeffrey Guenther Date: Fri, 1 Oct 2021 17:02:27 -0700 Subject: [PATCH 1/5] Make default regex default values empty strings We use a YAML nil `~` to provide a default of empty string. --- action.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/action.yml b/action.yml index c8a21cc..19e8cfe 100644 --- a/action.yml +++ b/action.yml @@ -15,10 +15,11 @@ inputs: delete_prev_regex_msg: description: If not empty, all messages matching the given regex will be deleted. required: false - default: nil + default: ~ duplicate_msg_pattern: description: Optional pattern to use when checking for duplicate message. required: false + default: ~ runs: using: 'docker' From d728af3ba79acece5817bb627aab27571ded96cb Mon Sep 17 00:00:00 2001 From: Jeffrey Guenther Date: Fri, 1 Oct 2021 17:15:19 -0700 Subject: [PATCH 2/5] Check for empty strings rather than falsey values In Ruby, an empty string is truthy. As a result, passed a value of "" or "nil", these branches of the code will run. As a result, we need to change the default values of the input to `""` and check if the passed value is an empty string. No parsing is done to the values passed in as args so these are just strings. `"nil"` doesn't become `nil` and is truthy. --- entrypoint.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index bb11f74..8123d72 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -43,8 +43,7 @@ end comments = github.issue_comments(repo, pr_number) if check_duplicate_msg == "true" - duplicate = if duplicate_msg_pattern - comments.find { |c| c["body"] =~ Regexp.new(duplicate_msg_pattern) } + duplicate = if !duplicate_msg_pattern.empty? else comments.find { |c| c["body"] == message } end @@ -55,7 +54,7 @@ if check_duplicate_msg == "true" end end -if delete_prev_regex_msg + if !delete_prev_regex_msg.empty? comments.each do |comment| if comment["body"].match(/#{delete_prev_regex_msg}/) github.delete_comment(repo, comment["id"]) From eab3a30f7b38ac52fcb7fa2f1d79dc374bbb6c05 Mon Sep 17 00:00:00 2001 From: Jeffrey Guenther Date: Fri, 1 Oct 2021 17:15:59 -0700 Subject: [PATCH 3/5] Update input descriptions We want to make thse messages consistent and read in the positive. "not empty" is harder to understand than "provided". Also, let's match the description formats. --- action.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index 19e8cfe..f4ac186 100644 --- a/action.yml +++ b/action.yml @@ -13,11 +13,11 @@ inputs: required: false default: true delete_prev_regex_msg: - description: If not empty, all messages matching the given regex will be deleted. + description: If provided, all messages matching the given regex will be deleted. required: false default: ~ duplicate_msg_pattern: - description: Optional pattern to use when checking for duplicate message. + description: If provided, the regex pattern will be used when checking for duplicate messages. required: false default: ~ From aec5cbe272bfc179b9748dfbf8e9c4430d6840c7 Mon Sep 17 00:00:00 2001 From: Jeffrey Guenther Date: Fri, 1 Oct 2021 17:17:25 -0700 Subject: [PATCH 4/5] Use consistent creation of regex Let's use the same approach for matching the regex to make this file easier future changes. This approach uses ruby regex string interpolation, where you can turn a string into a regex with interpolating it into a regex expression. --- entrypoint.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/entrypoint.sh b/entrypoint.sh index 8123d72..9082899 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -44,6 +44,7 @@ comments = github.issue_comments(repo, pr_number) if check_duplicate_msg == "true" duplicate = if !duplicate_msg_pattern.empty? + comments.find { |c| c["body"].match(/#{duplicate_msg_pattern}/) } else comments.find { |c| c["body"] == message } end From 9aac0ab874c140608e0bde6edbb8df09625366c6 Mon Sep 17 00:00:00 2001 From: Jeffrey Guenther Date: Fri, 1 Oct 2021 17:20:35 -0700 Subject: [PATCH 5/5] Skip retrieving comments if unused Let's skip retrieving the list of issue comments if none of the options requiring them will be used. This restores a previous optimization that was removed. --- entrypoint.sh | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 9082899..6011d24 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -40,25 +40,27 @@ else pr_number = pr["number"] end -comments = github.issue_comments(repo, pr_number) +if !duplicate_msg_pattern.empty? || !delete_prev_regex_msg.empty? + comments = github.issue_comments(repo, pr_number) -if check_duplicate_msg == "true" + if check_duplicate_msg == "true" duplicate = if !duplicate_msg_pattern.empty? comments.find { |c| c["body"].match(/#{duplicate_msg_pattern}/) } - else - comments.find { |c| c["body"] == message } - end + else + comments.find { |c| c["body"] == message } + end - if duplicate - puts "The PR already contains this message" - exit(0) + if duplicate + puts "The PR already contains this message" + exit(0) + end end -end if !delete_prev_regex_msg.empty? - comments.each do |comment| - if comment["body"].match(/#{delete_prev_regex_msg}/) - github.delete_comment(repo, comment["id"]) + comments.each do |comment| + if comment["body"].match(/#{delete_prev_regex_msg}/) + github.delete_comment(repo, comment["id"]) + end end end end