-
Notifications
You must be signed in to change notification settings - Fork 786
/
get_pipeline.go
242 lines (205 loc) · 5.3 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package cmd
import (
"errors"
"github.com/jenkins-x/jx/pkg/log"
"net/url"
"sort"
"github.com/jenkins-x/jx/pkg/prow"
"github.com/spf13/cobra"
"strings"
"time"
"github.com/jenkins-x/golang-jenkins"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/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 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 *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()
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, err := o.KubeClient()
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
}
o.dump(jenkins, job.Name, &table)
}
table.Render()
return nil
}
o.ProwOptions = prow.Options{
KubeClient: client,
NS: o.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 {
if err != nil {
return err
}
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(jenkins gojenkins.JenkinsClient, name string, table *table.Table) error {
job, err := jenkins.GetJob(name)
if err != nil {
return err
}
if job.Jobs != nil {
for _, child := range job.Jobs {
o.dump(jenkins, job.FullName+"/"+child.Name, table)
}
if len(job.Jobs) == 0 {
log.Warnf("Job %s has no children!\n", job.Name)
}
} else {
job.Url = switchJenkinsBaseURL(job.Url, jenkins.BaseURL())
last, err := jenkins.GetLastBuild(job)
if err != nil {
if jenkins.IsErrNotFound(err) {
if o.matchesFilter(&job) {
table.AddRow(job.FullName, job.Url, "", "Never Built", "")
}
} else {
log.Warnf("Failed to find last build for job %s: %s\n", 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
}
// switchJenkinsBaseURL sometimes a Jenkins server does not know its external URL so lets switch the base URL of the job
// URL to use the known working baseURL of the jenkins server
func switchJenkinsBaseURL(jobURL string, baseURL string) string {
if jobURL == "" {
return baseURL
}
if baseURL == "" {
return jobURL
}
u, err := url.Parse(jobURL)
if err != nil {
log.Warnf("failed to parse Jenkins Job URL %s due to: %s\n", jobURL, err)
return jobURL
}
u2, err := url.Parse(baseURL)
if err != nil {
log.Warnf("failed to parse Jenkins base URL %s due to: %s\n", baseURL, err)
return jobURL
}
u.Host = u2.Host
u.Scheme = u2.Scheme
return u.String()
}
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
}