From abf1e114e4ddefe0719ecbac813c1380a5b41242 Mon Sep 17 00:00:00 2001 From: micnncim Date: Wed, 16 Oct 2019 00:39:10 +0900 Subject: [PATCH] Support regexp and array in trigger (#17) --- README.md | 2 +- action.yml | 2 +- cmd/action-lgtm-reaction/main.go | 35 ++++++++++++++++++++++++++++++-- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 062957b..7b3f730 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ jobs: GITHUB_REVIEW_BODY: ${{ github.event.review.body }} GITHUB_REVIEW_ID: ${{ github.event.review.id }} with: - trigger: 'looks good to me' # default: 'lgtm' + trigger: '[".*looks good to me.*"]' # default: '["^(lgtm|LGTM)$", "^[gG]ood [jJ]ob!?$"]' override: true # default: false ``` diff --git a/action.yml b/action.yml index c0627a1..dbeebd7 100644 --- a/action.yml +++ b/action.yml @@ -5,7 +5,7 @@ inputs: trigger: description: 'Trigger comment on issue to send reaction' required: false - default: 'lgtm' + default: '["^(lgtm|LGTM)$", "^[gG]ood [jJ]ob!?$"]' overide: description: 'Whether the original comment is overridden' required: false diff --git a/cmd/action-lgtm-reaction/main.go b/cmd/action-lgtm-reaction/main.go index 684b82d..f35d069 100644 --- a/cmd/action-lgtm-reaction/main.go +++ b/cmd/action-lgtm-reaction/main.go @@ -2,9 +2,11 @@ package main import ( "context" + "encoding/json" "fmt" "math/rand" "os" + "regexp" "strconv" "strings" "time" @@ -33,8 +35,14 @@ func main() { exit("unable to parse string to bool in override flag: %v\n", err) } - matchComment := strings.ToUpper(trigger) == strings.ToUpper(githubCommentBody) - matchReview := strings.ToUpper(trigger) == strings.ToUpper(githubReviewBody) + matchComment, err := matchTrigger(trigger, githubCommentBody) + if err != nil { + exit("invalid trigger: %v\n", err) + } + matchReview, err := matchTrigger(trigger, githubReviewBody) + if err != nil { + exit("invalid trigger: %v\n", err) + } needCreateComment := (matchComment || matchReview) && !needOverride needUpdateComment := matchComment && needOverride @@ -116,3 +124,26 @@ func exit(format string, a ...interface{}) { fmt.Fprintf(os.Stderr, format, a...) os.Exit(1) } + +// trigger is expected as JSON array like '["a", "b"]'. +func parseTrigger(trigger string) ([]string, error) { + var a []string + if err := json.Unmarshal([]byte(trigger), &a); err != nil { + return nil, err + } + return a, nil +} + +func matchTrigger(trigger, target string) (bool, error) { + regexps, err := parseTrigger(trigger) + if err != nil { + return false, err + } + for _, s := range regexps { + r := regexp.MustCompile(s) + if r.MatchString(target) { + return true, nil + } + } + return false, nil +}