From 86566d8187b492c882d74ec044b72fa1013acbab Mon Sep 17 00:00:00 2001 From: hirasawayuki Date: Sun, 16 Jan 2022 21:22:31 +0900 Subject: [PATCH] Add author information in `pr list` JSON output --- api/export_pr.go | 12 ++++++++++++ api/queries_issue.go | 9 ++++++--- api/query_builder.go | 2 +- api/query_builder_test.go | 2 +- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/api/export_pr.go b/api/export_pr.go index 18bce025b..708d855bb 100644 --- a/api/export_pr.go +++ b/api/export_pr.go @@ -34,6 +34,18 @@ 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": diff --git a/api/queries_issue.go b/api/queries_issue.go index 4146bfeaa..1266780fc 100644 --- a/api/queries_issue.go +++ b/api/queries_issue.go @@ -110,12 +110,15 @@ type Owner struct { } type Author struct { - // adding these breaks generated GraphQL requests - //ID string `json:"id,omitempty"` - //Name string `json:"name,omitempty"` + ID string `json:"id,omitempty"` + Name string `json:"name,omitempty"` Login string `json:"login"` } +func (author *Author) IsBot() bool { + return author.ID == "" +} + // IssueCreate creates an issue in a GitHub repository func IssueCreate(client *Client, repo *Repository, params map[string]interface{}) (*Issue, error) { query := ` diff --git a/api/query_builder.go b/api/query_builder.go index 88f8bfa4b..fccba4f47 100644 --- a/api/query_builder.go +++ b/api/query_builder.go @@ -200,7 +200,7 @@ func PullRequestGraphQL(fields []string) string { for _, field := range fields { switch field { case "author": - q = append(q, `author{login}`) + q = append(q, `author{login,...on User{id,name}}`) case "mergedBy": q = append(q, `mergedBy{login}`) case "headRepositoryOwner": diff --git a/api/query_builder_test.go b/api/query_builder_test.go index 7806f2d05..3c510d6da 100644 --- a/api/query_builder_test.go +++ b/api/query_builder_test.go @@ -21,7 +21,7 @@ func TestPullRequestGraphQL(t *testing.T) { { name: "fields with nested structures", fields: []string{"author", "assignees"}, - want: "author{login},assignees(first:100){nodes{id,login,name},totalCount}", + want: "author{login,...on User{id,name}},assignees(first:100){nodes{id,login,name},totalCount}", }, { name: "compressed query",