From 178fc2e51ead22bd0ef2904b4b5e3018d657914d Mon Sep 17 00:00:00 2001 From: nobe4 Date: Tue, 14 May 2024 07:57:38 +0200 Subject: [PATCH 1/3] feat: add json output for PR checks --- pkg/cmd/pr/checks/checks.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pkg/cmd/pr/checks/checks.go b/pkg/cmd/pr/checks/checks.go index 335226a9c..26931b992 100644 --- a/pkg/cmd/pr/checks/checks.go +++ b/pkg/cmd/pr/checks/checks.go @@ -20,10 +20,23 @@ import ( const defaultInterval time.Duration = 10 * time.Second +var prChecksFields = []string{ + "name", + "state", + "startedAt", + "completedAt", + "link", + "bucket", + "event", + "workflow", + "description", +} + type ChecksOptions struct { HttpClient func() (*http.Client, error) IO *iostreams.IOStreams Browser browser.Browser + Exporter cmdutil.Exporter Finder shared.PRFinder Detector fd.Detector @@ -97,6 +110,8 @@ func NewCmdChecks(f *cmdutil.Factory, runF func(*ChecksOptions) error) *cobra.Co cmd.Flags().IntVarP(&interval, "interval", "i", 10, "Refresh interval in seconds when using `--watch` flag") cmd.Flags().BoolVar(&opts.Required, "required", false, "Only show checks that are required") + cmdutil.AddJSONFlags(cmd, &opts.Exporter, prChecksFields) + return cmd } @@ -161,6 +176,10 @@ func checksRun(opts *ChecksOptions) error { return err } + if opts.Exporter != nil { + return opts.Exporter.Write(opts.IO, checks) + } + if opts.Watch { opts.IO.StartAlternateScreenBuffer() } else { From 8a1995c98d728dccb2e3dc5bae034c583ea646ed Mon Sep 17 00:00:00 2001 From: nobe4 Date: Tue, 14 May 2024 08:05:47 +0200 Subject: [PATCH 2/3] fix: rename fields list --- pkg/cmd/pr/checks/checks.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/pr/checks/checks.go b/pkg/cmd/pr/checks/checks.go index 26931b992..bbf2f453b 100644 --- a/pkg/cmd/pr/checks/checks.go +++ b/pkg/cmd/pr/checks/checks.go @@ -20,7 +20,7 @@ import ( const defaultInterval time.Duration = 10 * time.Second -var prChecksFields = []string{ +var prCheckFields = []string{ "name", "state", "startedAt", @@ -110,7 +110,7 @@ func NewCmdChecks(f *cmdutil.Factory, runF func(*ChecksOptions) error) *cobra.Co cmd.Flags().IntVarP(&interval, "interval", "i", 10, "Refresh interval in seconds when using `--watch` flag") cmd.Flags().BoolVar(&opts.Required, "required", false, "Only show checks that are required") - cmdutil.AddJSONFlags(cmd, &opts.Exporter, prChecksFields) + cmdutil.AddJSONFlags(cmd, &opts.Exporter, prCheckFields) return cmd } From ac5510362b68198786a4494a2e914ea4d25867d3 Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Tue, 14 May 2024 09:49:07 -0400 Subject: [PATCH 3/3] Implement ExportData to filter json fields In order to filter json fields, the `ExportData` interface needed to be implemented with logic that iterated on the selected fields. --- pkg/cmd/pr/checks/aggregate.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/cmd/pr/checks/aggregate.go b/pkg/cmd/pr/checks/aggregate.go index 40e34e79a..91cec4335 100644 --- a/pkg/cmd/pr/checks/aggregate.go +++ b/pkg/cmd/pr/checks/aggregate.go @@ -6,6 +6,7 @@ import ( "time" "github.com/cli/cli/v2/api" + "github.com/cli/cli/v2/pkg/cmdutil" ) type check struct { @@ -28,6 +29,10 @@ type checkCounts struct { Canceled int } +func (ch *check) ExportData(fields []string) map[string]interface{} { + return cmdutil.StructExportData(ch, fields) +} + func aggregateChecks(checkContexts []api.CheckContext, requiredChecks bool) (checks []check, counts checkCounts) { for _, c := range eliminateDuplicates(checkContexts) { if requiredChecks && !c.IsRequired {