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

Commit

Permalink
Support overriding comment (#14)
Browse files Browse the repository at this point in the history
* Refactor variable declaration in main

* Support overriding comment
  • Loading branch information
micnncim authored Oct 15, 2019
1 parent 82f5598 commit 48fe6aa
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 27 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
72 changes: 47 additions & 25 deletions cmd/action-lgtm-reaction/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
20 changes: 19 additions & 1 deletion pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 48fe6aa

Please sign in to comment.