Merge pull request #6779 from cli/fix-comments-author

Fix fetching issue/PR comments
This commit is contained in:
Mislav Marohnić 2022-12-23 17:23:44 +01:00 committed by GitHub
commit 6618baaf63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 37 additions and 36 deletions

View file

@ -11,18 +11,6 @@ func (issue *Issue) ExportData(fields []string) map[string]interface{} {
for _, f := range fields {
switch f {
case "author":
author := map[string]interface{}{
"is_bot": issue.Author.IsBot(),
}
if issue.Author.IsBot() {
author["login"] = "app/" + issue.Author.Login
} else {
author["login"] = issue.Author.Login
author["name"] = issue.Author.Name
author["id"] = issue.Author.ID
}
data[f] = author
case "comments":
data[f] = issue.Comments.Nodes
case "assignees":
@ -46,18 +34,6 @@ func (pr *PullRequest) ExportData(fields []string) map[string]interface{} {
for _, f := range fields {
switch f {
case "author":
author := map[string]interface{}{
"is_bot": pr.Author.IsBot(),
}
if pr.Author.IsBot() {
author["login"] = "app/" + pr.Author.Login
} else {
author["login"] = pr.Author.Login
author["name"] = pr.Author.Name
author["id"] = pr.Author.ID
}
data[f] = author
case "headRepository":
data[f] = pr.HeadRepository
case "statusCheckRollup":

View file

@ -27,7 +27,7 @@ func (cs Comments) CurrentUserComments() []Comment {
type Comment struct {
ID string `json:"id"`
Author Author `json:"author"`
Author CommentAuthor `json:"author"`
AuthorAssociation string `json:"authorAssociation"`
Body string `json:"body"`
CreatedAt time.Time `json:"createdAt"`

View file

@ -1,6 +1,7 @@
package api
import (
"encoding/json"
"fmt"
"time"
@ -120,13 +121,35 @@ type Owner struct {
}
type Author struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Login string `json:"login"`
ID string
Name string
Login string
}
func (author *Author) IsBot() bool {
return author.ID == ""
func (author Author) MarshalJSON() ([]byte, error) {
if author.ID == "" {
return json.Marshal(map[string]interface{}{
"is_bot": true,
"login": "app/" + author.Login,
})
}
return json.Marshal(map[string]interface{}{
"is_bot": false,
"login": author.Login,
"id": author.ID,
"name": author.Name,
})
}
type CommentAuthor struct {
Login string `json:"login"`
// Unfortunately, there is no easy way to add "id" and "name" fields to this struct because it's being
// used in both shurcool-graphql type queries and string-based queries where the response gets parsed
// by an ordinary JSON decoder that doesn't understand "graphql" directives via struct tags.
// User *struct {
// ID string
// Name string
// } `graphql:"... on User"`
}
// IssueCreate creates an issue in a GitHub repository

View file

@ -24,7 +24,7 @@ var issueComments = shortenQuery(`
comments(first: 100) {
nodes {
id,
author{login},
author{login,...on User{id,name}},
authorAssociation,
body,
createdAt,
@ -43,7 +43,7 @@ var issueComments = shortenQuery(`
var issueCommentLast = shortenQuery(`
comments(last: 1) {
nodes {
author{login},
author{login,...on User{id,name}},
authorAssociation,
body,
createdAt,

View file

@ -324,8 +324,8 @@ func Test_commentRun(t *testing.T) {
ID: "ISSUE-ID",
URL: "https://github.com/OWNER/REPO/issues/123",
Comments: api.Comments{Nodes: []api.Comment{
{ID: "id1", Author: api.Author{Login: "octocat"}, URL: "https://github.com/OWNER/REPO/issues/123#issuecomment-111", ViewerDidAuthor: true},
{ID: "id2", Author: api.Author{Login: "monalisa"}, URL: "https://github.com/OWNER/REPO/issues/123#issuecomment-222"},
{ID: "id1", Author: api.CommentAuthor{Login: "octocat"}, URL: "https://github.com/OWNER/REPO/issues/123#issuecomment-111", ViewerDidAuthor: true},
{ID: "id2", Author: api.CommentAuthor{Login: "monalisa"}, URL: "https://github.com/OWNER/REPO/issues/123#issuecomment-222"},
}},
}, ghrepo.New("OWNER", "REPO"), nil
}

View file

@ -153,6 +153,8 @@ func findIssue(client *http.Client, baseRepoFn func() (ghrepo.Interface, error),
}
if fieldSet.Contains("comments") {
// FIXME: this re-fetches the comments connection even though the initial set of 100 were
// fetched in the previous request.
err = preloadIssueComments(client, repo, issue)
}
return issue, err

View file

@ -344,8 +344,8 @@ func Test_commentRun(t *testing.T) {
Number: 123,
URL: "https://github.com/OWNER/REPO/pull/123",
Comments: api.Comments{Nodes: []api.Comment{
{ID: "id1", Author: api.Author{Login: "octocat"}, URL: "https://github.com/OWNER/REPO/pull/123#issuecomment-111", ViewerDidAuthor: true},
{ID: "id2", Author: api.Author{Login: "monalisa"}, URL: "https://github.com/OWNER/REPO/pull/123#issuecomment-222"},
{ID: "id1", Author: api.CommentAuthor{Login: "octocat"}, URL: "https://github.com/OWNER/REPO/pull/123#issuecomment-111", ViewerDidAuthor: true},
{ID: "id2", Author: api.CommentAuthor{Login: "monalisa"}, URL: "https://github.com/OWNER/REPO/pull/123#issuecomment-222"},
}},
}, ghrepo.New("OWNER", "REPO"), nil
}