-
Notifications
You must be signed in to change notification settings - Fork 14
/
pipelines_view.go
186 lines (164 loc) · 5.39 KB
/
pipelines_view.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
package views // import "github.com/jenkins-x/octant-jx/pkg/plugin/views"
import (
"fmt"
"log"
"math"
"strconv"
"strings"
"time"
v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/octant-jx/pkg/common/pluginctx"
"github.com/jenkins-x/octant-jx/pkg/common/viewhelpers"
"github.com/jenkins-x/octant-jx/pkg/plugin"
"github.com/vmware-tanzu/octant/pkg/plugin/service"
"github.com/vmware-tanzu/octant/pkg/store"
"github.com/vmware-tanzu/octant/pkg/view/component"
)
type PipelinesViewConfig struct {
Context pluginctx.Context
Columns []string
Title string
Header string
Filter func(*v1.PipelineActivity, []*v1.PipelineActivity) bool
OwnerFilter component.TableFilter
RepositoryFilter component.TableFilter
BranchFilter component.TableFilter
StatusFilter component.TableFilter
}
func BuildPipelinesViewDefault(request service.Request, pluginContext pluginctx.Context) (component.Component, error) {
return BuildPipelinesView(request, &PipelinesViewConfig{Context: pluginContext})
}
func BuildPipelinesViewRecent(request service.Request, pluginContext pluginctx.Context) (component.Component, error) {
recentTime := time.Now().Add(-1 * time.Hour)
config := &PipelinesViewConfig{Context: pluginContext}
config.Title = "Recent Pipelines"
config.Filter = func(pa *v1.PipelineActivity, all []*v1.PipelineActivity) bool {
completed := pa.Spec.CompletedTimestamp
if completed != nil {
if completed.Time.Before(recentTime) {
return false
}
}
return true
}
return BuildPipelinesView(request, config)
}
func BuildPipelinesView(request service.Request, config *PipelinesViewConfig) (component.Component, error) {
ctx := request.Context()
client := request.DashboardClient()
dl, err := client.List(ctx, store.Key{
APIVersion: "jenkins.io/v1",
Kind: "PipelineActivity",
Namespace: config.Context.Namespace,
})
if err != nil {
log.Printf("failed: %s", err.Error())
}
title := config.Title
if title == "" {
title = "Pipelines"
}
colNames := config.Columns
if len(colNames) == 0 {
colNames = []string{"Owner", "Repository", "Branch", "Build", "Status", "Message"}
}
table := component.NewTableWithRows(
title, "There are no "+title,
component.NewTableCols(colNames...),
[]component.TableRow{})
paList := []*v1.PipelineActivity{}
if dl != nil {
for _, u := range dl.Items {
pa, err := viewhelpers.ToPipelineActivity(&u)
if err != nil {
log.Printf("failed to convert to PipelineActivity for %s: %s", u.GetName(), err.Error())
continue
}
if pa != nil {
paList = append(paList, pa)
}
}
}
if config.Filter != nil {
allList := paList
paList = []*v1.PipelineActivity{}
for _, r := range allList {
if config.Filter(r, allList) {
paList = append(paList, r)
}
}
}
// default statuses
config.StatusFilter.Values = []string{"Succeeded", "Running", "Failed"}
for _, pa := range paList {
tr, err := toPipelineTableRow(pa, config)
if err != nil {
log.Printf("failed to create Table Row: %s", err.Error())
continue
}
if tr != nil {
table.Add(*tr)
}
}
table.Sort("Sort", false)
viewhelpers.InitTableFilters(config.TableFilters())
table.AddFilter("Owner", config.OwnerFilter)
table.AddFilter("Repository", config.RepositoryFilter)
table.AddFilter("Branch", config.BranchFilter)
table.AddFilter("Status", config.StatusFilter)
flexLayout := component.NewFlexLayout("")
if !config.Context.Composite {
headerText := config.Header
if headerText == "" {
headerText = viewhelpers.ToBreadcrumbMarkdown(plugin.RootBreadcrumb, title)
}
header := component.NewMarkdownText(headerText)
flexLayout.AddSections(component.FlexLayoutSection{
{Width: component.WidthFull, View: header},
})
}
flexLayout.AddSections(component.FlexLayoutSection{
{Width: component.WidthFull, View: table},
})
return flexLayout, nil
}
func (f *PipelinesViewConfig) TableFilters() []*component.TableFilter {
return []*component.TableFilter{&f.OwnerFilter, &f.RepositoryFilter, &f.BranchFilter, &f.StatusFilter}
}
func toPipelineTableRow(pa *v1.PipelineActivity, filters *PipelinesViewConfig) (*component.TableRow, error) {
s := &pa.Spec
owner := s.GitOwner
repository := s.GitRepository
branch := s.GitBranch
status := string(s.Status)
viewhelpers.AddFilterValue(&filters.OwnerFilter, owner)
viewhelpers.AddFilterValue(&filters.RepositoryFilter, repository)
viewhelpers.AddFilterValue(&filters.BranchFilter, branch)
viewhelpers.AddFilterValue(&filters.StatusFilter, status)
return &component.TableRow{
"Owner": component.NewText(owner),
"Repository": component.NewText(repository),
"Branch": component.NewText(branch),
"Build": ToPipelineName(pa),
"Status": component.NewText(status),
"Message": ToPipelineLastStepStatus(pa, false, true),
"Sort": ToOrder(pa),
}, nil
}
func ToOrder(pa *v1.PipelineActivity) component.Component {
s := &pa.Spec
n := math.MaxInt64 - viewhelpers.PipelineBuildNumber(pa)
// lets sort in most recent PR first
lower := strings.ToLower(s.GitBranch)
if strings.HasPrefix(lower, "pr-") {
prNumber := strings.TrimPrefix(lower, "pr-")
if prNumber != "" {
pr, err := strconv.Atoi(prNumber)
if err == nil {
lower = fmt.Sprintf("pr-%019d", math.MaxInt64-pr)
}
}
}
order := fmt.Sprintf("%s/%s/%s/%019d/%s", s.GitOwner, s.GitRepository, lower, n, s.Context)
return component.NewText(order)
}