X Tutup
Skip to content

Commit ddaef8b

Browse files
cristiand391mislav
andauthored
Add --json export flag for workflow runs (cli#3869)
Co-authored-by: Mislav Marohnić <mislav@github.com>
1 parent 0607ce5 commit ddaef8b

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

pkg/cmd/run/list/list.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type ListOptions struct {
2525
BaseRepo func() (ghrepo.Interface, error)
2626

2727
PlainOutput bool
28+
Exporter cmdutil.Exporter
2829

2930
Limit int
3031
WorkflowSelector string
@@ -61,6 +62,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
6162

6263
cmd.Flags().IntVarP(&opts.Limit, "limit", "L", defaultLimit, "Maximum number of runs to fetch")
6364
cmd.Flags().StringVarP(&opts.WorkflowSelector, "workflow", "w", "", "Filter runs by workflow")
65+
cmdutil.AddJSONFlags(cmd, &opts.Exporter, shared.RunFields)
6466

6567
return cmd
6668
}
@@ -96,6 +98,10 @@ func listRun(opts *ListOptions) error {
9698
return fmt.Errorf("failed to get runs: %w", err)
9799
}
98100

101+
if opts.Exporter != nil {
102+
return opts.Exporter.Write(opts.IO, runs)
103+
}
104+
99105
tp := utils.NewTablePrinter(opts.IO)
100106

101107
cs := opts.IO.ColorScheme()

pkg/cmd/run/shared/shared.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"net/url"
8+
"reflect"
89
"strings"
910
"time"
1011

@@ -42,6 +43,20 @@ type Status string
4243
type Conclusion string
4344
type Level string
4445

46+
var RunFields = []string{
47+
"name",
48+
"headBranch",
49+
"headSha",
50+
"createdAt",
51+
"updatedAt",
52+
"status",
53+
"conclusion",
54+
"event",
55+
"databaseId",
56+
"workflowDatabaseId",
57+
"url",
58+
}
59+
4560
type Run struct {
4661
Name string
4762
CreatedAt time.Time `json:"created_at"`
@@ -50,6 +65,7 @@ type Run struct {
5065
Conclusion Conclusion
5166
Event string
5267
ID int64
68+
WorkflowID int64 `json:"workflow_id"`
5369
HeadBranch string `json:"head_branch"`
5470
JobsURL string `json:"jobs_url"`
5571
HeadCommit Commit `json:"head_commit"`
@@ -78,6 +94,30 @@ func (r Run) CommitMsg() string {
7894
}
7995
}
8096

97+
func (r *Run) ExportData(fields []string) map[string]interface{} {
98+
v := reflect.ValueOf(r).Elem()
99+
fieldByName := func(v reflect.Value, field string) reflect.Value {
100+
return v.FieldByNameFunc(func(s string) bool {
101+
return strings.EqualFold(field, s)
102+
})
103+
}
104+
data := map[string]interface{}{}
105+
106+
for _, f := range fields {
107+
switch f {
108+
case "databaseId":
109+
data[f] = r.ID
110+
case "workflowDatabaseId":
111+
data[f] = r.WorkflowID
112+
default:
113+
sf := fieldByName(v, f)
114+
data[f] = sf.Interface()
115+
}
116+
}
117+
118+
return data
119+
}
120+
81121
type Job struct {
82122
ID int64
83123
Status Status

pkg/cmd/run/view/view.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ type ViewOptions struct {
7878
LogFailed bool
7979
Web bool
8080

81-
Prompt bool
81+
Prompt bool
82+
Exporter cmdutil.Exporter
8283

8384
Now func() time.Time
8485
}
@@ -155,6 +156,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
155156
cmd.Flags().BoolVar(&opts.Log, "log", false, "View full log for either a run or specific job")
156157
cmd.Flags().BoolVar(&opts.LogFailed, "log-failed", false, "View the log for any failed steps in a run or specific job")
157158
cmd.Flags().BoolVarP(&opts.Web, "web", "w", false, "Open run in the browser")
159+
cmdutil.AddJSONFlags(cmd, &opts.Exporter, shared.RunFields)
158160

159161
return cmd
160162
}
@@ -228,6 +230,10 @@ func runView(opts *ViewOptions) error {
228230
}
229231
}
230232

233+
if opts.Exporter != nil {
234+
return opts.Exporter.Write(opts.IO, run)
235+
}
236+
231237
if opts.Web {
232238
url := run.URL
233239
if selectedJob != nil {

0 commit comments

Comments
 (0)
X Tutup