From d7e58a7d2a50b9031fb674e8ea162bec36256f24 Mon Sep 17 00:00:00 2001 From: micnncim Date: Wed, 16 Oct 2019 08:23:20 +0900 Subject: [PATCH] Use github context instead of each github event context (#20) --- README.md | 10 +--- cmd/action-lgtm-reaction/main.go | 79 ++++++++++++++++++-------------- 2 files changed, 47 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 495d81d..b78f544 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Currently supports [GIPHY](https://giphy.com). ### Create Workflow -`jobs..steps.with.trigger` should be an JSON string array of regexp. +`jobs..steps.with.trigger` should be a JSON string array of regexp. ```yaml name: Send LGTM reaction @@ -31,13 +31,7 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GIPHY_API_KEY: ${{ secrets.GIPHY_API_KEY }} - GITHUB_REPOSITORY: ${{ github.repository }} - GITHUB_ISSUE_NUMBER: ${{ github.event.issue.number }} - GITHUB_COMMENT_BODY: ${{ github.event.comment.body }} - GITHUB_COMMENT_ID: ${{ github.event.comment.id }} - GITHUB_PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number }} - GITHUB_REVIEW_BODY: ${{ github.event.review.body }} - GITHUB_REVIEW_ID: ${{ github.event.review.id }} + GITHUB: ${{ github }} with: trigger: '[".*looks good to me.*"]' # default: '["^(lgtm|LGTM)$", "^[gG]ood [jJ]ob!?$"]' override: true # default: false diff --git a/cmd/action-lgtm-reaction/main.go b/cmd/action-lgtm-reaction/main.go index f35d069..7e8712f 100644 --- a/cmd/action-lgtm-reaction/main.go +++ b/cmd/action-lgtm-reaction/main.go @@ -16,30 +16,49 @@ import ( ) var ( - githubToken = os.Getenv("GITHUB_TOKEN") - giphyAPIKey = os.Getenv("GIPHY_API_KEY") - githubRepository = os.Getenv("GITHUB_REPOSITORY") - githubIssueNumber = os.Getenv("GITHUB_ISSUE_NUMBER") - githubCommentBody = os.Getenv("GITHUB_COMMENT_BODY") - githubCommentID = os.Getenv("GITHUB_COMMENT_ID") - githubPullRequestNumber = os.Getenv("GITHUB_PULL_REQUEST_NUMBER") - githubReviewBody = os.Getenv("GITHUB_REVIEW_BODY") - githubReviewID = os.Getenv("GITHUB_REVIEW_ID") - trigger = os.Getenv("INPUT_TRIGGER") - override = os.Getenv("INPUT_OVERRIDE") + githubToken = os.Getenv("GITHUB_TOKEN") + giphyAPIKey = os.Getenv("GIPHY_API_KEY") + githubContext = os.Getenv("GITHUB") + trigger = os.Getenv("INPUT_TRIGGER") + override = os.Getenv("INPUT_OVERRIDE") ) +type GitHubContext struct { + Repository string `json:"repository"` + Event struct { + Issue struct { + Number int `json:"number"` + } + Comment struct { + ID int `json:"id"` + Body string `json:"body"` + } `json:"comment"` + PullRequest struct { + Number int `json:"number"` + } `json:"pull_request"` + Review struct { + ID int `json:"id"` + Body string `json:"body"` + } `json:"review"` + } `json:"event"` +} + func main() { + ghContext, err := parseGitHubContext(githubContext) + if err != nil { + exit("unable to parse github context: %v\n", err) + } + needOverride, err := strconv.ParseBool(override) if err != nil { exit("unable to parse string to bool in override flag: %v\n", err) } - matchComment, err := matchTrigger(trigger, githubCommentBody) + matchComment, err := matchTrigger(trigger, ghContext.Event.Comment.Body) if err != nil { exit("invalid trigger: %v\n", err) } - matchReview, err := matchTrigger(trigger, githubReviewBody) + matchReview, err := matchTrigger(trigger, ghContext.Event.Review.Body) if err != nil { exit("invalid trigger: %v\n", err) } @@ -70,9 +89,9 @@ func main() { exit("unable to create github client: %v\n", err) } - slugs := strings.Split(githubRepository, "/") + slugs := strings.Split(ghContext.Repository, "/") if len(slugs) != 2 { - exit("invalid githubRepository: %v\n", githubRepository) + exit("invalid githubRepository: %v\n", ghContext.Repository) } owner, repo := slugs[0], slugs[1] @@ -83,37 +102,21 @@ func main() { ctx := context.Background() if needUpdateComment { - 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 { + if err := githubClient.UpdateIssueComment(ctx, owner, repo, ghContext.Event.Comment.ID, comment); err != nil { exit("unable to update issue comment: %v\n", err) } return } if needCreateComment { - 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 { + if err := githubClient.CreateIssueComment(ctx, owner, repo, ghContext.Event.Issue.Number, comment); err != nil { exit("unable to create issue comment: %v\n", err) } return } if needUpdateReview { - number, err := strconv.Atoi(githubPullRequestNumber) - if err != nil { - exit("unable to convert string to int in issue number: %v\n", err) - } - reviewID, err := strconv.Atoi(githubReviewID) - if err != nil { - exit("unable to convert string to int in review id: %v\n", err) - } - if err := githubClient.UpdateReview(ctx, owner, repo, number, reviewID, comment); err != nil { + if err := githubClient.UpdateReview(ctx, owner, repo, ghContext.Event.PullRequest.Number, ghContext.Event.Review.ID, comment); err != nil { exit("unable to update review: %v\n", err) } return @@ -125,6 +128,14 @@ func exit(format string, a ...interface{}) { os.Exit(1) } +func parseGitHubContext(s string) (*GitHubContext, error) { + gc := &GitHubContext{} + if err := json.Unmarshal([]byte(s), gc); err != nil { + return nil, err + } + return gc, nil +} + // trigger is expected as JSON array like '["a", "b"]'. func parseTrigger(trigger string) ([]string, error) { var a []string