Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/cmd/run/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type ListOptions struct {
BaseRepo func() (ghrepo.Interface, error)

PlainOutput bool
Exporter cmdutil.Exporter

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

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

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

if opts.Exporter != nil {
return opts.Exporter.Write(opts.IO, runs)
}

tp := utils.NewTablePrinter(opts.IO)

cs := opts.IO.ColorScheme()
Expand Down
40 changes: 40 additions & 0 deletions pkg/cmd/run/shared/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/url"
"reflect"
"strings"
"time"

Expand Down Expand Up @@ -42,6 +43,20 @@ type Status string
type Conclusion string
type Level string

var RunFields = []string{
Comment thread
cristiand391 marked this conversation as resolved.
"name",
"headBranch",
"headSha",
"createdAt",
"updatedAt",
"status",
"conclusion",
"event",
"databaseId",
"workflowDatabaseId",
"url",
}

type Run struct {
Name string
CreatedAt time.Time `json:"created_at"`
Expand All @@ -50,6 +65,7 @@ type Run struct {
Conclusion Conclusion
Event string
ID int64
WorkflowID int64 `json:"workflow_id"`
HeadBranch string `json:"head_branch"`
JobsURL string `json:"jobs_url"`
HeadCommit Commit `json:"head_commit"`
Expand Down Expand Up @@ -78,6 +94,30 @@ func (r Run) CommitMsg() string {
}
}

func (r *Run) ExportData(fields []string) map[string]interface{} {
v := reflect.ValueOf(r).Elem()
fieldByName := func(v reflect.Value, field string) reflect.Value {
return v.FieldByNameFunc(func(s string) bool {
return strings.EqualFold(field, s)
})
}
data := map[string]interface{}{}

for _, f := range fields {
switch f {
case "databaseId":
data[f] = r.ID
case "workflowDatabaseId":
data[f] = r.WorkflowID
default:
sf := fieldByName(v, f)
data[f] = sf.Interface()
}
}

return data
}

type Job struct {
ID int64
Status Status
Expand Down
8 changes: 7 additions & 1 deletion pkg/cmd/run/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ type ViewOptions struct {
LogFailed bool
Web bool

Prompt bool
Prompt bool
Exporter cmdutil.Exporter

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

return cmd
}
Expand Down Expand Up @@ -228,6 +230,10 @@ func runView(opts *ViewOptions) error {
}
}

if opts.Exporter != nil {
return opts.Exporter.Write(opts.IO, run)
}

if opts.Web {
url := run.URL
if selectedJob != nil {
Expand Down