-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathformat.go
298 lines (256 loc) · 9.75 KB
/
format.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
Copyright 2022 Red Hat Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package status
import (
"bytes"
"fmt"
"os"
"strings"
"text/template"
"github.com/go-logr/logr"
"github.com/konflux-ci/integration-service/gitops"
"github.com/konflux-ci/integration-service/helpers"
"knative.dev/pkg/apis"
)
const commentTemplate = `### {{ .Title }}
{{ .Summary }}`
const summaryTemplate = `
{{- $pipelineRunName := .PipelineRunName -}} {{ $namespace := .Namespace -}} {{ $logger := .Logger -}}
<ul>
<li><b>Pipelinerun</b>: <a href="{{ formatPipelineURL $pipelineRunName $namespace $logger }}">{{ $pipelineRunName }}</a></li>
</ul>
<hr>
| Task | Duration | Test Suite | Status | Details |
| --- | --- | --- | --- | --- |
{{- range $tr := .TaskRuns }}
| <a href="{{ formatTaskLogURL $tr $pipelineRunName $namespace $logger }}">{{ formatTaskName $tr }}</a> | {{ $tr.GetDuration.String }} | {{ formatNamespace $tr }} | {{ formatStatus $tr }} | {{ formatDetails $tr }} |
{{- end }}
{{ formatFootnotes .TaskRuns }}
{{ if .ComponentSnapshotInfos}}
The group snapshot is generated for pr group {{ .PRGroup }} and the component snasphots as below:
| Component | Snapshot | BuildPipelineRun | PullRequest |
| --- | --- | --- | --- |
{{- range $cs := .ComponentSnapshotInfos }}
| {{ $cs.Component }} | {{ $cs.Snapshot }} | <a href="{{ formatPipelineURL $cs.BuildPipelineRun $namespace $logger }}">{{ $cs.BuildPipelineRun }}</a> | <a href="{{ formatPullRequestURL $cs.RepoUrl $cs.PullRequestNumber }}">{{ formatRepoURL $cs.RepoUrl }}</a> |
{{- end }}
{{end}}`
// SummaryTemplateData holds the data necessary to construct a PipelineRun summary.
type SummaryTemplateData struct {
TaskRuns []*helpers.TaskRun
PipelineRunName string
Namespace string
PRGroup string
ComponentSnapshotInfos []*gitops.ComponentSnapshotInfo
Logger logr.Logger
}
// TaskLogTemplateData holds the data necessary to construct a Task log URL.
type TaskLogTemplateData struct {
TaskName string
PipelineRunName string
Namespace string
}
// CommentTemplateData holds the data necessary to construct a PipelineRun comment.
type CommentTemplateData struct {
Title string
Summary string
}
// FormatTestsSummary builds a markdown summary for a list of integration TaskRuns.
func FormatTestsSummary(taskRuns []*helpers.TaskRun, pipelineRunName string, namespace string, componentSnapshotInfos []*gitops.ComponentSnapshotInfo, pr_group string, logger logr.Logger) (string, error) {
funcMap := template.FuncMap{
"formatTaskName": FormatTaskName,
"formatNamespace": FormatNamespace,
"formatStatus": FormatStatus,
"formatDetails": FormatDetails,
"formatPipelineURL": FormatPipelineURL,
"formatTaskLogURL": FormatTaskLogURL,
"formatFootnotes": FormatFootnotes,
"formatPullRequestURL": FormatPullRequestURL,
"formatRepoURL": FormatRepoURL,
}
buf := bytes.Buffer{}
data := SummaryTemplateData{TaskRuns: taskRuns, PipelineRunName: pipelineRunName, Namespace: namespace, PRGroup: pr_group, ComponentSnapshotInfos: componentSnapshotInfos, Logger: logger}
t := template.Must(template.New("").Funcs(funcMap).Parse(summaryTemplate))
if err := t.Execute(&buf, data); err != nil {
return "", err
}
return buf.String(), nil
}
// FormatComment build a markdown comment with the details in text
func FormatComment(title, text string) (string, error) {
buf := bytes.Buffer{}
data := CommentTemplateData{Title: title, Summary: text}
t := template.Must(template.New("").Parse(commentTemplate))
if err := t.Execute(&buf, data); err != nil {
return "", err
}
return buf.String(), nil
}
// FormatStatus accepts a TaskRun and returns a Markdown friendly representation of its overall status, if any.
func FormatStatus(taskRun *helpers.TaskRun) (string, error) {
result, err := taskRun.GetTestResult()
if err != nil {
return "", err
}
var emoji string
if result == nil || result.TestOutput == nil {
taskSucceededReason := taskRun.GetStatusCondition(string(apis.ConditionSucceeded)).GetReason()
switch taskSucceededReason {
case "Succeeded":
emoji = ":heavy_check_mark:"
case "Failed":
emoji = ":x:"
default:
emoji = ":question:"
}
return fmt.Sprintf(emoji+" Reason: %s", taskSucceededReason), nil
}
switch result.TestOutput.Result {
case helpers.AppStudioTestOutputSuccess:
emoji = ":heavy_check_mark:"
case helpers.AppStudioTestOutputFailure:
emoji = ":x:"
case helpers.AppStudioTestOutputWarning:
emoji = ":warning:"
case helpers.AppStudioTestOutputSkipped:
emoji = ":white_check_mark:"
case helpers.AppStudioTestOutputError:
emoji = ":heavy_exclamation_mark:"
default:
emoji = ":question:"
}
return emoji + " " + result.TestOutput.Result, nil
}
// FormatTaskName accepts a TaskRun and returns a Markdown friendly representation of its name.
func FormatTaskName(taskRun *helpers.TaskRun) (string, error) {
result, err := taskRun.GetTestResult()
if err != nil {
return "", err
}
name := taskRun.GetPipelineTaskName()
if result == nil || result.TestOutput == nil {
return name, nil
}
if result.TestOutput.Note == "" {
return name, nil
}
return name + "[^" + name + "]", nil
}
// FormatNamespace accepts a TaskRun and returns a Markdown friendly representation of its test suite, if any.
func FormatNamespace(taskRun *helpers.TaskRun) (string, error) {
result, err := taskRun.GetTestResult()
if err != nil {
return "", err
}
if result == nil || result.TestOutput == nil {
return "", nil
}
return result.TestOutput.Namespace, nil
}
// FormatDetails accepts a TaskRun and returns a Markdown friendly representation of its detailed test results, if any.
func FormatDetails(taskRun *helpers.TaskRun) (string, error) {
result, err := taskRun.GetTestResult()
if err != nil {
return "", err
}
if result == nil {
var emoji string
taskSucceededReason := taskRun.GetStatusCondition(string(apis.ConditionSucceeded)).GetReason()
switch taskSucceededReason {
case "Succeeded":
emoji = ":heavy_check_mark:"
case "Failed":
emoji = ":x:"
default:
emoji = ":question:"
}
return fmt.Sprintf(emoji+" Reason: %s", taskSucceededReason), nil
}
if result.ValidationError != nil {
return fmt.Sprintf("Invalid result: %s", result.ValidationError), nil
}
details := []string{}
if result.TestOutput.Successes > 0 {
details = append(details, fmt.Sprint(":heavy_check_mark: ", result.TestOutput.Successes, " success(es)"))
}
if result.TestOutput.Warnings > 0 {
details = append(details, fmt.Sprint(":warning: ", result.TestOutput.Warnings, " warning(s)"))
}
if result.TestOutput.Failures > 0 {
details = append(details, fmt.Sprint(":x: ", result.TestOutput.Failures, " failure(s)"))
}
return strings.Join(details, "<br>"), nil
}
// FormatResults accepts a list of TaskRuns and returns a Markdown friendly representation of their footnotes, if any.
func FormatFootnotes(taskRuns []*helpers.TaskRun) (string, error) {
footnotes := []string{}
for _, tr := range taskRuns {
result, err := tr.GetTestResult()
if err != nil {
return "", err
}
if result == nil || result.TestOutput == nil {
continue
}
if result.TestOutput.Note != "" {
footnotes = append(footnotes, "[^"+tr.GetPipelineTaskName()+"]: "+result.TestOutput.Note)
}
}
return strings.Join(footnotes, "\n"), nil
}
// FormatPipelineURL accepts a name of application, pipelinerun, namespace and returns a complete pipelineURL.
func FormatPipelineURL(pipelinerun string, namespace string, logger logr.Logger) string {
console_url := os.Getenv("CONSOLE_URL")
if console_url == "" {
return "https://CONSOLE_URL_NOT_AVAILABLE"
}
buf := bytes.Buffer{}
data := SummaryTemplateData{PipelineRunName: pipelinerun, Namespace: namespace}
t := template.Must(template.New("").Parse(console_url))
if err := t.Execute(&buf, data); err != nil {
logger.Error(err, "Error occured when executing template.")
}
return buf.String()
}
// FormatPullRequestURL accepts a name of application, pipelinerun, namespace and returns a complete pipelineURL.
func FormatPullRequestURL(repoUrl string, pullRequestNumber string) string {
pullRequestUrl := "https://PULLREQUEST_URL_NOT_AVAILABLE"
if strings.Contains(repoUrl, "https://github") {
pullRequestUrl = repoUrl + "/pull/" + pullRequestNumber
} else if strings.Contains(repoUrl, "https://gitlab") {
pullRequestUrl = repoUrl + "/-/merge_requests/" + pullRequestNumber
}
return pullRequestUrl
}
func FormatRepoURL(repoUrl string) string {
repoName := "NOT_AVAILABLE"
if repoUrl != "" {
repoUrlStrings := strings.Split(repoUrl, "/")
repoName = repoUrlStrings[len(repoUrlStrings)-1]
}
return repoName
}
// FormatTaskLogURL accepts name of pipelinerun, task, namespace and returns a complete task log URL.
func FormatTaskLogURL(taskRun *helpers.TaskRun, pipelinerun string, namespace string, logger logr.Logger) string {
consoleTaskLogURL := os.Getenv("CONSOLE_URL_TASKLOG")
if consoleTaskLogURL == "" {
return "https://CONSOLE_URL_TASKLOG_NOT_AVAILABLE"
}
taskName := taskRun.GetPipelineTaskName()
buf := bytes.Buffer{}
data := TaskLogTemplateData{PipelineRunName: pipelinerun, TaskName: taskName, Namespace: namespace}
t := template.Must(template.New("").Parse(consoleTaskLogURL))
if err := t.Execute(&buf, data); err != nil {
logger.Error(err, "Error occured when executing task log template.")
}
return buf.String()
}