forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_activity.go
278 lines (250 loc) · 6.91 KB
/
get_activity.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
package cmd
import (
"io"
"time"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
tbl "github.com/jenkins-x/jx/pkg/jx/cmd/table"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
cmdutil "github.com/jenkins-x/jx/pkg/jx/cmd/util"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"
)
const (
indentation = " "
)
// GetActivityOptions containers the CLI options
type GetActivityOptions struct {
CommonOptions
Filter string
BuildNumber string
}
var (
get_activity_long = templates.LongDesc(`
Display the current activities for one more more projects.
`)
get_activity_example = templates.Examples(`
# List the current activities for all applications in the current team
jx get activities
# List the current activities for application 'foo'
jx get act foo
`)
)
// NewCmdGetActivity creates the new command for: jx get version
func NewCmdGetActivity(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &GetActivityOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "activities",
Short: "Display one or many Activities on projects",
Aliases: []string{"activity", "act"},
Long: get_activity_long,
Example: get_activity_example,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
cmdutil.CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Filter, "filter", "f", "", "Text to filter the pipeline names")
cmd.Flags().StringVarP(&options.BuildNumber, "build", "b", "", "The build number to filter on")
return cmd
}
// Run implements this command
func (o *GetActivityOptions) Run() error {
f := o.Factory
client, currentNs, err := f.CreateJXClient()
if err != nil {
return err
}
kubeClient, _, err := o.Factory.CreateClient()
if err != nil {
return err
}
ns, _, err := kube.GetDevNamespace(kubeClient, currentNs)
if err != nil {
return err
}
envList, err := client.JenkinsV1().Environments(ns).List(metav1.ListOptions{})
if err != nil {
return err
}
kube.SortEnvironments(envList.Items)
apisClient, err := f.CreateApiExtensionsClient()
if err != nil {
return err
}
err = kube.RegisterPipelineActivityCRD(apisClient)
if err != nil {
return err
}
list, err := client.JenkinsV1().PipelineActivities(ns).List(metav1.ListOptions{})
if err != nil {
return err
}
table := o.CreateTable()
table.SetColumnAlign(1, util.ALIGN_RIGHT)
table.SetColumnAlign(2, util.ALIGN_RIGHT)
table.AddRow("STEP", "STARTED AGO", "DURATION", "STATUS")
for _, activity := range list.Items {
if o.matches(&activity) {
spec := &activity.Spec
text := ""
version := activity.Spec.Version
if version != "" {
text = "Version: " + util.ColorInfo(version)
}
statusText := statusString(activity.Spec.Status)
if statusText == "" {
statusText = text
} else {
statusText += " " + text
}
table.AddRow(spec.Pipeline+" #"+spec.Build,
timeToString(spec.StartedTimestamp),
durationString(spec.StartedTimestamp, spec.CompletedTimestamp),
statusText)
indent := indentation
for _, step := range spec.Steps {
o.addStepRow(&table, &step, indent)
}
}
}
table.Render()
return nil
}
func (o *CommonOptions) addStepRow(table *tbl.Table, parent *v1.PipelineActivityStep, indent string) {
stage := parent.Stage
promote := parent.Promote
if stage != nil {
addStageRow(table, stage, indent)
} else if promote != nil {
addPromoteRow(table, promote, indent)
} else {
o.warnf("Unknown step kind %#v\n", parent)
}
}
func addStageRow(table *tbl.Table, stage *v1.StageActivityStep, indent string) {
name := "Stage"
if stage.Name != "" {
name = ""
}
addStepRowItem(table, &stage.CoreActivityStep, indent, name, "")
indent += indentation
for _, step := range stage.Steps {
addStepRowItem(table, &step, indent, "", "")
}
}
func addPromoteRow(table *tbl.Table, parent *v1.PromoteActivityStep, indent string) {
addStepRowItem(table, &parent.CoreActivityStep, indent, "Promote: "+parent.Environment, "")
indent += indentation
pullRequest := parent.PullRequest
update := parent.Update
if pullRequest != nil {
addStepRowItem(table, &pullRequest.CoreActivityStep, indent, "PullRequest", describePromotePullRequest(pullRequest))
}
if update != nil {
addStepRowItem(table, &update.CoreActivityStep, indent, "Update", describePromoteUpdate(update))
}
}
func addStepRowItem(table *tbl.Table, step *v1.CoreActivityStep, indent string, name string, description string) {
text := step.Description
if description != "" {
if text == "" {
text = description
} else {
text += description
}
}
textName := step.Name
if textName == "" {
textName = name
} else {
if name != "" {
textName = name + ":" + textName
}
}
table.AddRow(indent+textName,
timeToString(step.StartedTimestamp),
durationString(step.StartedTimestamp, step.CompletedTimestamp),
statusString(step.Status)+text)
}
func statusString(statusType v1.ActivityStatusType) string {
text := statusType.String()
switch statusType {
case v1.ActivityStatusTypeFailed, v1.ActivityStatusTypeError:
return util.ColorError(text)
case v1.ActivityStatusTypeSucceeded:
return util.ColorInfo(text)
case v1.ActivityStatusTypeRunning:
return util.ColorStatus(text)
}
return text
}
func describePromotePullRequest(promote *v1.PromotePullRequestStep) string {
description := ""
if promote.PullRequestURL != "" {
description += " PullRequest: " + util.ColorInfo(promote.PullRequestURL)
}
if promote.MergeCommitSHA != "" {
description += " Merge SHA: " + util.ColorInfo(promote.MergeCommitSHA)
}
return description
}
func describePromoteUpdate(promote *v1.PromoteUpdateStep) string {
description := ""
for _, status := range promote.Statuses {
url := status.URL
state := status.Status
if url != "" && state != "" {
description += " Status: " + pullRequestStatusString(state) + " at: " + util.ColorInfo(url)
}
}
return description
}
func pullRequestStatusString(text string) string {
title := strings.Title(text)
switch text {
case "success":
return util.ColorInfo(title)
case "error", "failed":
return util.ColorError(title)
default:
return util.ColorStatus(title)
}
}
func durationString(start *metav1.Time, end *metav1.Time) string {
if start == nil || end == nil {
return ""
}
return end.Sub(start.Time).Round(time.Second).String()
}
func timeToString(t *metav1.Time) string {
if t == nil {
return ""
}
now := &metav1.Time{
Time: time.Now(),
}
return durationString(t, now)
}
func (o *GetActivityOptions) matches(activity *v1.PipelineActivity) bool {
answer := true
filter := o.Filter
if filter != "" {
answer = strings.Contains(activity.Name, filter) || strings.Contains(activity.Spec.Pipeline, filter)
}
build := o.BuildNumber
if answer && build != "" {
answer = activity.Spec.Build == build
}
return answer
}