diff --git a/README.md b/README.md index 4a0bdf2..d22558c 100644 --- a/README.md +++ b/README.md @@ -33,9 +33,11 @@ jobs: GIPHY_API_KEY: ${{ secrets.GIPHY_API_KEY }} GITHUB_REPOSITORY: ${{ github.repository }} GITHUB_COMMENT_BODY: ${{ github.event.comment.body }} + GITHUB_COMMENT_ID: ${{ github.event.comment.id }} # not necessary if `override` is false GITHUB_ISSUE_NUMBER: ${{ github.event.issue.number }} with: - trigger: 'looks good to me' # default: lgtm + trigger: 'looks good to me' # default: 'lgtm' + override: true # default: false ``` ## License diff --git a/action.yml b/action.yml index f07a767..c0627a1 100644 --- a/action.yml +++ b/action.yml @@ -6,6 +6,10 @@ inputs: description: 'Trigger comment on issue to send reaction' required: false default: 'lgtm' + overide: + description: 'Whether the original comment is overridden' + required: false + default: false runs: using: 'docker' image: 'Dockerfile' diff --git a/cmd/action-lgtm-reaction/main.go b/cmd/action-lgtm-reaction/main.go index c8efeda..f450cbf 100644 --- a/cmd/action-lgtm-reaction/main.go +++ b/cmd/action-lgtm-reaction/main.go @@ -13,56 +13,78 @@ import ( "github.com/micnncim/action-lgtm-reaction/pkg/github" ) +var ( + githubToken = os.Getenv("GITHUB_TOKEN") + giphyAPIKey = os.Getenv("GIPHY_API_KEY") + githubRepository = os.Getenv("GITHUB_REPOSITORY") + githubCommentBody = os.Getenv("GITHUB_COMMENT_BODY") + githubCommentID = os.Getenv("GITHUB_COMMENT_ID") + githubIssueNumber = os.Getenv("GITHUB_ISSUE_NUMBER") + trigger = os.Getenv("INPUT_TRIGGER") + override = os.Getenv("INPUT_OVERRIDE") +) + func main() { - trigger := os.Getenv("INPUT_TRIGGER") - givenComment := os.Getenv("GITHUB_COMMENT_BODY") - if strings.ToUpper(trigger) != strings.ToUpper(givenComment) { + if strings.ToUpper(trigger) != strings.ToUpper(githubCommentBody) { fmt.Fprintf(os.Stderr, "no match issue comment\n") return } - apiKey := os.Getenv("GIPHY_API_KEY") - giphyClient, err := giphy.NewClient(apiKey) + giphyClient, err := giphy.NewClient(giphyAPIKey) if err != nil { - fmt.Fprintf(os.Stderr, "unable to create giphy client: %v\n", err) - os.Exit(1) + exit("unable to create giphy client: %v\n", err) } giphies, err := giphyClient.Search("lgtm") if err != nil { - os.Exit(1) + exit("unable to search giphy :%v\n", err) } if len(giphies) == 0 { - fmt.Fprintf(os.Stderr, "no giphy contents found\n") - os.Exit(1) + exit("no giphy contents found\n") } - token := os.Getenv("GITHUB_TOKEN") - githubClient, err := github.NewClient(token) + githubClient, err := github.NewClient(githubToken) if err != nil { - fmt.Fprintf(os.Stderr, "unable to create github client: %v\n", err) - os.Exit(1) + exit("unable to create github client: %v\n", err) } - repository := os.Getenv("GITHUB_REPOSITORY") - slugs := strings.Split(repository, "/") + slugs := strings.Split(githubRepository, "/") if len(slugs) != 2 { - fmt.Fprintf(os.Stderr, "invalid repository: %v\n", repository) - os.Exit(1) + exit("invalid githubRepository: %v\n", githubRepository) } owner, repo := slugs[0], slugs[1] - issueNumber := os.Getenv("GITHUB_ISSUE_NUMBER") - number, err := strconv.Atoi(issueNumber) - if err != nil { - fmt.Fprintf(os.Stderr, "unable to convert string to int in issue number\n") - os.Exit(1) - } - ctx := context.Background() rand.Seed(time.Now().Unix()) index := rand.Intn(len(giphies)) comment := giphies[index].GIFURLInMarkdownStyle() + needUpdate, err := strconv.ParseBool(override) + if err != nil { + exit("unable to parse string to bool in override flag: %v\n", err) + } + + ctx := context.Background() + + if needUpdate { + commentID, err := strconv.ParseInt(githubCommentID, 10, 64) + if err != nil { + exit("unable to convert string to int in issue number: %v\n", err) + } + if err := githubClient.UpdateIssueComment(ctx, owner, repo, int(commentID), comment); err != nil { + exit("unable to update issue comment: %v\n", err) + } + return + } + + number, err := strconv.Atoi(githubIssueNumber) + if err != nil { + exit("unable to convert string to int in issue number: %v\n", err) + } if err := githubClient.CreateIssueComment(ctx, owner, repo, number, comment); err != nil { os.Exit(1) } } + +func exit(format string, a ...interface{}) { + fmt.Fprintf(os.Stderr, format, a...) + os.Exit(1) +} diff --git a/pkg/github/github.go b/pkg/github/github.go index a69e769..c946481 100644 --- a/pkg/github/github.go +++ b/pkg/github/github.go @@ -43,7 +43,25 @@ func (c *Client) CreateIssueComment(ctx context.Context, owner, repo string, num Body: pointer.String(body), }) if err != nil { - log.Error("unable to create issue", zap.Error(err)) + log.Error("unable to create issue comment", zap.Error(err)) + return err + } + return nil +} + +func (c *Client) UpdateIssueComment(ctx context.Context, owner, repo string, commentID int, body string) error { + log := c.log.With( + zap.String("owner", owner), + zap.String("repo", repo), + zap.Int("commentID", commentID), + zap.String("body", body), + ) + + _, _, err := c.githubClient.Issues.EditComment(ctx, owner, repo, int64(commentID), &github.IssueComment{ + Body: pointer.String(body), + }) + if err != nil { + log.Error("unable to update issue comment", zap.Error(err)) return err } return nil