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

Commit

Permalink
Implement action (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
micnncim authored Oct 14, 2019
1 parent 361f19b commit ca4488d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
54 changes: 53 additions & 1 deletion cmd/action-lgtm-reaction/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit ca4488d

Please sign in to comment.