github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/issue/view/http.go (about)

     1  package view
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/ungtb10d/cli/v2/api"
     7  	"github.com/ungtb10d/cli/v2/internal/ghrepo"
     8  	"github.com/shurcooL/githubv4"
     9  )
    10  
    11  func preloadIssueComments(client *http.Client, repo ghrepo.Interface, issue *api.Issue) error {
    12  	type response struct {
    13  		Node struct {
    14  			Issue struct {
    15  				Comments *api.Comments `graphql:"comments(first: 100, after: $endCursor)"`
    16  			} `graphql:"...on Issue"`
    17  			PullRequest struct {
    18  				Comments *api.Comments `graphql:"comments(first: 100, after: $endCursor)"`
    19  			} `graphql:"...on PullRequest"`
    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 := api.NewClientFromHTTP(client)
    34  	for {
    35  		var query response
    36  		err := gql.Query(repo.RepoHost(), "CommentsForIssue", &query, variables)
    37  		if err != nil {
    38  			return err
    39  		}
    40  
    41  		comments := query.Node.Issue.Comments
    42  		if comments == nil {
    43  			comments = query.Node.PullRequest.Comments
    44  		}
    45  
    46  		issue.Comments.Nodes = append(issue.Comments.Nodes, comments.Nodes...)
    47  		if !comments.PageInfo.HasNextPage {
    48  			break
    49  		}
    50  		variables["endCursor"] = githubv4.String(comments.PageInfo.EndCursor)
    51  	}
    52  
    53  	issue.Comments.PageInfo.HasNextPage = false
    54  	return nil
    55  }