Skip to content

Commit 9f101ff

Browse files
committed
Add comments to pr view
1 parent b2edf78 commit 9f101ff

File tree

7 files changed

+910
-42
lines changed

7 files changed

+910
-42
lines changed

api/queries_comments.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,39 @@ func CommentsForIssue(client *Client, repo ghrepo.Interface, issue *Issue) (*Com
6363

6464
return &Comments{Nodes: comments, TotalCount: len(comments)}, nil
6565
}
66+
67+
func CommentsForPullRequest(client *Client, repo ghrepo.Interface, pr *PullRequest) (*Comments, error) {
68+
type response struct {
69+
Repository struct {
70+
PullRequest struct {
71+
Comments Comments `graphql:"comments(first: 100, after: $endCursor)"`
72+
} `graphql:"pullRequest(number: $number)"`
73+
} `graphql:"repository(owner: $owner, name: $repo)"`
74+
}
75+
76+
variables := map[string]interface{}{
77+
"owner": githubv4.String(repo.RepoOwner()),
78+
"repo": githubv4.String(repo.RepoName()),
79+
"number": githubv4.Int(pr.Number),
80+
"endCursor": (*githubv4.String)(nil),
81+
}
82+
83+
gql := graphQLClient(client.http, repo.RepoHost())
84+
85+
var comments []Comment
86+
for {
87+
var query response
88+
err := gql.QueryNamed(context.Background(), "CommentsForPullRequest", &query, variables)
89+
if err != nil {
90+
return nil, err
91+
}
92+
93+
comments = append(comments, query.Repository.PullRequest.Comments.Nodes...)
94+
if !query.Repository.PullRequest.Comments.PageInfo.HasNextPage {
95+
break
96+
}
97+
variables["endCursor"] = githubv4.String(query.Repository.PullRequest.Comments.PageInfo.EndCursor)
98+
}
99+
100+
return &Comments{Nodes: comments, TotalCount: len(comments)}, nil
101+
}

api/queries_pr.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ type PullRequest struct {
137137
Milestone struct {
138138
Title string
139139
}
140+
Comments Comments
141+
ReactionGroups ReactionGroups
140142
}
141143

142144
type NotFoundError struct {
@@ -603,6 +605,30 @@ func PullRequestByNumber(client *Client, repo ghrepo.Interface, number int) (*Pu
603605
milestone{
604606
title
605607
}
608+
comments(last: 1) {
609+
nodes {
610+
author {
611+
login
612+
}
613+
authorAssociation
614+
body
615+
createdAt
616+
includesCreatedEdit
617+
reactionGroups {
618+
content
619+
users {
620+
totalCount
621+
}
622+
}
623+
}
624+
totalCount
625+
}
626+
reactionGroups {
627+
content
628+
users {
629+
totalCount
630+
}
631+
}
606632
}
607633
}
608634
}`
@@ -712,6 +738,30 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, hea
712738
milestone{
713739
title
714740
}
741+
comments(last: 1) {
742+
nodes {
743+
author {
744+
login
745+
}
746+
authorAssociation
747+
body
748+
createdAt
749+
includesCreatedEdit
750+
reactionGroups {
751+
content
752+
users {
753+
totalCount
754+
}
755+
}
756+
}
757+
totalCount
758+
}
759+
reactionGroups {
760+
content
761+
users {
762+
totalCount
763+
}
764+
}
715765
}
716766
}
717767
}

pkg/cmd/issue/view/view.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,13 @@ func printRawIssuePreview(out io.Writer, issue *api.Issue) error {
152152
func printRawIssueComments(out io.Writer, issue *api.Issue) error {
153153
var b strings.Builder
154154
for _, comment := range issue.Comments.Nodes {
155-
fmt.Fprint(&b, rawIssueComment(comment))
155+
fmt.Fprint(&b, formatRawIssueComment(comment))
156156
}
157157
fmt.Fprint(out, b.String())
158158
return nil
159159
}
160160

161-
func rawIssueComment(comment api.Comment) string {
161+
func formatRawIssueComment(comment api.Comment) string {
162162
var b strings.Builder
163163
fmt.Fprintf(&b, "author:\t%s\n", comment.Author.Login)
164164
fmt.Fprintf(&b, "association:\t%s\n", strings.ToLower(comment.AuthorAssociation))

0 commit comments

Comments
 (0)