Add issue status --json support

This commit is contained in:
Mislav Marohnić 2021-04-13 21:26:13 +02:00
parent 3f22e3b353
commit a516ee6833
4 changed files with 48 additions and 25 deletions

View file

@ -82,6 +82,14 @@ func (pr *PullRequest) ExportData(fields []string) *map[string]interface{} {
return &data
}
func ExportIssues(issues []Issue, fields []string) *[]interface{} {
data := make([]interface{}, len(issues))
for i, issue := range issues {
data[i] = issue.ExportData(fields)
}
return &data
}
func ExportPRs(prs []PullRequest, fields []string) *[]interface{} {
data := make([]interface{}, len(prs))
for i, pr := range prs {

View file

@ -102,22 +102,6 @@ type Author struct {
Login string `json:"login"`
}
const fragments = `
fragment issue on Issue {
number
title
url
state
updatedAt
labels(first: 100) {
nodes {
name
}
totalCount
}
}
`
// IssueCreate creates an issue in a GitHub repository
func IssueCreate(client *Client, repo *Repository, params map[string]interface{}) (*Issue, error) {
query := `
@ -153,7 +137,12 @@ func IssueCreate(client *Client, repo *Repository, params map[string]interface{}
return &result.CreateIssue.Issue, nil
}
func IssueStatus(client *Client, repo ghrepo.Interface, currentUsername string) (*IssuesPayload, error) {
type IssueStatusOptions struct {
Username string
Fields []string
}
func IssueStatus(client *Client, repo ghrepo.Interface, options IssueStatusOptions) (*IssuesPayload, error) {
type response struct {
Repository struct {
Assigned struct {
@ -172,6 +161,7 @@ func IssueStatus(client *Client, repo ghrepo.Interface, currentUsername string)
}
}
fragments := fmt.Sprintf("fragment issue on Issue{%s}", PullRequestGraphQL(options.Fields))
query := fragments + `
query IssueStatus($owner: String!, $repo: String!, $viewer: String!, $per_page: Int = 10) {
repository(owner: $owner, name: $repo) {
@ -200,7 +190,7 @@ func IssueStatus(client *Client, repo ghrepo.Interface, currentUsername string)
variables := map[string]interface{}{
"owner": repo.RepoOwner(),
"repo": repo.RepoName(),
"viewer": currentUsername,
"viewer": options.Username,
}
var resp response

View file

@ -155,11 +155,7 @@ func listRun(opts *ListOptions) error {
defer opts.IO.StopPager()
if opts.Export != nil {
data := make([]interface{}, len(listResult.Issues))
for i, issue := range listResult.Issues {
data[i] = issue.ExportData(opts.Export.Fields)
}
return opts.Export.Write(opts.IO.Out, &data, opts.IO.ColorEnabled())
return opts.Export.Write(opts.IO.Out, api.ExportIssues(listResult.Issues, opts.Export.Fields), opts.IO.ColorEnabled())
}
if isTerminal {

View file

@ -19,6 +19,8 @@ type StatusOptions struct {
Config func() (config.Config, error)
IO *iostreams.IOStreams
BaseRepo func() (ghrepo.Interface, error)
Export *cmdutil.ExportFormat
}
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
},
}
cmdutil.AddJSONFlags(cmd, &opts.Export, api.IssueFields)
return cmd
}
var defaultFields = []string{
"number",
"title",
"url",
"state",
"updatedAt",
"labels",
}
func statusRun(opts *StatusOptions) error {
httpClient, err := opts.HttpClient()
if err != nil {
@ -63,17 +76,33 @@ func statusRun(opts *StatusOptions) error {
return err
}
issuePayload, err := api.IssueStatus(apiClient, baseRepo, currentUser)
options := api.IssueStatusOptions{
Username: currentUser,
Fields: defaultFields,
}
if opts.Export != nil {
options.Fields = opts.Export.Fields
}
issuePayload, err := api.IssueStatus(apiClient, baseRepo, options)
if err != nil {
return err
}
err = opts.IO.StartPager()
if err != nil {
return err
fmt.Fprintf(opts.IO.ErrOut, "error starting pager: %v\n", err)
}
defer opts.IO.StopPager()
if opts.Export != nil {
data := map[string]interface{}{
"createdBy": api.ExportIssues(issuePayload.Authored.Issues, opts.Export.Fields),
"assigned": api.ExportIssues(issuePayload.Assigned.Issues, opts.Export.Fields),
"mentioned": api.ExportIssues(issuePayload.Mentioned.Issues, opts.Export.Fields),
}
return opts.Export.Write(opts.IO.Out, &data, opts.IO.ColorEnabled())
}
out := opts.IO.Out
fmt.Fprintln(out, "")