Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Commit

Permalink
Support regexp and array in trigger (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
micnncim authored Oct 15, 2019
1 parent e7bb179 commit abf1e11
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 33 additions & 2 deletions cmd/action-lgtm-reaction/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package main

import (
"context"
"encoding/json"
"fmt"
"math/rand"
"os"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit abf1e11

Please sign in to comment.