-
Notifications
You must be signed in to change notification settings - Fork 115
/
reporter.go
236 lines (216 loc) · 6.83 KB
/
reporter.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
/*
Copyright 2017 The Kubernetes Authors.
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 reporter contains helpers for writing comments in scm providers.
package reporter
import (
"bytes"
"fmt"
"strings"
"text/template"
"github.com/jenkins-x/go-scm/scm"
"github.com/jenkins-x/lighthouse/pkg/apis/lighthouse/v1alpha1"
"github.com/jenkins-x/lighthouse/pkg/config/job"
"github.com/jenkins-x/lighthouse/pkg/plugins"
)
const (
commentTag = "!-- test report --"
)
// SCMProviderClient provides a client interface to report job status updates
// through GitHub comments.
type SCMProviderClient interface {
BotName() (string, error)
ListPullRequestComments(string, string, int) ([]*scm.Comment, error)
CreateComment(string, string, int, bool, string) error
DeleteComment(string, string, int, int, bool) error
EditComment(string, string, int, int, string, bool) error
QuoteAuthorForComment(string) string
}
// ShouldReport determines whether this LighthouseJob is of a type to be reporting back.
func ShouldReport(lhj *v1alpha1.LighthouseJob, validTypes []job.PipelineKind) bool {
valid := false
for _, t := range validTypes {
if lhj.Spec.Type == t {
valid = true
}
}
if !valid {
return false
}
return true
}
// Report is creating/updating/removing report comments in the SCM provider based on the state of
// the provided LighthouseJob.
func Report(spc SCMProviderClient, reportTemplate *template.Template, lhj *v1alpha1.LighthouseJob, validTypes []job.PipelineKind) error {
if spc == nil {
return fmt.Errorf("trying to report lhj %s, but found empty SCM provider client", lhj.ObjectMeta.Name)
}
if !ShouldReport(lhj, validTypes) {
return nil
}
refs := lhj.Spec.Refs
// we are not reporting for batch jobs, we can consider support that in the future
if len(refs.Pulls) > 1 {
return nil
}
if lhj.Status.CompletionTime == nil {
return nil
}
if len(refs.Pulls) == 0 {
return nil
}
prcs, err := spc.ListPullRequestComments(refs.Org, refs.Repo, refs.Pulls[0].Number)
if err != nil {
return fmt.Errorf("error listing comments: %v", err)
}
botName, err := spc.BotName()
if err != nil {
return fmt.Errorf("error getting bot name: %v", err)
}
deletes, entries, updateID := parsePRComments(lhj, botName, prcs)
for _, delete := range deletes {
if err := spc.DeleteComment(refs.Org, refs.Repo, refs.Pulls[0].Number, delete, true); err != nil {
return fmt.Errorf("error deleting comment: %v", err)
}
}
if len(entries) > 0 {
comment, err := createComment(reportTemplate, lhj, spc.QuoteAuthorForComment(lhj.Spec.Refs.Pulls[0].Author), entries)
if err != nil {
return fmt.Errorf("generating comment: %v", err)
}
if updateID == 0 {
if err := spc.CreateComment(refs.Org, refs.Repo, refs.Pulls[0].Number, true, comment); err != nil {
return fmt.Errorf("error creating comment: %v", err)
}
} else {
if err := spc.EditComment(refs.Org, refs.Repo, refs.Pulls[0].Number, updateID, comment, true); err != nil {
return fmt.Errorf("error updating comment: %v", err)
}
}
}
return nil
}
// parsePRComments returns a list of comments to delete, a list of table
// entries, and the ID of the comment to update. If there are no table entries
// then don't make a new comment. Otherwise, if the comment to update is 0,
// create a new comment.
func parsePRComments(lhj *v1alpha1.LighthouseJob, botName string, ics []*scm.Comment) ([]int, []string, int) {
var toDelete []int
var previousComments []int
var latestComment int
var entries []string
// First accumulate result entries and comment IDs
for _, ic := range ics {
if ic.Author.Login != botName {
continue
}
// Old report comments started with the context. Delete them.
// TODO(spxtr): Delete this check a few weeks after this merges.
if strings.HasPrefix(ic.Body, lhj.Spec.Context) {
toDelete = append(toDelete, ic.ID)
}
if !strings.Contains(ic.Body, commentTag) {
continue
}
if latestComment != 0 {
previousComments = append(previousComments, latestComment)
}
latestComment = ic.ID
var tracking bool
for _, line := range strings.Split(ic.Body, "\n") {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "---") {
tracking = true
} else if len(line) == 0 {
tracking = false
} else if tracking {
entries = append(entries, line)
}
}
}
var newEntries []string
// Next decide which entries to keep.
for i := range entries {
keep := true
f1 := strings.Split(entries[i], " | ")
for j := range entries {
if i == j {
continue
}
f2 := strings.Split(entries[j], " | ")
// Use the newer results if there are multiple.
if j > i && f2[0] == f1[0] {
keep = false
}
}
// Use the current result if there is an old one.
if lhj.Spec.Context == f1[0] {
keep = false
}
if keep {
newEntries = append(newEntries, entries[i])
}
}
var createNewComment bool
if lhj.Status.State == v1alpha1.FailureState {
newEntries = append(newEntries, createEntry(lhj))
createNewComment = true
}
toDelete = append(toDelete, previousComments...)
if (createNewComment || len(newEntries) == 0) && latestComment != 0 {
toDelete = append(toDelete, latestComment)
latestComment = 0
}
return toDelete, newEntries, latestComment
}
func createEntry(lhj *v1alpha1.LighthouseJob) string {
return strings.Join([]string{
lhj.Spec.Context,
lhj.Spec.Refs.Pulls[0].SHA,
fmt.Sprintf("[link](%s)", lhj.Status.ReportURL),
fmt.Sprintf("`%s`", lhj.Spec.RerunCommand),
}, " | ")
}
// createComment take a LighthouseJob and a list of entries generated with
// createEntry and returns a nicely formatted comment. It may fail if template
// execution fails.
func createComment(reportTemplate *template.Template, lhj *v1alpha1.LighthouseJob, author string, entries []string) (string, error) {
plural := ""
if len(entries) > 1 {
plural = "s"
}
var b bytes.Buffer
if reportTemplate != nil {
if err := reportTemplate.Execute(&b, &lhj); err != nil {
return "", err
}
}
lines := []string{
fmt.Sprintf("@%s: The following test%s **failed**, say `/retest` to rerun them all:", author, plural),
"",
"Test name | Commit | Details | Rerun command",
"--- | --- | --- | ---",
}
lines = append(lines, entries...)
if reportTemplate != nil {
lines = append(lines, "", b.String())
}
lines = append(lines, []string{
"",
"<details>",
"",
plugins.AboutThisBot,
"</details>",
"<" + commentTag + ">",
}...)
return strings.Join(lines, "\n"), nil
}