-
Notifications
You must be signed in to change notification settings - Fork 785
/
get_pipeline.go
222 lines (185 loc) · 4.93 KB
/
get_pipeline.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
package get
import (
"errors"
"sort"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/jenkins"
gojenkins "github.com/jenkins-x/golang-jenkins"
"github.com/jenkins-x/jx/v2/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/prow"
"github.com/spf13/cobra"
"strings"
"time"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/table"
)
// GetPipelineOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
// referencing the cmd.Flags()
type GetPipelineOptions struct {
GetOptions
JenkinsSelector opts.JenkinsSelectorOptions
ProwOptions prow.Options
}
var (
getPipelineLong = templates.LongDesc(`
Display one or more pipelines.
`)
getPipelineExample = templates.Examples(`
# list all pipelines
jx get pipeline
# Lists all the pipelines in a custom Jenkins App
jx get pipeline -m
`)
)
// NewCmdGetPipeline creates the command
func NewCmdGetPipeline(commonOpts *opts.CommonOptions) *cobra.Command {
options := &GetPipelineOptions{
GetOptions: GetOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "pipelines [flags]",
Short: "Display one or more Pipelines",
Long: getPipelineLong,
Example: getPipelineExample,
Aliases: []string{"pipe", "pipes", "pipeline"},
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
options.AddGetFlags(cmd)
cmd.Flags().BoolVarP(&options.JenkinsSelector.UseCustomJenkins, "custom", "m", false, "List the pipelines in custom Jenkins App instead of the default execution engine in Jenkins X")
cmd.Flags().StringVarP(&options.JenkinsSelector.CustomJenkinsName, "name", "n", "", "The name of the custom Jenkins App if you don't wish to list the pipelines in the default execution engine in Jenkins X")
return cmd
}
// Run implements this command
func (o *GetPipelineOptions) Run() error {
jo := &o.JenkinsSelector
if jo.CustomJenkinsName != "" {
jo.UseCustomJenkins = true
}
_, _, err := o.JXClient()
if err != nil {
return err
}
client, currentNamespace, err := o.KubeClientAndNamespace()
if err != nil {
return err
}
isProw, err := o.IsProw()
if err != nil {
return err
}
if jo.UseCustomJenkins || !isProw {
jenkins, err := o.CreateCustomJenkinsClient(jo)
if err != nil {
return err
}
jobs, err := jenkins.GetJobs()
if err != nil {
return err
}
if len(jobs) == 0 {
return outputEmptyListWarning(o.Out)
}
if o.Output != "" {
return o.renderResult(jobs, o.Output)
}
table := createTable(o)
for _, j := range jobs {
job, err := jenkins.GetJob(j.Name)
if err != nil {
return err
}
err = o.dump(jenkins, job.Name, &table)
if err != nil {
return err
}
}
table.Render()
return nil
}
o.ProwOptions = prow.Options{
KubeClient: client,
NS: currentNamespace,
}
names, err := o.ProwOptions.GetReleaseJobs()
if err != nil {
return err
}
if len(names) == 0 {
return errors.New("no pipelines found")
}
sort.Strings(names)
if len(names) == 0 {
return outputEmptyListWarning(o.Out)
}
if o.Output != "" {
return o.renderResult(names, o.Output)
}
table := createTable(o)
for _, j := range names {
table.AddRow(j, "N/A", "N/A", "N/A", "N/A")
}
table.Render()
return nil
}
func createTable(o *GetPipelineOptions) table.Table {
table := o.CreateTable()
table.AddRow("Name", "URL", "LAST_BUILD", "STATUS", "DURATION")
return table
}
func (o *GetPipelineOptions) dump(jenkinsClient gojenkins.JenkinsClient, name string, table *table.Table) error {
job, err := jenkinsClient.GetJob(name)
if err != nil {
return err
}
if job.Jobs != nil {
for _, child := range job.Jobs {
//Todo: Recursive call
o.dump(jenkinsClient, job.FullName+"/"+child.Name, table) //nolint:errcheck
}
if len(job.Jobs) == 0 {
log.Logger().Warnf("Job %s has no children!", job.Name)
}
} else {
job.Url = jenkins.SwitchJenkinsBaseURL(job.Url, jenkinsClient.BaseURL())
last, err := jenkinsClient.GetLastBuild(job)
if err != nil {
if jenkinsClient.IsErrNotFound(err) {
if o.matchesFilter(&job) {
table.AddRow(job.FullName, job.Url, "", "Never Built", "")
}
} else {
log.Logger().Warnf("Failed to find last build for job %s: %s", job.Name, err.Error())
}
return nil
}
if o.matchesFilter(&job) {
if last.Building {
table.AddRow(job.FullName, job.Url, "#"+last.Id, "Building", time.Duration(last.EstimatedDuration).String()+"(est.)")
} else {
table.AddRow(job.FullName, job.Url, "#"+last.Id, last.Result, time.Duration(last.Duration).String())
}
}
}
return nil
}
func (o *GetPipelineOptions) matchesFilter(job *gojenkins.Job) bool {
args := o.Args
if len(args) == 0 {
return true
}
name := job.FullName
for _, arg := range args {
if strings.Contains(name, arg) {
return true
}
}
return false
}