-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtemplate.go
More file actions
175 lines (154 loc) · 4.35 KB
/
template.go
File metadata and controls
175 lines (154 loc) · 4.35 KB
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
package transform
import (
"bytes"
"os"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/sirupsen/logrus"
)
var defaultTemplate = `
{{ .HeaderPrefix }} {{ .TagID }} {{ .JobLink }}
{{- if .Collapsible }}
<details>
<summary>Click to expand</summary>
{{- end }}
{{ .Backticks }}diff
{{ .Content }}
{{ .Backticks }}
{{- if .Collapsible }}
</details>
{{- end }}
`
var extendedTemplate = `
{{ .HeaderPrefix }} {{ .TagID }} {{ .JobLink }}
{{ .NumberOfDifferencesString }}
{{- if .NumberReplaces }}
⚠️ Number of resources that require replacement: {{ .NumberReplaces }}
{{- end }}
{{- if .Collapsible }}
<details>
<summary>Click to expand</summary>
{{- end }}
{{ .Backticks }}diff
{{ .Content }}
{{ .Backticks }}
{{- if .Collapsible }}
</details>
{{- end }}
`
var extendedWithResourcesTemplate = `
{{ .HeaderPrefix }} {{ .TagID }} {{ .JobLink }}
{{ .NumberOfDifferencesString }}
{{- if .NumberReplaces }}
⚠️ Number of resources that require replacement: {{ .NumberReplaces }}
{{- end }}
{{- if .ChangedBaseResource }}
### Resources that are subject of change
{{- range $key, $value := .ChangedBaseResource }}
{{ $key }}: {{ $value.Count }}{{ if $value.Replaced }} (required replacement){{ end }}
{{- end }}
{{- end }}
{{- if .Collapsible }}
<details>
<summary>Click to expand</summary>
{{- end }}
{{ .Backticks }}diff
{{ .Content }}
{{ .Backticks }}
{{- if .Collapsible }}
</details>
{{- end }}
`
// commentTemplate wrapper object to use go templating
type commentTemplate struct {
TagID string
Content string
JobLink string
Backticks string
HeaderPrefix string
Collapsible bool
ShowOverview bool
NumberOfDifferencesString string
NumberReplaces int
ChangedBaseResource map[string]ResourceMetric
Template string // template type
customTemplate string // template file or string
}
type TemplateStrategy interface {
getTemplateContent() string
}
type DefaultTemplate struct{}
func (d DefaultTemplate) getTemplateContent() string {
return defaultTemplate
}
type ExtendedTemplate struct{}
func (e ExtendedTemplate) getTemplateContent() string {
return extendedTemplate
}
type ExtendedWithResourcesTemplate struct{}
func (e ExtendedWithResourcesTemplate) getTemplateContent() string {
return extendedWithResourcesTemplate
}
type CustomTemplate struct {
TemplateContent string
}
func (ct CustomTemplate) getTemplateContent() string {
return ct.TemplateContent
}
func (t *commentTemplate) ChooseTemplate() TemplateStrategy {
// TODO deprecated
if t.ShowOverview {
t.Template = "extended"
}
// If customTemplate is set, use it as the template
if t.customTemplate != "" {
templateContent, err := t.getCustomTemplate()
if err != nil {
logrus.Fatal(err)
}
return CustomTemplate{TemplateContent: templateContent}
}
logrus.Debugf("Using template %s", t.Template)
// If customTemplate is not set, use the template type
switch t.Template {
case "default":
return DefaultTemplate{}
case "extended":
return ExtendedTemplate{}
case "extendedWithResources":
return ExtendedWithResourcesTemplate{}
default:
logrus.Warnf("Template %s not found, using default template", t.Template)
return DefaultTemplate{}
}
}
// getCustomTemplate reads the file from provided file path. If the file path is not valid, it will use the string as the template
func (ct *commentTemplate) getCustomTemplate() (string, error) {
// Check if customTemplate is a valid file path
_, err := os.Stat(ct.customTemplate)
if err == nil {
// If it's a valid file path, read the file
b, err := os.ReadFile(ct.customTemplate)
if err != nil {
return "", err
}
return string(b), nil
}
logrus.Warnf("Template file %s not found, using string as template", ct.customTemplate)
// If it's not a valid file path, use customTemplate as the template string
return ct.customTemplate, nil
}
func (t *commentTemplate) render() (string, error) {
templateContent := t.ChooseTemplate().getTemplateContent()
logrus.Debugf("Using template content %s", templateContent)
tmpl, err := template.New("commentTemplate").Funcs(sprig.FuncMap()).Parse(templateContent)
if err != nil {
return "", err
}
stringWriter := bytes.NewBufferString("")
err = tmpl.Execute(stringWriter, t)
if err != nil {
return "", err
}
return stringWriter.String(), nil
}