From ca4488db3a04b0b23b3a843bd1b15aa82f1d69a4 Mon Sep 17 00:00:00 2001 From: micnncim Date: Mon, 14 Oct 2019 14:08:43 +0900 Subject: [PATCH] Implement action (#3) --- README.md | 3 +- cmd/action-lgtm-reaction/main.go | 54 +++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7d3701e..c7f43b6 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,8 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GIPHY_API_KEY: ${{ secrets.GIPHY_API_KEY }} GITHUB_REPOSITORY: ${{ github.repository }} - GITHUB_COMMENT: ${{ github.event }} + GITHUB_COMMENT_BODY: ${{ github.event.comment.body }} + GITHUB_ISSUE_NUMBER: ${{ github.event.issue.number }} with: trigger: 'looks good to me' # default: lgtm ``` diff --git a/cmd/action-lgtm-reaction/main.go b/cmd/action-lgtm-reaction/main.go index 2eccf89..961eb25 100644 --- a/cmd/action-lgtm-reaction/main.go +++ b/cmd/action-lgtm-reaction/main.go @@ -1,10 +1,62 @@ package main import ( + "context" "fmt" "os" + "strconv" + "strings" + + "github.com/micnncim/action-lgtm-reaction/pkg/giphy" + "github.com/micnncim/action-lgtm-reaction/pkg/github" ) func main() { - fmt.Fprintf(os.Stderr, "INPUT_TRIGGER: %s", os.Getenv("INPUT_TRIGGER")) + trigger := os.Getenv("INPUT_TRIGGER") + givenComment := os.Getenv("GITHUB_COMMENT_BODY") + if strings.ToUpper(trigger) != strings.ToUpper(givenComment) { + fmt.Fprintf(os.Stderr, "no match issue comment\n") + return + } + + apiKey := os.Getenv("GIPHY_API_KEY") + giphyClient, err := giphy.NewClient(apiKey) + if err != nil { + fmt.Fprintf(os.Stderr, "unable to create giphy client: %v\n", err) + os.Exit(1) + } + giphies, err := giphyClient.Search("lgtm") + if err != nil { + os.Exit(1) + } + if len(giphies) == 0 { + fmt.Fprintf(os.Stderr, "no giphy contents found\n") + os.Exit(1) + } + + token := os.Getenv("GITHUB_TOKEN") + githubClient, err := github.NewClient(token) + if err != nil { + fmt.Fprintf(os.Stderr, "unable to create github client: %v\n", err) + os.Exit(1) + } + + repository := os.Getenv("GITHUB_REPOSITORY") + slugs := strings.Split(repository, "/") + if len(slugs) != 2 { + fmt.Fprintf(os.Stderr, "invalid repository: %v\n", repository) + os.Exit(1) + } + 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() + comment := giphies[0].GIFURLInMarkdownStyle() + if err := githubClient.CreateIssueComment(ctx, owner, repo, number, comment); err != nil { + os.Exit(1) + } }