-
Notifications
You must be signed in to change notification settings - Fork 14
/
repositories_view.go
129 lines (107 loc) · 3.52 KB
/
repositories_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
package views // import "github.com/jenkins-x/octant-jx/pkg/plugin/views"
import (
"html"
"strings"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
v1 "github.com/jenkins-x/jx-api/v4/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/vmware-tanzu/octant/pkg/plugin/service"
"github.com/vmware-tanzu/octant/pkg/store"
"github.com/vmware-tanzu/octant/pkg/view/component"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
type RepositoriesViewConfig struct {
OwnerFilter component.TableFilter
}
func (f *RepositoriesViewConfig) TableFilters() []*component.TableFilter {
return []*component.TableFilter{&f.OwnerFilter}
}
func BuildRepositoriesView(request service.Request, pluginContext pluginctx.Context) (component.Component, error) {
ctx := request.Context()
client := request.DashboardClient()
dl, err := client.List(ctx, store.Key{
APIVersion: "jenkins.io/v1",
Kind: "SourceRepository",
Namespace: pluginContext.Namespace,
})
if err != nil {
log.Logger().Infof("failed: %s", err.Error())
return nil, err
}
log.Logger().Infof("got list of SourceRepository %d\n", len(dl.Items))
config := &RepositoriesViewConfig{}
table := component.NewTableWithRows(
viewhelpers.TableTitle("Repositories"), "There are no Repositories!",
component.NewTableCols("Owner", "Name", "Status"),
[]component.TableRow{})
for _, pa := range dl.Items {
tr, err := toRepositoryTableRow(pa, config)
if err != nil {
log.Logger().Infof("failed to create Table Row: %s", err.Error())
continue
}
if tr != nil {
table.Add(*tr)
}
}
table.Sort("Name")
viewhelpers.InitTableFilters(config.TableFilters())
table.AddFilter("Owner", config.OwnerFilter)
flexLayout := component.NewFlexLayout("")
flexLayout.AddSections(component.FlexLayoutSection{
{Width: component.WidthFull, View: table},
})
return flexLayout, nil
}
func toRepositoryTableRow(u unstructured.Unstructured, config *RepositoriesViewConfig) (*component.TableRow, error) {
r := &v1.SourceRepository{}
err := viewhelpers.ToStructured(&u, r)
if err != nil {
return nil, err
}
owner := r.Spec.Org
viewhelpers.AddFilterValue(&config.OwnerFilter, owner)
return &component.TableRow{
"Owner": component.NewText(owner),
"Name": ToRepositoryName(r),
"Status": ToRepositoryStatus(r),
}, nil
}
func ToRepositoryStatus(r *v1.SourceRepository) component.Component {
status := ""
if r.Annotations != nil {
value := strings.ToLower(r.Annotations["webhook.jenkins-x.io"])
if value == "true" {
status = `<clr-icon shape="check-circle" class="is-solid is-success" title="Webhook registered successfully"></clr-icon>`
} else if value != "" {
if strings.HasPrefix(value, "creat") {
status = `<span class="spinner spinner-inline" title="Registering webhook..."></span>`
} else {
text := "Failed to register Webook"
message := r.Annotations["webhook.jenkins-x.io/error"]
if message != "" {
text += ": " + html.EscapeString(message)
}
status = `<clr-icon shape="warning-standard" class="is-solid is-danger" title="` + text + `"></clr-icon>`
}
}
}
return viewhelpers.NewMarkdownText(status)
}
func ToRepositoryName(r *v1.SourceRepository) component.Component {
s := &r.Spec
u := s.URL
if u == "" {
u = s.HTTPCloneURL
}
if u == "" {
if s.Org != "" && s.Repo != "" {
u = s.Org + "/" + s.Repo
if s.Provider != "" {
u = s.Provider + "/" + u
}
}
}
return viewhelpers.NewMarkdownText(viewhelpers.ToMarkdownLink(s.Repo, u))
}