X Tutup
Skip to content

Commit a516ee6

Browse files
committed
Add issue status --json support
1 parent 3f22e3b commit a516ee6

File tree

4 files changed

+48
-25
lines changed

4 files changed

+48
-25
lines changed

api/export_pr.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ func (pr *PullRequest) ExportData(fields []string) *map[string]interface{} {
8282
return &data
8383
}
8484

85+
func ExportIssues(issues []Issue, fields []string) *[]interface{} {
86+
data := make([]interface{}, len(issues))
87+
for i, issue := range issues {
88+
data[i] = issue.ExportData(fields)
89+
}
90+
return &data
91+
}
92+
8593
func ExportPRs(prs []PullRequest, fields []string) *[]interface{} {
8694
data := make([]interface{}, len(prs))
8795
for i, pr := range prs {

api/queries_issue.go

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,6 @@ type Author struct {
102102
Login string `json:"login"`
103103
}
104104

105-
const fragments = `
106-
fragment issue on Issue {
107-
number
108-
title
109-
url
110-
state
111-
updatedAt
112-
labels(first: 100) {
113-
nodes {
114-
name
115-
}
116-
totalCount
117-
}
118-
}
119-
`
120-
121105
// IssueCreate creates an issue in a GitHub repository
122106
func IssueCreate(client *Client, repo *Repository, params map[string]interface{}) (*Issue, error) {
123107
query := `
@@ -153,7 +137,12 @@ func IssueCreate(client *Client, repo *Repository, params map[string]interface{}
153137
return &result.CreateIssue.Issue, nil
154138
}
155139

156-
func IssueStatus(client *Client, repo ghrepo.Interface, currentUsername string) (*IssuesPayload, error) {
140+
type IssueStatusOptions struct {
141+
Username string
142+
Fields []string
143+
}
144+
145+
func IssueStatus(client *Client, repo ghrepo.Interface, options IssueStatusOptions) (*IssuesPayload, error) {
157146
type response struct {
158147
Repository struct {
159148
Assigned struct {
@@ -172,6 +161,7 @@ func IssueStatus(client *Client, repo ghrepo.Interface, currentUsername string)
172161
}
173162
}
174163

164+
fragments := fmt.Sprintf("fragment issue on Issue{%s}", PullRequestGraphQL(options.Fields))
175165
query := fragments + `
176166
query IssueStatus($owner: String!, $repo: String!, $viewer: String!, $per_page: Int = 10) {
177167
repository(owner: $owner, name: $repo) {
@@ -200,7 +190,7 @@ func IssueStatus(client *Client, repo ghrepo.Interface, currentUsername string)
200190
variables := map[string]interface{}{
201191
"owner": repo.RepoOwner(),
202192
"repo": repo.RepoName(),
203-
"viewer": currentUsername,
193+
"viewer": options.Username,
204194
}
205195

206196
var resp response

pkg/cmd/issue/list/list.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,7 @@ func listRun(opts *ListOptions) error {
155155
defer opts.IO.StopPager()
156156

157157
if opts.Export != nil {
158-
data := make([]interface{}, len(listResult.Issues))
159-
for i, issue := range listResult.Issues {
160-
data[i] = issue.ExportData(opts.Export.Fields)
161-
}
162-
return opts.Export.Write(opts.IO.Out, &data, opts.IO.ColorEnabled())
158+
return opts.Export.Write(opts.IO.Out, api.ExportIssues(listResult.Issues, opts.Export.Fields), opts.IO.ColorEnabled())
163159
}
164160

165161
if isTerminal {

pkg/cmd/issue/status/status.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ type StatusOptions struct {
1919
Config func() (config.Config, error)
2020
IO *iostreams.IOStreams
2121
BaseRepo func() (ghrepo.Interface, error)
22+
23+
Export *cmdutil.ExportFormat
2224
}
2325

2426
func NewCmdStatus(f *cmdutil.Factory, runF func(*StatusOptions) error) *cobra.Command {
@@ -43,9 +45,20 @@ func NewCmdStatus(f *cmdutil.Factory, runF func(*StatusOptions) error) *cobra.Co
4345
},
4446
}
4547

48+
cmdutil.AddJSONFlags(cmd, &opts.Export, api.IssueFields)
49+
4650
return cmd
4751
}
4852

53+
var defaultFields = []string{
54+
"number",
55+
"title",
56+
"url",
57+
"state",
58+
"updatedAt",
59+
"labels",
60+
}
61+
4962
func statusRun(opts *StatusOptions) error {
5063
httpClient, err := opts.HttpClient()
5164
if err != nil {
@@ -63,17 +76,33 @@ func statusRun(opts *StatusOptions) error {
6376
return err
6477
}
6578

66-
issuePayload, err := api.IssueStatus(apiClient, baseRepo, currentUser)
79+
options := api.IssueStatusOptions{
80+
Username: currentUser,
81+
Fields: defaultFields,
82+
}
83+
if opts.Export != nil {
84+
options.Fields = opts.Export.Fields
85+
}
86+
issuePayload, err := api.IssueStatus(apiClient, baseRepo, options)
6787
if err != nil {
6888
return err
6989
}
7090

7191
err = opts.IO.StartPager()
7292
if err != nil {
73-
return err
93+
fmt.Fprintf(opts.IO.ErrOut, "error starting pager: %v\n", err)
7494
}
7595
defer opts.IO.StopPager()
7696

97+
if opts.Export != nil {
98+
data := map[string]interface{}{
99+
"createdBy": api.ExportIssues(issuePayload.Authored.Issues, opts.Export.Fields),
100+
"assigned": api.ExportIssues(issuePayload.Assigned.Issues, opts.Export.Fields),
101+
"mentioned": api.ExportIssues(issuePayload.Mentioned.Issues, opts.Export.Fields),
102+
}
103+
return opts.Export.Write(opts.IO.Out, &data, opts.IO.ColorEnabled())
104+
}
105+
77106
out := opts.IO.Out
78107

79108
fmt.Fprintln(out, "")

0 commit comments

Comments
 (0)
X Tutup