-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
list.go
160 lines (130 loc) · 3.7 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package list
import (
"fmt"
"net/http"
"time"
"github.com/cli/cli/api"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/cmd/run/shared"
workflowShared "github.com/cli/cli/pkg/cmd/workflow/shared"
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/iostreams"
"github.com/cli/cli/utils"
"github.com/spf13/cobra"
)
const (
defaultLimit = 20
)
type ListOptions struct {
IO *iostreams.IOStreams
HttpClient func() (*http.Client, error)
BaseRepo func() (ghrepo.Interface, error)
PlainOutput bool
Limit int
WorkflowSelector string
}
func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command {
opts := &ListOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
}
cmd := &cobra.Command{
Use: "list",
Short: "List recent workflow runs",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
// support `-R, --repo` override
opts.BaseRepo = f.BaseRepo
terminal := opts.IO.IsStdoutTTY() && opts.IO.IsStdinTTY()
opts.PlainOutput = !terminal
if opts.Limit < 1 {
return &cmdutil.FlagError{Err: fmt.Errorf("invalid limit: %v", opts.Limit)}
}
if runF != nil {
return runF(opts)
}
return listRun(opts)
},
}
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")
return cmd
}
func listRun(opts *ListOptions) error {
baseRepo, err := opts.BaseRepo()
if err != nil {
return fmt.Errorf("failed to determine base repo: %w", err)
}
c, err := opts.HttpClient()
if err != nil {
return fmt.Errorf("failed to create http client: %w", err)
}
client := api.NewClientFromHTTP(c)
var runs []shared.Run
var workflow *workflowShared.Workflow
opts.IO.StartProgressIndicator()
if opts.WorkflowSelector != "" {
states := []workflowShared.WorkflowState{workflowShared.Active}
workflow, err = workflowShared.ResolveWorkflow(
opts.IO, client, baseRepo, false, opts.WorkflowSelector, states)
if err == nil {
runs, err = shared.GetRunsByWorkflow(client, baseRepo, opts.Limit, workflow.ID)
}
} else {
runs, err = shared.GetRuns(client, baseRepo, opts.Limit)
}
opts.IO.StopProgressIndicator()
if err != nil {
return fmt.Errorf("failed to get runs: %w", err)
}
tp := utils.NewTablePrinter(opts.IO)
cs := opts.IO.ColorScheme()
if len(runs) == 0 {
if !opts.PlainOutput {
fmt.Fprintln(opts.IO.ErrOut, "No runs found")
}
return nil
}
out := opts.IO.Out
if !opts.PlainOutput {
tp.AddField("STATUS", nil, nil)
tp.AddField("NAME", nil, nil)
tp.AddField("WORKFLOW", nil, nil)
tp.AddField("BRANCH", nil, nil)
tp.AddField("EVENT", nil, nil)
tp.AddField("ID", nil, nil)
tp.AddField("ELAPSED", nil, nil)
tp.AddField("AGE", nil, nil)
tp.EndRow()
}
for _, run := range runs {
if opts.PlainOutput {
tp.AddField(string(run.Status), nil, nil)
tp.AddField(string(run.Conclusion), nil, nil)
} else {
symbol, symbolColor := shared.Symbol(cs, run.Status, run.Conclusion)
tp.AddField(symbol, nil, symbolColor)
}
tp.AddField(run.CommitMsg(), nil, cs.Bold)
tp.AddField(run.Name, nil, nil)
tp.AddField(run.HeadBranch, nil, cs.Bold)
tp.AddField(string(run.Event), nil, nil)
tp.AddField(fmt.Sprintf("%d", run.ID), nil, cs.Cyan)
elapsed := run.UpdatedAt.Sub(run.CreatedAt)
if elapsed < 0 {
elapsed = 0
}
tp.AddField(elapsed.String(), nil, nil)
tp.AddField(utils.FuzzyAgoAbbr(time.Now(), run.CreatedAt), nil, nil)
tp.EndRow()
}
err = tp.Render()
if err != nil {
return err
}
if !opts.PlainOutput {
fmt.Fprintln(out)
fmt.Fprintln(out, "For details on a run, try: gh run view <run-id>")
}
return nil
}