forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.go
More file actions
250 lines (202 loc) · 5.92 KB
/
view.go
File metadata and controls
250 lines (202 loc) · 5.92 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package view
import (
"errors"
"fmt"
"io"
"net/http"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/api"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/cmd/run/shared"
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/iostreams"
"github.com/cli/cli/pkg/prompt"
"github.com/cli/cli/utils"
"github.com/spf13/cobra"
)
type ViewOptions struct {
HttpClient func() (*http.Client, error)
IO *iostreams.IOStreams
BaseRepo func() (ghrepo.Interface, error)
JobID string
Log bool
ExitStatus bool
Prompt bool
Now func() time.Time
}
func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Command {
opts := &ViewOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
Now: time.Now,
}
cmd := &cobra.Command{
Use: "view [<job-id>]",
Short: "View the summary or full logs of a workflow run's job",
Args: cobra.MaximumNArgs(1),
Hidden: true,
Example: heredoc.Doc(`
# Interactively select a run then job
$ gh job view
# Just view the logs for a job
$ gh job view 0451 --log
# Exit non-zero if a job failed
$ gh job view 0451 -e && echo "job pending or passed"
`),
RunE: func(cmd *cobra.Command, args []string) error {
// support `-R, --repo` override
opts.BaseRepo = f.BaseRepo
if len(args) > 0 {
opts.JobID = args[0]
} else if !opts.IO.CanPrompt() {
return &cmdutil.FlagError{Err: errors.New("job ID required when not running interactively")}
} else {
opts.Prompt = true
}
if runF != nil {
return runF(opts)
}
return runView(opts)
},
}
cmd.Flags().BoolVarP(&opts.Log, "log", "l", false, "Print full logs for job")
// TODO should we try and expose pending via another exit code?
cmd.Flags().BoolVar(&opts.ExitStatus, "exit-status", false, "Exit with non-zero status if job failed")
return cmd
}
func runView(opts *ViewOptions) error {
c, err := opts.HttpClient()
if err != nil {
return fmt.Errorf("failed to create http client: %w", err)
}
client := api.NewClientFromHTTP(c)
repo, err := opts.BaseRepo()
if err != nil {
return fmt.Errorf("failed to determine base repo: %w", err)
}
out := opts.IO.Out
cs := opts.IO.ColorScheme()
jobID := opts.JobID
if opts.Prompt {
runID, err := shared.PromptForRun(cs, client, repo)
if err != nil {
return err
}
// TODO I'd love to overwrite the result of the prompt since it adds visual noise but I'm not sure
// the cleanest way to do that.
fmt.Fprintln(out)
opts.IO.StartProgressIndicator()
defer opts.IO.StopProgressIndicator()
run, err := shared.GetRun(client, repo, runID)
if err != nil {
return fmt.Errorf("failed to get run: %w", err)
}
opts.IO.StopProgressIndicator()
jobID, err = promptForJob(*opts, client, repo, *run)
if err != nil {
return err
}
fmt.Fprintln(out)
}
opts.IO.StartProgressIndicator()
job, err := getJob(client, repo, jobID)
if err != nil {
return fmt.Errorf("failed to get job: %w", err)
}
if opts.Log {
r, err := client.JobLog(repo, jobID)
if err != nil {
return err
}
opts.IO.StopProgressIndicator()
opts.IO.DetectTerminalTheme()
err = opts.IO.StartPager()
if err != nil {
return err
}
defer opts.IO.StopPager()
if _, err := io.Copy(opts.IO.Out, r); err != nil {
return fmt.Errorf("failed to read log: %w", err)
}
if opts.ExitStatus && shared.IsFailureState(job.Conclusion) {
return cmdutil.SilentError
}
return nil
}
annotations, err := shared.GetAnnotations(client, repo, *job)
opts.IO.StopProgressIndicator()
if err != nil {
return fmt.Errorf("failed to get annotations: %w", err)
}
elapsed := job.CompletedAt.Sub(job.StartedAt)
elapsedStr := fmt.Sprintf(" in %s", elapsed)
if elapsed < 0 {
elapsedStr = ""
}
symbol, symColor := shared.Symbol(cs, job.Status, job.Conclusion)
fmt.Fprintf(out, "%s (ID %s)\n", cs.Bold(job.Name), cs.Cyanf("%d", job.ID))
fmt.Fprintf(out, "%s %s ago%s\n",
symColor(symbol),
utils.FuzzyAgoAbbr(opts.Now(), job.StartedAt),
elapsedStr)
fmt.Fprintln(out)
for _, step := range job.Steps {
stepSym, stepSymColor := shared.Symbol(cs, step.Status, step.Conclusion)
fmt.Fprintf(out, "%s %s\n",
stepSymColor(stepSym),
step.Name)
}
if len(annotations) > 0 {
fmt.Fprintln(out)
fmt.Fprintln(out, cs.Bold("ANNOTATIONS"))
for _, a := range annotations {
fmt.Fprintf(out, "%s %s\n", shared.AnnotationSymbol(cs, a), a.Message)
fmt.Fprintln(out, cs.Grayf("%s#%d\n", a.Path, a.StartLine))
}
}
fmt.Fprintln(out)
fmt.Fprintf(out, "To see the full logs for this job, try: gh job view %s --log\n", jobID)
fmt.Fprintf(out, cs.Gray("View this job on GitHub: %s\n"), job.URL)
if opts.ExitStatus && shared.IsFailureState(job.Conclusion) {
return cmdutil.SilentError
}
return nil
}
func getJob(client *api.Client, repo ghrepo.Interface, jobID string) (*shared.Job, error) {
path := fmt.Sprintf("repos/%s/actions/jobs/%s", ghrepo.FullName(repo), jobID)
var result shared.Job
err := client.REST(repo.RepoHost(), "GET", path, nil, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func promptForJob(opts ViewOptions, client *api.Client, repo ghrepo.Interface, run shared.Run) (string, error) {
cs := opts.IO.ColorScheme()
jobs, err := shared.GetJobs(client, repo, run)
if err != nil {
return "", err
}
if len(jobs) == 1 {
return fmt.Sprintf("%d", jobs[0].ID), nil
}
var selected int
candidates := []string{}
for _, job := range jobs {
symbol, symColor := shared.Symbol(cs, job.Status, job.Conclusion)
candidates = append(candidates, fmt.Sprintf("%s %s", symColor(symbol), job.Name))
}
// TODO consider custom filter so it's fuzzier. right now matches start anywhere in string but
// become contiguous
err = prompt.SurveyAskOne(&survey.Select{
Message: "Select a job to view",
Options: candidates,
PageSize: 10,
}, &selected)
if err != nil {
return "", err
}
return fmt.Sprintf("%d", jobs[selected].ID), nil
}