/
custom.go
120 lines (107 loc) · 3.95 KB
/
custom.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
package consoleui
import (
"context"
"fmt"
"net/url"
"strings"
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/settings"
"github.com/openshift-pipelines/pipelines-as-code/pkg/templates"
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"k8s.io/client-go/dynamic"
)
type CustomConsole struct {
params map[string]string
pacInfo *info.PacOpts
}
func NewCustomConsole(pacInfo *info.PacOpts) *CustomConsole {
return &CustomConsole{pacInfo: pacInfo}
}
func (o *CustomConsole) SetParams(mt map[string]string) {
o.params = mt
}
func (o *CustomConsole) GetName() string {
if o.pacInfo.CustomConsoleName == "" {
return "Not configured"
}
return o.pacInfo.CustomConsoleName
}
func (o *CustomConsole) URL() string {
if o.pacInfo.CustomConsoleURL == "" {
return fmt.Sprintf("https://url.setting.%s.is.not.configured", settings.CustomConsoleURLKey)
}
return o.pacInfo.CustomConsoleURL
}
// generateURL will generate a URL from a template, trim some of the spaces and
// \n we get from yaml
// return the default URL if there it's not become a proper url or that it has
// some of the templates like {{}} left.
func (o *CustomConsole) generateURL(urlTmpl string, dict map[string]string) string {
newurl := templates.ReplacePlaceHoldersVariables(urlTmpl, dict, nil, nil, map[string]interface{}{})
// trim new line because yaml parser adds new line at the end of the string
newurl = strings.TrimSpace(strings.TrimSuffix(newurl, "\n"))
if _, err := url.ParseRequestURI(newurl); err != nil {
return o.URL()
}
// detect if there is still some {{}} in the url
if keys.ParamsRe.MatchString(newurl) {
return o.URL()
}
return newurl
}
func (o *CustomConsole) DetailURL(pr *tektonv1.PipelineRun) string {
if o.pacInfo.CustomConsolePRdetail == "" {
return fmt.Sprintf("https://detailurl.setting.%s.is.not.configured", settings.CustomConsolePRDetailKey)
}
nm := o.params
// make sure the map is not nil before setting this up
// there is a case where SetParams is not called before DetailURL and this would crash the container
if nm == nil {
nm = make(map[string]string)
}
nm["namespace"] = pr.GetNamespace()
nm["pr"] = pr.GetName()
return o.generateURL(o.pacInfo.CustomConsolePRdetail, nm)
}
func (o *CustomConsole) NamespaceURL(pr *tektonv1.PipelineRun) string {
if o.pacInfo.CustomConsoleNamespaceURL == "" {
return fmt.Sprintf("https://detailurl.setting.%s.is.not.configured", settings.CustomConsoleNamespaceURLKey)
}
nm := o.params
// make sure the map is not nil before setting this up
// there is a case where SetParams is not called before DetailURL and this would crash the container
if nm == nil {
nm = make(map[string]string)
}
nm["namespace"] = pr.GetNamespace()
return o.generateURL(o.pacInfo.CustomConsoleNamespaceURL, nm)
}
func (o *CustomConsole) TaskLogURL(pr *tektonv1.PipelineRun, taskRunStatus *tektonv1.PipelineRunTaskRunStatus) string {
if o.pacInfo.CustomConsolePRTaskLog == "" {
return fmt.Sprintf("https://tasklogurl.setting.%s.is.not.configured", settings.CustomConsolePRTaskLogKey)
}
firstFailedStep := ""
// search for the first failed steps in taskrunstatus
for _, step := range taskRunStatus.Status.Steps {
if step.Terminated != nil && step.Terminated.ExitCode != 0 {
firstFailedStep = step.Name
break
}
}
nm := o.params
// make sure the map is not nil before setting this up
// there is a case where SetParams is not called before DetailURL and this would crash the container
if nm == nil {
nm = make(map[string]string)
}
nm["namespace"] = pr.GetNamespace()
nm["pr"] = pr.GetName()
nm["task"] = taskRunStatus.PipelineTaskName
nm["pod"] = taskRunStatus.Status.PodName
nm["firstFailedStep"] = firstFailedStep
return o.generateURL(o.pacInfo.CustomConsolePRTaskLog, nm)
}
func (o *CustomConsole) UI(_ context.Context, _ dynamic.Interface) error {
return nil
}