This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
/
trace.go
165 lines (135 loc) · 3.9 KB
/
trace.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
package trace
import (
"context"
"fmt"
"regexp"
"github.com/profclems/glab/pkg/iostreams"
"github.com/profclems/glab/internal/glrepo"
"github.com/profclems/glab/pkg/prompt"
"github.com/profclems/glab/api"
"github.com/profclems/glab/commands/ci/ciutils"
"github.com/profclems/glab/commands/cmdutils"
"github.com/profclems/glab/pkg/git"
"github.com/profclems/glab/pkg/utils"
"github.com/AlecAivazis/survey/v2"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"github.com/xanzy/go-gitlab"
)
type TraceOpts struct {
Branch string
JobID int
BaseRepo func() (glrepo.Interface, error)
HTTPClient func() (*gitlab.Client, error)
IO *iostreams.IOStreams
}
func NewCmdTrace(f *cmdutils.Factory, runE func(traceOpts *TraceOpts) error) *cobra.Command {
opts := &TraceOpts{
IO: f.IO,
}
var pipelineCITraceCmd = &cobra.Command{
Use: "trace [<job-id>] [flags]",
Short: `Trace a CI job log in real time`,
Example: heredoc.Doc(`
$ glab ci trace
#=> interactively select a job to trace
$ glab ci trace 224356863
#=> trace job with id 224356863
`),
RunE: func(cmd *cobra.Command, args []string) error {
var err error
// support `-R, --repo` override
//
// NOTE: it is important to assign the BaseRepo and HTTPClient in RunE because
// they are overridden in a PersistentRun hook (when `-R, --repo` is specified)
// which runs before RunE is executed
opts.BaseRepo = f.BaseRepo
opts.HTTPClient = f.HttpClient
if len(args) != 0 {
opts.JobID = utils.StringToInt(args[0])
}
if opts.Branch == "" {
opts.Branch, err = git.CurrentBranch()
if err != nil {
return err
}
}
if runE != nil {
return runE(opts)
}
return TraceRun(opts)
},
}
pipelineCITraceCmd.Flags().StringVarP(&opts.Branch, "branch", "b", "", "Check pipeline status for a branch. (Default is the current branch)")
return pipelineCITraceCmd
}
func TraceRun(opts *TraceOpts) error {
apiClient, err := opts.HTTPClient()
if err != nil {
return err
}
repo, err := opts.BaseRepo()
if err != nil {
return err
}
if opts.JobID < 1 {
fmt.Fprintf(opts.IO.StdOut, "\nSearching for latest pipeline on %s...\n", opts.Branch)
pipeline, err := api.GetLastPipeline(apiClient, repo.FullName(), opts.Branch)
if err != nil {
return err
}
fmt.Fprintf(opts.IO.StdOut, "Getting jobs for pipeline %d...\n\n", pipeline.ID)
jobs, err := api.GetPipelineJobs(apiClient, pipeline.ID, repo.FullName())
if err != nil {
return err
}
var jobOptions []string
var selectedJob string
for _, job := range jobs {
jobOptions = append(jobOptions, fmt.Sprintf("%s (%d) - %s", job.Name, job.ID, job.Status))
}
promptOpts := &survey.Select{
Message: "Select pipeline job to trace:",
Options: jobOptions,
}
_ = prompt.AskOne(promptOpts, &selectedJob)
if selectedJob != "" {
re := regexp.MustCompile(`(?s)\((.*)\)`)
m := re.FindAllStringSubmatch(selectedJob, -1)
opts.JobID = utils.StringToInt(m[0][1])
} else if len(jobs) > 0 {
opts.JobID = jobs[0].ID
} else {
// use commit statuses to show external jobs
cs, err := api.GetCommitStatuses(apiClient, repo.FullName(), pipeline.SHA)
if err != nil {
return nil
}
c := opts.IO.Color()
fmt.Fprint(opts.IO.StdOut, "Getting external jobs...")
for _, status := range cs {
var s string
switch status.Status {
case "success":
s = c.Green(status.Status)
case "error":
s = c.Red(status.Status)
default:
s = c.Gray(status.Status)
}
fmt.Fprintf(opts.IO.StdOut, "(%s) %s\nURL: %s\n\n", s, c.Bold(status.Name), c.Gray(status.TargetURL))
}
return nil
}
}
job, err := api.GetPipelineJob(apiClient, opts.JobID, repo.FullName())
if err != nil {
return err
}
fmt.Fprintln(opts.IO.StdOut)
err = ciutils.RunTrace(context.Background(), apiClient, opts.IO.StdOut, repo.FullName(), job.Pipeline.Sha, job.Name)
if err != nil {
return err
}
return nil
}