11package api
22
33import (
4+ "context"
45 "fmt"
56 "strings"
67 "time"
78
9+ "github.com/shurcooL/githubv4"
10+
811 "github.com/cli/cli/internal/ghrepo"
912)
1013
14+ type PullRequestReviewState int
15+
16+ const (
17+ ReviewApprove PullRequestReviewState = iota
18+ ReviewRequestChanges
19+ ReviewComment
20+ )
21+
22+ type PullRequestReviewInput struct {
23+ Body string
24+ State PullRequestReviewState
25+ }
26+
1127type PullRequestsPayload struct {
1228 ViewerCreated PullRequestAndTotalCount
1329 ReviewRequested PullRequestAndTotalCount
@@ -24,6 +40,7 @@ type PullRequest struct {
2440 Number int
2541 Title string
2642 State string
43+ Closed bool
2744 URL string
2845 BaseRefName string
2946 HeadRefName string
@@ -345,10 +362,12 @@ func PullRequestByNumber(client *Client, repo ghrepo.Interface, number int) (*Pu
345362 query($owner: String!, $repo: String!, $pr_number: Int!) {
346363 repository(owner: $owner, name: $repo) {
347364 pullRequest(number: $pr_number) {
365+ id
348366 url
349367 number
350368 title
351369 state
370+ closed
352371 body
353372 author {
354373 login
@@ -451,6 +470,7 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, hea
451470 repository(owner: $owner, name: $repo) {
452471 pullRequests(headRefName: $headRefName, states: OPEN, first: 30) {
453472 nodes {
473+ id
454474 number
455475 title
456476 state
@@ -654,6 +674,34 @@ func isBlank(v interface{}) bool {
654674 }
655675}
656676
677+ func AddReview (client * Client , pr * PullRequest , input * PullRequestReviewInput ) error {
678+ var mutation struct {
679+ AddPullRequestReview struct {
680+ ClientMutationID string
681+ } `graphql:"addPullRequestReview(input:$input)"`
682+ }
683+
684+ state := githubv4 .PullRequestReviewEventComment
685+ switch input .State {
686+ case ReviewApprove :
687+ state = githubv4 .PullRequestReviewEventApprove
688+ case ReviewRequestChanges :
689+ state = githubv4 .PullRequestReviewEventRequestChanges
690+ }
691+
692+ body := githubv4 .String (input .Body )
693+
694+ gqlInput := githubv4.AddPullRequestReviewInput {
695+ PullRequestID : pr .ID ,
696+ Event : & state ,
697+ Body : & body ,
698+ }
699+
700+ v4 := githubv4 .NewClient (client .http )
701+
702+ return v4 .Mutate (context .Background (), & mutation , gqlInput , nil )
703+ }
704+
657705func PullRequestList (client * Client , vars map [string ]interface {}, limit int ) (* PullRequestAndTotalCount , error ) {
658706 type prBlock struct {
659707 Edges []struct {
@@ -822,6 +870,44 @@ loop:
822870 return & res , nil
823871}
824872
873+ func PullRequestClose (client * Client , repo ghrepo.Interface , pr * PullRequest ) error {
874+ var mutation struct {
875+ ClosePullRequest struct {
876+ PullRequest struct {
877+ ID githubv4.ID
878+ }
879+ } `graphql:"closePullRequest(input: $input)"`
880+ }
881+
882+ input := githubv4.ClosePullRequestInput {
883+ PullRequestID : pr .ID ,
884+ }
885+
886+ v4 := githubv4 .NewClient (client .http )
887+ err := v4 .Mutate (context .Background (), & mutation , input , nil )
888+
889+ return err
890+ }
891+
892+ func PullRequestReopen (client * Client , repo ghrepo.Interface , pr * PullRequest ) error {
893+ var mutation struct {
894+ ReopenPullRequest struct {
895+ PullRequest struct {
896+ ID githubv4.ID
897+ }
898+ } `graphql:"reopenPullRequest(input: $input)"`
899+ }
900+
901+ input := githubv4.ReopenPullRequestInput {
902+ PullRequestID : pr .ID ,
903+ }
904+
905+ v4 := githubv4 .NewClient (client .http )
906+ err := v4 .Mutate (context .Background (), & mutation , input , nil )
907+
908+ return err
909+ }
910+
825911func min (a , b int ) int {
826912 if a < b {
827913 return a
0 commit comments