-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
view.go
664 lines (562 loc) · 18.7 KB
/
view.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
package view
import (
"archive/zip"
"bufio"
"bytes"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"time"
"unicode/utf16"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/browser"
"github.com/cli/cli/v2/internal/ghinstance"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/internal/text"
"github.com/cli/cli/v2/pkg/cmd/run/shared"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/spf13/cobra"
)
type RunLogCache struct {
cacheDir string
}
func (c RunLogCache) Exists(key string) (bool, error) {
_, err := os.Stat(c.filepath(key))
if err == nil {
return true, nil
}
if errors.Is(err, os.ErrNotExist) {
return false, nil
}
return false, fmt.Errorf("checking cache entry: %v", err)
}
func (c RunLogCache) Create(key string, content io.Reader) error {
if err := os.MkdirAll(c.cacheDir, 0755); err != nil {
return fmt.Errorf("creating cache directory: %v", err)
}
out, err := os.Create(c.filepath(key))
if err != nil {
return fmt.Errorf("creating cache entry: %v", err)
}
defer out.Close()
if _, err := io.Copy(out, content); err != nil {
return fmt.Errorf("writing cache entry: %v", err)
}
return nil
}
func (c RunLogCache) Open(key string) (*zip.ReadCloser, error) {
r, err := zip.OpenReader(c.filepath(key))
if err != nil {
return nil, fmt.Errorf("opening cache entry: %v", err)
}
return r, nil
}
func (c RunLogCache) filepath(key string) string {
return filepath.Join(c.cacheDir, fmt.Sprintf("run-log-%s.zip", key))
}
type ViewOptions struct {
HttpClient func() (*http.Client, error)
IO *iostreams.IOStreams
BaseRepo func() (ghrepo.Interface, error)
Browser browser.Browser
Prompter shared.Prompter
RunLogCache RunLogCache
RunID string
JobID string
Verbose bool
ExitStatus bool
Log bool
LogFailed bool
Web bool
Attempt uint64
Prompt bool
Exporter cmdutil.Exporter
Now func() time.Time
}
func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Command {
opts := &ViewOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
Prompter: f.Prompter,
Now: time.Now,
Browser: f.Browser,
}
cmd := &cobra.Command{
Use: "view [<run-id>]",
Short: "View a summary of a workflow run",
Long: heredoc.Docf(`
View a summary of a workflow run.
This command does not support authenticating via fine grained PATs
as it is not currently possible to create a PAT with the %[1]schecks:read%[1]s permission.
`, "`"),
Args: cobra.MaximumNArgs(1),
Example: heredoc.Doc(`
# Interactively select a run to view, optionally selecting a single job
$ gh run view
# View a specific run
$ gh run view 12345
# View a specific run with specific attempt number
$ gh run view 12345 --attempt 3
# View a specific job within a run
$ gh run view --job 456789
# View the full log for a specific job
$ gh run view --log --job 456789
# Exit non-zero if a run failed
$ gh run view 0451 --exit-status && echo "run pending or passed"
`),
RunE: func(cmd *cobra.Command, args []string) error {
// support `-R, --repo` override
opts.BaseRepo = f.BaseRepo
config, err := f.Config()
if err != nil {
return err
}
opts.RunLogCache = RunLogCache{
cacheDir: config.CacheDir(),
}
if len(args) == 0 && opts.JobID == "" {
if !opts.IO.CanPrompt() {
return cmdutil.FlagErrorf("run or job ID required when not running interactively")
} else {
opts.Prompt = true
}
} else if len(args) > 0 {
opts.RunID = args[0]
}
if opts.RunID != "" && opts.JobID != "" {
opts.RunID = ""
if opts.IO.CanPrompt() {
cs := opts.IO.ColorScheme()
fmt.Fprintf(opts.IO.ErrOut, "%s both run and job IDs specified; ignoring run ID\n", cs.WarningIcon())
}
}
if opts.Web && opts.Log {
return cmdutil.FlagErrorf("specify only one of --web or --log")
}
if opts.Log && opts.LogFailed {
return cmdutil.FlagErrorf("specify only one of --log or --log-failed")
}
if runF != nil {
return runF(opts)
}
return runView(opts)
},
}
cmd.Flags().BoolVarP(&opts.Verbose, "verbose", "v", false, "Show job steps")
// 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 run failed")
cmd.Flags().StringVarP(&opts.JobID, "job", "j", "", "View a specific job ID from a run")
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")
cmd.Flags().Uint64VarP(&opts.Attempt, "attempt", "a", 0, "The attempt number of the workflow run")
cmdutil.AddJSONFlags(cmd, &opts.Exporter, shared.SingleRunFields)
return cmd
}
func runView(opts *ViewOptions) error {
httpClient, err := opts.HttpClient()
if err != nil {
return fmt.Errorf("failed to create http client: %w", err)
}
client := api.NewClientFromHTTP(httpClient)
repo, err := opts.BaseRepo()
if err != nil {
return fmt.Errorf("failed to determine base repo: %w", err)
}
jobID := opts.JobID
runID := opts.RunID
attempt := opts.Attempt
var selectedJob *shared.Job
var run *shared.Run
var jobs []shared.Job
defer opts.IO.StopProgressIndicator()
if jobID != "" {
opts.IO.StartProgressIndicator()
selectedJob, err = shared.GetJob(client, repo, jobID)
opts.IO.StopProgressIndicator()
if err != nil {
return fmt.Errorf("failed to get job: %w", err)
}
// TODO once more stuff is merged, standardize on using ints
runID = fmt.Sprintf("%d", selectedJob.RunID)
}
cs := opts.IO.ColorScheme()
if opts.Prompt {
// TODO arbitrary limit
opts.IO.StartProgressIndicator()
runs, err := shared.GetRuns(client, repo, nil, 10)
opts.IO.StopProgressIndicator()
if err != nil {
return fmt.Errorf("failed to get runs: %w", err)
}
runID, err = shared.SelectRun(opts.Prompter, cs, runs.WorkflowRuns)
if err != nil {
return err
}
}
opts.IO.StartProgressIndicator()
run, err = shared.GetRun(client, repo, runID, attempt)
opts.IO.StopProgressIndicator()
if err != nil {
return fmt.Errorf("failed to get run: %w", err)
}
if shouldFetchJobs(opts) {
opts.IO.StartProgressIndicator()
jobs, err = shared.GetJobs(client, repo, run, attempt)
opts.IO.StopProgressIndicator()
if err != nil {
return err
}
}
if opts.Prompt && len(jobs) > 1 {
selectedJob, err = promptForJob(opts.Prompter, cs, jobs)
if err != nil {
return err
}
}
if err := opts.IO.StartPager(); err == nil {
defer opts.IO.StopPager()
} else {
fmt.Fprintf(opts.IO.ErrOut, "failed to start pager: %v\n", err)
}
if opts.Exporter != nil {
return opts.Exporter.Write(opts.IO, run)
}
if opts.Web {
url := run.URL
if selectedJob != nil {
url = selectedJob.URL + "?check_suite_focus=true"
}
if opts.IO.IsStdoutTTY() {
fmt.Fprintf(opts.IO.Out, "Opening %s in your browser.\n", text.DisplayURL(url))
}
return opts.Browser.Browse(url)
}
if selectedJob == nil && len(jobs) == 0 {
opts.IO.StartProgressIndicator()
jobs, err = shared.GetJobs(client, repo, run, attempt)
opts.IO.StopProgressIndicator()
if err != nil {
return fmt.Errorf("failed to get jobs: %w", err)
}
} else if selectedJob != nil {
jobs = []shared.Job{*selectedJob}
}
if opts.Log || opts.LogFailed {
if selectedJob != nil && selectedJob.Status != shared.Completed {
return fmt.Errorf("job %d is still in progress; logs will be available when it is complete", selectedJob.ID)
}
if run.Status != shared.Completed {
return fmt.Errorf("run %d is still in progress; logs will be available when it is complete", run.ID)
}
opts.IO.StartProgressIndicator()
runLogZip, err := getRunLog(opts.RunLogCache, httpClient, repo, run, attempt)
opts.IO.StopProgressIndicator()
if err != nil {
return fmt.Errorf("failed to get run log: %w", err)
}
defer runLogZip.Close()
attachRunLog(&runLogZip.Reader, jobs)
return displayRunLog(opts.IO.Out, jobs, opts.LogFailed)
}
prNumber := ""
number, err := shared.PullRequestForRun(client, repo, *run)
if err == nil {
prNumber = fmt.Sprintf(" %s#%d", ghrepo.FullName(repo), number)
}
var artifacts []shared.Artifact
if selectedJob == nil {
artifacts, err = shared.ListArtifacts(httpClient, repo, strconv.FormatInt(int64(run.ID), 10))
if err != nil {
return fmt.Errorf("failed to get artifacts: %w", err)
}
}
var annotations []shared.Annotation
for _, job := range jobs {
as, err := shared.GetAnnotations(client, repo, job)
if err != nil {
return fmt.Errorf("failed to get annotations: %w", err)
}
annotations = append(annotations, as...)
}
out := opts.IO.Out
fmt.Fprintln(out)
fmt.Fprintln(out, shared.RenderRunHeader(cs, *run, text.FuzzyAgo(opts.Now(), run.StartedTime()), prNumber, attempt))
fmt.Fprintln(out)
if len(jobs) == 0 && run.Conclusion == shared.Failure || run.Conclusion == shared.StartupFailure {
fmt.Fprintf(out, "%s %s\n",
cs.FailureIcon(),
cs.Bold("This run likely failed because of a workflow file issue."))
fmt.Fprintln(out)
fmt.Fprintf(out, "For more information, see: %s\n", cs.Bold(run.URL))
if opts.ExitStatus {
return cmdutil.SilentError
}
return nil
}
if selectedJob == nil {
fmt.Fprintln(out, cs.Bold("JOBS"))
fmt.Fprintln(out, shared.RenderJobs(cs, jobs, opts.Verbose))
} else {
fmt.Fprintln(out, shared.RenderJobs(cs, jobs, true))
}
if len(annotations) > 0 {
fmt.Fprintln(out)
fmt.Fprintln(out, cs.Bold("ANNOTATIONS"))
fmt.Fprintln(out, shared.RenderAnnotations(cs, annotations))
}
if selectedJob == nil {
if len(artifacts) > 0 {
fmt.Fprintln(out)
fmt.Fprintln(out, cs.Bold("ARTIFACTS"))
for _, a := range artifacts {
expiredBadge := ""
if a.Expired {
expiredBadge = cs.Gray(" (expired)")
}
fmt.Fprintf(out, "%s%s\n", a.Name, expiredBadge)
}
}
fmt.Fprintln(out)
if shared.IsFailureState(run.Conclusion) {
fmt.Fprintf(out, "To see what failed, try: gh run view %d --log-failed\n", run.ID)
} else if len(jobs) == 1 {
fmt.Fprintf(out, "For more information about the job, try: gh run view --job=%d\n", jobs[0].ID)
} else {
fmt.Fprintf(out, "For more information about a job, try: gh run view --job=<job-id>\n")
}
fmt.Fprintf(out, cs.Gray("View this run on GitHub: %s\n"), run.URL)
if opts.ExitStatus && shared.IsFailureState(run.Conclusion) {
return cmdutil.SilentError
}
} else {
fmt.Fprintln(out)
if shared.IsFailureState(selectedJob.Conclusion) {
fmt.Fprintf(out, "To see the logs for the failed steps, try: gh run view --log-failed --job=%d\n", selectedJob.ID)
} else {
fmt.Fprintf(out, "To see the full job log, try: gh run view --log --job=%d\n", selectedJob.ID)
}
fmt.Fprintf(out, cs.Gray("View this run on GitHub: %s\n"), run.URL)
if opts.ExitStatus && shared.IsFailureState(selectedJob.Conclusion) {
return cmdutil.SilentError
}
}
return nil
}
func shouldFetchJobs(opts *ViewOptions) bool {
if opts.Prompt {
return true
}
if opts.Exporter != nil {
for _, f := range opts.Exporter.Fields() {
if f == "jobs" {
return true
}
}
}
return false
}
func getLog(httpClient *http.Client, logURL string) (io.ReadCloser, error) {
req, err := http.NewRequest("GET", logURL, nil)
if err != nil {
return nil, err
}
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode == 404 {
return nil, errors.New("log not found")
} else if resp.StatusCode != 200 {
return nil, api.HandleHTTPError(resp)
}
return resp.Body, nil
}
func getRunLog(cache RunLogCache, httpClient *http.Client, repo ghrepo.Interface, run *shared.Run, attempt uint64) (*zip.ReadCloser, error) {
cacheKey := fmt.Sprintf("%d-%d", run.ID, run.StartedTime().Unix())
isCached, err := cache.Exists(cacheKey)
if err != nil {
return nil, err
}
if !isCached {
// Run log does not exist in cache so retrieve and store it
logURL := fmt.Sprintf("%srepos/%s/actions/runs/%d/logs",
ghinstance.RESTPrefix(repo.RepoHost()), ghrepo.FullName(repo), run.ID)
if attempt > 0 {
logURL = fmt.Sprintf("%srepos/%s/actions/runs/%d/attempts/%d/logs",
ghinstance.RESTPrefix(repo.RepoHost()), ghrepo.FullName(repo), run.ID, attempt)
}
resp, err := getLog(httpClient, logURL)
if err != nil {
return nil, err
}
defer resp.Close()
data, err := io.ReadAll(resp)
if err != nil {
return nil, err
}
respReader := bytes.NewReader(data)
// Check if the response is a valid zip format
_, err = zip.NewReader(respReader, respReader.Size())
if err != nil {
return nil, err
}
err = cache.Create(cacheKey, respReader)
if err != nil {
return nil, err
}
}
return cache.Open(cacheKey)
}
func promptForJob(prompter shared.Prompter, cs *iostreams.ColorScheme, jobs []shared.Job) (*shared.Job, error) {
candidates := []string{"View all jobs in this run"}
for _, job := range jobs {
symbol, _ := shared.Symbol(cs, job.Status, job.Conclusion)
candidates = append(candidates, fmt.Sprintf("%s %s", symbol, job.Name))
}
selected, err := prompter.Select("View a specific job in this run?", "", candidates)
if err != nil {
return nil, err
}
if selected > 0 {
return &jobs[selected-1], nil
}
// User wants to see all jobs
return nil, nil
}
const JOB_NAME_MAX_LENGTH = 90
func logFilenameRegexp(job shared.Job, step shared.Step) *regexp.Regexp {
// As described in https://github.com/cli/cli/issues/5011#issuecomment-1570713070, there are a number of steps
// the server can take when producing the downloaded zip file that can result in a mismatch between the job name
// and the filename in the zip including:
// * Removing characters in the job name that aren't allowed in file paths
// * Truncating names that are too long for zip files
// * Adding collision deduplicating numbers for jobs with the same name
//
// We are hesitant to duplicate all the server logic due to the fragility but it may be unavoidable. Currently, we:
// * Strip `/` which occur when composite action job names are constructed of the form `<JOB_NAME`> / <ACTION_NAME>`
// * Truncate long job names
//
sanitizedJobName := strings.ReplaceAll(job.Name, "/", "")
sanitizedJobName = truncateAsUTF16(sanitizedJobName, JOB_NAME_MAX_LENGTH)
re := fmt.Sprintf(`^%s\/%d_.*\.txt`, regexp.QuoteMeta(sanitizedJobName), step.Number)
return regexp.MustCompile(re)
}
/*
If you're reading this comment by necessity, I'm sorry and if you're reading it for fun, you're welcome, you weirdo.
What is the length of this string "a😅😅"? If you said 9 you'd be right. If you said 3 or 5 you might also be right!
Here's a summary:
"a" takes 1 byte (`\x61`)
"😅" takes 4 `bytes` (`\xF0\x9F\x98\x85`)
"a😅😅" therefore takes 9 `bytes`
In Go `len("a😅😅")` is 9 because the `len` builtin counts `bytes`
In Go `len([]rune("a😅😅"))` is 3 because each `rune` is 4 `bytes` so each character fits within a `rune`
In C# `"a😅😅".Length` is 5 because `.Length` counts `Char` objects, `Chars` hold 2 bytes, and "😅" takes 2 Chars.
But wait, what does C# have to do with anything? Well the server is running C#. Which server? The one that serves log
files to us in `.zip` format of course! When the server is constructing the zip file to avoid running afoul of a 260
byte zip file path length limitation, it applies transformations to various strings in order to limit their length.
In C#, the server truncates strings with this function:
public static string TruncateAfter(string str, int max)
{
string result = str.Length > max ? str.Substring(0, max) : str;
result = result.Trim();
return result;
}
This seems like it would be easy enough to replicate in Go but as we already discovered, the length of a string isn't
as obvious as it might seem. Since C# uses UTF-16 encoding for strings, and Go uses UTF-8 encoding and represents
characters by runes (which are an alias of int32) we cannot simply slice the string without any further consideration.
Instead, we need to encode the string as UTF-16 bytes, slice it and then decode it back to UTF-8.
Interestingly, in C# length and substring both act on the Char type so it's possible to slice into the middle of
a visual, "representable" character. For example we know `"a😅😅".Length` = 5 (1+2+2) and therefore Substring(0,4)
results in the final character being cleaved in two, resulting in "a😅�". Since our int32 runes are being encoded as
2 uint16 elements, we also mimic this behaviour by slicing into the UTF-16 encoded string.
Here's a program you can put into a dotnet playground to see how C# works:
using System;
public class Program {
public static void Main() {
string s = "a😅😅";
Console.WriteLine("{0} {1}", s.Length, s);
string t = TruncateAfter(s, 4);
Console.WriteLine("{0} {1}", t.Length, t);
}
public static string TruncateAfter(string str, int max) {
string result = str.Length > max ? str.Substring(0, max) : str;
return result.Trim();
}
}
This will output:
5 a😅😅
4 a😅�
*/
func truncateAsUTF16(str string, max int) string {
// Encode the string to UTF-16 to count code units
utf16Encoded := utf16.Encode([]rune(str))
if len(utf16Encoded) > max {
// Decode back to UTF-8 up to the max length
str = string(utf16.Decode(utf16Encoded[:max]))
}
return strings.TrimSpace(str)
}
// This function takes a zip file of logs and a list of jobs.
// Structure of zip file
//
// zip/
// ├── jobname1/
// │ ├── 1_stepname.txt
// │ ├── 2_anotherstepname.txt
// │ ├── 3_stepstepname.txt
// │ └── 4_laststepname.txt
// └── jobname2/
// ├── 1_stepname.txt
// └── 2_somestepname.txt
//
// It iterates through the list of jobs and tries to find the matching
// log in the zip file. If the matching log is found it is attached
// to the job.
func attachRunLog(rlz *zip.Reader, jobs []shared.Job) {
for i, job := range jobs {
for j, step := range job.Steps {
re := logFilenameRegexp(job, step)
for _, file := range rlz.File {
if re.MatchString(file.Name) {
jobs[i].Steps[j].Log = file
break
}
}
}
}
}
func displayRunLog(w io.Writer, jobs []shared.Job, failed bool) error {
for _, job := range jobs {
steps := job.Steps
sort.Sort(steps)
for _, step := range steps {
if failed && !shared.IsFailureState(step.Conclusion) {
continue
}
if step.Log == nil {
continue
}
prefix := fmt.Sprintf("%s\t%s\t", job.Name, step.Name)
f, err := step.Log.Open()
if err != nil {
return err
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fmt.Fprintf(w, "%s%s\n", prefix, scanner.Text())
}
f.Close()
}
}
return nil
}