-
Notifications
You must be signed in to change notification settings - Fork 563
/
Copy pathreporting.go
250 lines (207 loc) · 8.07 KB
/
reporting.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
package reporting
import (
"fmt"
"github.com/diggerhq/digger/libs/ci"
"github.com/diggerhq/digger/libs/comment_utils/utils"
"log"
"strings"
"time"
)
type CiReporter struct {
CiService ci.PullRequestService
PrNumber int
IsSupportMarkdown bool
ReportStrategy ReportStrategy
}
func (ciReporter CiReporter) Report(report string, reportFormatting func(report string) string) (string, string, error) {
commentId, commentUrl, err := ciReporter.ReportStrategy.Report(ciReporter.CiService, ciReporter.PrNumber, report, reportFormatting, ciReporter.SupportsMarkdown())
return commentId, commentUrl, err
}
func (ciReporter CiReporter) Flush() (string, string, error) {
return "", "", nil
}
func (ciReporter CiReporter) Suppress() error {
return nil
}
func (ciReporter CiReporter) SupportsMarkdown() bool {
return ciReporter.IsSupportMarkdown
}
type CiReporterLazy struct {
CiReporter CiReporter
isSuppressed bool
reports []string
formatters []func(report string) string
}
func NewCiReporterLazy(ciReporter CiReporter) *CiReporterLazy {
return &CiReporterLazy{
CiReporter: ciReporter,
isSuppressed: false,
reports: []string{},
formatters: []func(report string) string{},
}
}
func (lazyReporter *CiReporterLazy) Report(report string, reportFormatting func(report string) string) (string, string, error) {
lazyReporter.reports = append(lazyReporter.reports, report)
lazyReporter.formatters = append(lazyReporter.formatters, reportFormatting)
return "", "", nil
}
func (lazyReporter *CiReporterLazy) Flush() (string, string, error) {
if lazyReporter.isSuppressed {
log.Printf("Reporter is suprresed, ignoring messages ...")
return "", "", nil
}
var commentId, commentUrl string
for i, _ := range lazyReporter.formatters {
var err error
commentId, commentUrl, err = lazyReporter.CiReporter.ReportStrategy.Report(lazyReporter.CiReporter.CiService, lazyReporter.CiReporter.PrNumber, lazyReporter.reports[i], lazyReporter.formatters[i], lazyReporter.SupportsMarkdown())
if err != nil {
log.Printf("failed to report strategy: ")
return "", "", err
}
}
// clear the buffers
lazyReporter.formatters = []func(comment string) string{}
lazyReporter.reports = []string{}
return commentId, commentUrl, nil
}
func (lazyReporter *CiReporterLazy) Suppress() error {
lazyReporter.isSuppressed = true
return nil
}
func (lazyReporter *CiReporterLazy) SupportsMarkdown() bool {
return lazyReporter.CiReporter.IsSupportMarkdown
}
type StdOutReporter struct{}
func (reporter StdOutReporter) Report(report string, reportFormatting func(report string) string) (string, string, error) {
log.Printf("Info: %v", report)
return "", "", nil
}
func (reporter StdOutReporter) Flush() (string, string, error) {
return "", "", nil
}
func (reporter StdOutReporter) SupportsMarkdown() bool {
return false
}
func (reporter StdOutReporter) Suppress() error {
return nil
}
type ReportStrategy interface {
Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (commentId string, commentUrl string, error error)
}
type AlwaysSameCommentStrategy struct {
Title string
CommentId string
TimeOfRun time.Time
}
func (strategy AlwaysSameCommentStrategy) Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) {
comments, err := ciService.GetComments(PrNumber)
if err != nil {
return "", "", fmt.Errorf("error getting comments: %v", err)
}
var commentBody *string
var commentUrl string
for _, comment := range comments {
if comment.Id == strategy.CommentId {
commentBody = comment.Body
commentUrl = comment.Url
}
}
var reportTitle string
if strategy.Title != "" {
reportTitle = strategy.Title + " " + strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)")
} else {
reportTitle = "Digger run report at " + strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)")
}
err = appendToExistingComment(ciService, PrNumber, *commentBody, report, reportTitle, strategy.CommentId, supportsCollapsibleComment)
if err != nil {
return "", "", fmt.Errorf("error while adding to existing comment: %v", err)
}
return strategy.CommentId, commentUrl, err
}
type CommentPerRunStrategy struct {
Title string
TimeOfRun time.Time
}
func (strategy CommentPerRunStrategy) Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) {
comments, err := ciService.GetComments(PrNumber)
if err != nil {
return "", "", fmt.Errorf("error getting comments: %v", err)
}
var reportTitle string
if strategy.Title != "" {
reportTitle = strategy.Title + " " + strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)")
} else {
reportTitle = "Digger run report at " + strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)")
}
commentId, commentUrl, err := upsertComment(ciService, PrNumber, report, reportFormatter, comments, reportTitle, supportsCollapsibleComment)
return commentId, commentUrl, err
}
func upsertComment(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, comments []ci.Comment, reportTitle string, supportsCollapsible bool) (string, string, error) {
report = reportFormatter(report)
commentIdForThisRun := ""
var commentBody string
var commentUrl string
for _, comment := range comments {
if strings.Contains(*comment.Body, reportTitle) {
commentIdForThisRun = comment.Id
commentBody = *comment.Body
commentUrl = comment.Url
break
}
}
if commentIdForThisRun == "" {
var commentMessage string
if !supportsCollapsible {
commentMessage = utils.AsComment(reportTitle)(report)
} else {
commentMessage = utils.AsCollapsibleComment(reportTitle, false)(report)
}
comment, err := ciService.PublishComment(PrNumber, commentMessage)
if err != nil {
return "", "", fmt.Errorf("error publishing comment: %v", err)
}
return fmt.Sprintf("%v", comment.Id), comment.Url, nil
}
err := appendToExistingComment(ciService, PrNumber, commentBody, report, reportTitle, commentIdForThisRun, supportsCollapsible)
if err != nil {
return "", "", err
}
return fmt.Sprintf("%v", commentIdForThisRun), commentUrl, nil
}
func appendToExistingComment(ciService ci.PullRequestService, prNumber int, existingCommentBody string, reportToAppend string, reportTitle string, commentId string, supportsCollapsible bool) error {
// strip first and last lines
lines := strings.Split(existingCommentBody, "\n")
//if len(lines) > 1 {
// lines = lines[1 : len(lines)-1]
//}
existingCommentBody = strings.Join(lines, "\n")
existingCommentBody = existingCommentBody + "\n\n" + reportToAppend + "\n"
var completeComment string
if !supportsCollapsible {
completeComment = utils.AsComment(reportTitle)(existingCommentBody)
} else {
completeComment = utils.AsCollapsibleComment(reportTitle, false)(existingCommentBody)
}
err := ciService.EditComment(prNumber, commentId, completeComment)
if err != nil {
return fmt.Errorf("error editing comment: %v", err)
}
return nil
}
type LatestRunCommentStrategy struct {
TimeOfRun time.Time
}
func (strategy LatestRunCommentStrategy) Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) {
comments, err := ciService.GetComments(PrNumber)
if err != nil {
return "", "", fmt.Errorf("error getting comments: %v", err)
}
reportTitle := "Digger latest run report"
commentId, commentUrl, err := upsertComment(ciService, PrNumber, report, reportFormatter, comments, reportTitle, supportsCollapsibleComment)
return commentId, commentUrl, err
}
type MultipleCommentsStrategy struct{}
func (strategy MultipleCommentsStrategy) Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) {
_, err := ciService.PublishComment(PrNumber, reportFormatter(report))
return "", "", err
}