Skip to content

Commit

Permalink
Find pr number with commit hash (#28)
Browse files Browse the repository at this point in the history
* create dynamics to use commit sha to find pr number

* create dynamics to use commit sha to find pr number

* Add docs

* Add docs

Co-authored-by: Andriy Knysh <[email protected]>
  • Loading branch information
tocy1 and aknysh authored Aug 30, 2022
1 parent 156af5a commit e552860
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
46 changes: 41 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down

0 comments on commit e552860

Please sign in to comment.