/
get_pipeline.go
148 lines (122 loc) · 3.52 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
package get
import (
"fmt"
"sort"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/tekton"
"github.com/pkg/errors"
pipelineapi "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/jenkins-x/jx/v2/pkg/prow"
"github.com/spf13/cobra"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
table2 "github.com/jenkins-x/jx/v2/pkg/table"
)
// PipelineOptions 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 PipelineOptions struct {
Options
ProwOptions prow.Options
}
var (
getPipelineLong = templates.LongDesc(`
Display one or more pipelines.
`)
getPipelineExample = templates.Examples(`
# list all pipelines
jx get pipeline
`)
)
// NewCmdGetPipeline creates the command
func NewCmdGetPipeline(commonOpts *opts.CommonOptions) *cobra.Command {
options := &PipelineOptions{
Options: Options{
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)
return cmd
}
// Run implements this command
func (o *PipelineOptions) Run() error {
tektonClient, ns, err := o.TektonClient()
if err != nil {
return errors.Wrap(err, "could not create tekton client")
}
pipelines := tektonClient.TektonV1alpha1().PipelineRuns(ns)
prList, err := pipelines.List(metav1.ListOptions{})
if err != nil {
return errors.Wrapf(err, "failed to list PipelineRuns in namespace %s", ns)
}
if len(prList.Items) == 0 {
return errors.New(fmt.Sprintf("no PipelineRuns were found in namespace %s", ns))
}
var owner, repo, branch, context, buildNumber, status string
names := []string{}
m := map[string]*pipelineapi.PipelineRun{}
for k := range prList.Items {
pr := prList.Items[k]
status = "not completed"
if tekton.PipelineRunIsComplete(&pr) {
status = "completed"
}
labels := pr.Labels
if labels == nil {
continue
}
owner = labels[tekton.LabelOwner]
repo = labels[tekton.LabelRepo]
branch = labels[tekton.LabelBranch]
context = labels[tekton.LabelContext]
buildNumber = labels[tekton.LabelBuild]
if owner == "" {
log.Logger().Warnf("missing label %s on PipelineRun %s has labels %#v", tekton.LabelOwner, pr.Name, labels)
continue
}
if repo == "" {
log.Logger().Warnf("missing label %s on PipelineRun %s has labels %#v", tekton.LabelRepo, pr.Name, labels)
continue
}
if branch == "" {
log.Logger().Warnf("missing label %s on PipelineRun %s has labels %#v", tekton.LabelBranch, pr.Name, labels)
continue
}
name := fmt.Sprintf("%s/%s/%s #%s %s", owner, repo, branch, buildNumber, status)
if context != "" {
name = fmt.Sprintf("%s-%s", name, context)
}
names = append(names, name)
m[name] = &pr
}
sort.Strings(names)
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 *PipelineOptions) table2.Table {
table := o.CreateTable()
table.AddRow("Name", "URL", "LAST_BUILD", "STATUS", "DURATION")
return table
}