diff --git a/README.md b/README.md index 89c6729..84ae405 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,9 @@ Command-line arguments take precedence over ENV vars. | baseURL | GITHUB_BASE_URL | Github Enterprise URL. _E.g._ `https://github.example.com/api/v3` | | uploadURL | GITHUB_UPLOAD_URL | Github Enterprise Upload URL to pass to the Github client | | insecure | GITHUB_INSECURE | Boolean to ignore SSL certificate check | +| use-sha-for-pr | GITHUB_USE_SHA_FOR_PR | Boolean to use commit sha to find pull request. Used for cases when only commit sha is available and not pr number. Currently only supported for Comment type: `pr-review` | | +| pr-state | GITHUB_PR_STATE | Pull request current state. (_e.g._ `open`, `closed`). Required for -use-sha-for-pr. Needed to filter pull requests to only one | | +| base-branch | GITHUB_PR_BASE_BRANCH | Base branch of the Pull request. Required for -use-sha-for-pr. Needed to filter pull requests to only one | | __NOTE__: The utility accepts the text of the comment from the command-line argument `comment`, from the ENV variable `GITHUB_COMMENT`, or from the standard input. diff --git a/main.go b/main.go index 295be23..8e9f323 100644 --- a/main.go +++ b/main.go @@ -51,6 +51,9 @@ var ( baseURL = flag.String("baseURL", os.Getenv("GITHUB_BASE_URL"), "Base URL of github enterprise") uploadURL = flag.String("uploadURL", os.Getenv("GITHUB_UPLOAD_URL"), "Upload URL of github enterprise") insecure = flag.Bool("insecure", strings.ToLower(os.Getenv("GITHUB_INSECURE")) == "true", "Ignore SSL certificate check") + useCommitShaforPR = flag.Bool("use-sha-for-pr", strings.ToLower(os.Getenv("GITHUB_USE_SHA_FOR_PR")) == "true", "Use commit sha to find PR number") + state = flag.String("pr-state", os.Getenv("GITHUB_PR_STATE"), "State of the PR e.g closed,open. Default is open") + baseBranch = flag.String("base-branch", os.Getenv("GITHUB_PR_BASE_BRANCH"), "Base branch of pull request") ) func getPullRequestOrIssueNumber(str string) (int, error) { @@ -66,6 +69,21 @@ func getPullRequestOrIssueNumber(str string) (int, error) { return num, nil } + +func getPullRequestNumberFromSha( sha, state, base string, client *github.Client) (int, error) { + + pullRequestsService := client.PullRequests + opts := &github.PullRequestListOptions { + State: state, + Base: base, + } + pullRequests,_,err := pullRequestsService.ListPullRequestsWithCommit(context.Background(), *owner, *repo, sha, opts, ) + if err !=nil { + return 0, err + } + return *pullRequests[0].Number, nil +} + func getPullRequestFilePosition(str string) (int, error) { if str == "" { return 0, errors.New("-position or GITHUB_PR_FILE_POSITION required") @@ -264,12 +282,30 @@ func main() { log.Println("github-commenter: Created GitHub Commit comment", *commitComment.ID) } else if *commentType == "pr-review" { - // https://developer.github.com/v3/pulls/reviews/#create-a-pull-request-review - num, err := getPullRequestOrIssueNumber(*number) - if err != nil { - log.Fatal(err) + var prNumber int + if *useCommitShaforPR { + if *baseBranch == "" || *state == "" { + flag.PrintDefaults() + log.Fatal("github-commenter: ( -pr-state or GITHUB_PR_STATE ) and ( -basebranch or GITHUB_PR_BASE_BRANCH ) must be provided when using flag -use-sha-for-pr ") + } + num,err := getPullRequestNumberFromSha(*sha, *state, *baseBranch, githubClient) + if err != nil{ + log.Fatal(err) + } + prNumber = num + + } else { + // https://developer.github.com/v3/pulls/reviews/#create-a-pull-request-review + num, err := getPullRequestOrIssueNumber(*number) + if err != nil { + log.Fatal(err) + } + prNumber = num } + + + comment, err := getComment() if err != nil { log.Fatal(err) @@ -281,7 +317,7 @@ func main() { } pullRequestReviewRequest := &github.PullRequestReviewRequest{Body: &formattedComment, Event: github.String("COMMENT")} - pullRequestReview, _, err := githubClient.PullRequests.CreateReview(context.Background(), *owner, *repo, num, pullRequestReviewRequest) + pullRequestReview, _, err := githubClient.PullRequests.CreateReview(context.Background(), *owner, *repo, prNumber, pullRequestReviewRequest) if err != nil { log.Fatal(err) }