github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/issue/view/http.go (about)

     1  package view
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  
     7  	"github.com/cli/cli/api"
     8  	"github.com/cli/cli/internal/ghinstance"
     9  	"github.com/cli/cli/internal/ghrepo"
    10  	"github.com/shurcooL/githubv4"
    11  	"github.com/shurcooL/graphql"
    12  )
    13  
    14  func preloadIssueComments(client *http.Client, repo ghrepo.Interface, issue *api.Issue) error {
    15  	type response struct {
    16  		Node struct {
    17  			Issue struct {
    18  				Comments api.Comments `graphql:"comments(first: 100, after: $endCursor)"`
    19  			} `graphql:"...on Issue"`
    20  		} `graphql:"node(id: $id)"`
    21  	}
    22  
    23  	variables := map[string]interface{}{
    24  		"id":        githubv4.ID(issue.ID),
    25  		"endCursor": (*githubv4.String)(nil),
    26  	}
    27  	if issue.Comments.PageInfo.HasNextPage {
    28  		variables["endCursor"] = githubv4.String(issue.Comments.PageInfo.EndCursor)
    29  	} else {
    30  		issue.Comments.Nodes = issue.Comments.Nodes[0:0]
    31  	}
    32  
    33  	gql := graphql.NewClient(ghinstance.GraphQLEndpoint(repo.RepoHost()), client)
    34  	for {
    35  		var query response
    36  		err := gql.QueryNamed(context.Background(), "CommentsForIssue", &query, variables)
    37  		if err != nil {
    38  			return err
    39  		}
    40  
    41  		issue.Comments.Nodes = append(issue.Comments.Nodes, query.Node.Issue.Comments.Nodes...)
    42  		if !query.Node.Issue.Comments.PageInfo.HasNextPage {
    43  			break
    44  		}
    45  		variables["endCursor"] = githubv4.String(query.Node.Issue.Comments.PageInfo.EndCursor)
    46  	}
    47  
    48  	issue.Comments.PageInfo.HasNextPage = false
    49  	return nil
    50  }