github.com/abdfnx/gh-api@v0.0.0-20210414084727-f5432eec23b8/api/queries_pr_review.go (about)

     1  package api
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/abdfnx/gh-api/internal/ghrepo"
     8  	"github.com/shurcooL/githubv4"
     9  )
    10  
    11  type PullRequestReviewState int
    12  
    13  const (
    14  	ReviewApprove PullRequestReviewState = iota
    15  	ReviewRequestChanges
    16  	ReviewComment
    17  )
    18  
    19  type PullRequestReviewInput struct {
    20  	Body  string
    21  	State PullRequestReviewState
    22  }
    23  
    24  type PullRequestReviews struct {
    25  	Nodes      []PullRequestReview
    26  	PageInfo   PageInfo
    27  	TotalCount int
    28  }
    29  
    30  type PullRequestReview struct {
    31  	Author              Author
    32  	AuthorAssociation   string
    33  	Body                string
    34  	CreatedAt           time.Time
    35  	IncludesCreatedEdit bool
    36  	ReactionGroups      ReactionGroups
    37  	State               string
    38  	URL                 string
    39  }
    40  
    41  func AddReview(client *Client, repo ghrepo.Interface, pr *PullRequest, input *PullRequestReviewInput) error {
    42  	var mutation struct {
    43  		AddPullRequestReview struct {
    44  			ClientMutationID string
    45  		} `graphql:"addPullRequestReview(input:$input)"`
    46  	}
    47  
    48  	state := githubv4.PullRequestReviewEventComment
    49  	switch input.State {
    50  	case ReviewApprove:
    51  		state = githubv4.PullRequestReviewEventApprove
    52  	case ReviewRequestChanges:
    53  		state = githubv4.PullRequestReviewEventRequestChanges
    54  	}
    55  
    56  	body := githubv4.String(input.Body)
    57  	variables := map[string]interface{}{
    58  		"input": githubv4.AddPullRequestReviewInput{
    59  			PullRequestID: pr.ID,
    60  			Event:         &state,
    61  			Body:          &body,
    62  		},
    63  	}
    64  
    65  	gql := graphQLClient(client.http, repo.RepoHost())
    66  	return gql.MutateNamed(context.Background(), "PullRequestReviewAdd", &mutation, variables)
    67  }
    68  
    69  func ReviewsForPullRequest(client *Client, repo ghrepo.Interface, pr *PullRequest) (*PullRequestReviews, error) {
    70  	type response struct {
    71  		Repository struct {
    72  			PullRequest struct {
    73  				Reviews PullRequestReviews `graphql:"reviews(first: 100, after: $endCursor)"`
    74  			} `graphql:"pullRequest(number: $number)"`
    75  		} `graphql:"repository(owner: $owner, name: $repo)"`
    76  	}
    77  
    78  	variables := map[string]interface{}{
    79  		"owner":     githubv4.String(repo.RepoOwner()),
    80  		"repo":      githubv4.String(repo.RepoName()),
    81  		"number":    githubv4.Int(pr.Number),
    82  		"endCursor": (*githubv4.String)(nil),
    83  	}
    84  
    85  	gql := graphQLClient(client.http, repo.RepoHost())
    86  
    87  	var reviews []PullRequestReview
    88  	for {
    89  		var query response
    90  		err := gql.QueryNamed(context.Background(), "ReviewsForPullRequest", &query, variables)
    91  		if err != nil {
    92  			return nil, err
    93  		}
    94  
    95  		reviews = append(reviews, query.Repository.PullRequest.Reviews.Nodes...)
    96  		if !query.Repository.PullRequest.Reviews.PageInfo.HasNextPage {
    97  			break
    98  		}
    99  		variables["endCursor"] = githubv4.String(query.Repository.PullRequest.Reviews.PageInfo.EndCursor)
   100  	}
   101  
   102  	return &PullRequestReviews{Nodes: reviews, TotalCount: len(reviews)}, nil
   103  }
   104  
   105  func (prr PullRequestReview) AuthorLogin() string {
   106  	return prr.Author.Login
   107  }
   108  
   109  func (prr PullRequestReview) Association() string {
   110  	return prr.AuthorAssociation
   111  }
   112  
   113  func (prr PullRequestReview) Content() string {
   114  	return prr.Body
   115  }
   116  
   117  func (prr PullRequestReview) Created() time.Time {
   118  	return prr.CreatedAt
   119  }
   120  
   121  func (prr PullRequestReview) HiddenReason() string {
   122  	return ""
   123  }
   124  
   125  func (prr PullRequestReview) IsEdited() bool {
   126  	return prr.IncludesCreatedEdit
   127  }
   128  
   129  func (prr PullRequestReview) IsHidden() bool {
   130  	return false
   131  }
   132  
   133  func (prr PullRequestReview) Link() string {
   134  	return prr.URL
   135  }
   136  
   137  func (prr PullRequestReview) Reactions() ReactionGroups {
   138  	return prr.ReactionGroups
   139  }
   140  
   141  func (prr PullRequestReview) Status() string {
   142  	return prr.State
   143  }