@@ -107,6 +107,42 @@ type ReportStrategy interface {
107
107
Report (ciService ci.PullRequestService , PrNumber int , report string , reportFormatter func (report string ) string , supportsCollapsibleComment bool ) (commentId string , commentUrl string , error error )
108
108
}
109
109
110
+ type AlwaysSameCommentStrategy struct {
111
+ Title string
112
+ CommentId string
113
+ TimeOfRun time.Time
114
+ }
115
+
116
+ func (strategy AlwaysSameCommentStrategy ) Report (ciService ci.PullRequestService , PrNumber int , report string , reportFormatter func (report string ) string , supportsCollapsibleComment bool ) (string , string , error ) {
117
+ comments , err := ciService .GetComments (PrNumber )
118
+ if err != nil {
119
+ return "" , "" , fmt .Errorf ("error getting comments: %v" , err )
120
+ }
121
+
122
+ var commentBody * string
123
+ var commentUrl string
124
+ for _ , comment := range comments {
125
+ if comment .Id == strategy .CommentId {
126
+ commentBody = comment .Body
127
+ commentUrl = comment .Url
128
+ }
129
+ }
130
+
131
+ var reportTitle string
132
+ if strategy .Title != "" {
133
+ reportTitle = strategy .Title + " " + strategy .TimeOfRun .Format ("2006-01-02 15:04:05 (MST)" )
134
+ } else {
135
+ reportTitle = "Digger run report at " + strategy .TimeOfRun .Format ("2006-01-02 15:04:05 (MST)" )
136
+ }
137
+
138
+ err = appendToExistingComment (ciService , PrNumber , * commentBody , report , reportTitle , strategy .CommentId , supportsCollapsibleComment )
139
+ if err != nil {
140
+ return "" , "" , fmt .Errorf ("error while adding to existing comment: %v" , err )
141
+ }
142
+
143
+ return strategy .CommentId , commentUrl , err
144
+ }
145
+
110
146
type CommentPerRunStrategy struct {
111
147
Title string
112
148
TimeOfRun time.Time
@@ -156,26 +192,37 @@ func upsertComment(ciService ci.PullRequestService, PrNumber int, report string,
156
192
return fmt .Sprintf ("%v" , comment .Id ), comment .Url , nil
157
193
}
158
194
195
+ err := appendToExistingComment (ciService , PrNumber , commentBody , report , reportTitle , commentIdForThisRun , supportsCollapsible )
196
+ if err != nil {
197
+ return "" , "" , err
198
+ }
199
+
200
+ return fmt .Sprintf ("%v" , commentIdForThisRun ), commentUrl , nil
201
+
202
+ }
203
+
204
+ func appendToExistingComment (ciService ci.PullRequestService , prNumber int , existingCommentBody string , reportToAppend string , reportTitle string , commentId string , supportsCollapsible bool ) error {
205
+
159
206
// strip first and last lines
160
- lines := strings .Split (commentBody , "\n " )
207
+ lines := strings .Split (existingCommentBody , "\n " )
161
208
lines = lines [1 : len (lines )- 1 ]
162
- commentBody = strings .Join (lines , "\n " )
209
+ existingCommentBody = strings .Join (lines , "\n " )
163
210
164
- commentBody = commentBody + "\n \n " + report + "\n "
211
+ existingCommentBody = existingCommentBody + "\n \n " + reportToAppend + "\n "
165
212
166
213
var completeComment string
167
214
if ! supportsCollapsible {
168
- completeComment = utils .AsComment (reportTitle )(commentBody )
215
+ completeComment = utils .AsComment (reportTitle )(existingCommentBody )
169
216
} else {
170
- completeComment = utils .AsCollapsibleComment (reportTitle , false )(commentBody )
217
+ completeComment = utils .AsCollapsibleComment (reportTitle , false )(existingCommentBody )
171
218
}
172
219
173
- err := ciService .EditComment (PrNumber , commentIdForThisRun , completeComment )
220
+ err := ciService .EditComment (prNumber , commentId , completeComment )
174
221
175
222
if err != nil {
176
- return "" , "" , fmt .Errorf ("error editing comment: %v" , err )
223
+ return fmt .Errorf ("error editing comment: %v" , err )
177
224
}
178
- return fmt . Sprintf ( "%v" , commentIdForThisRun ), commentUrl , nil
225
+ return nil
179
226
}
180
227
181
228
type LatestRunCommentStrategy struct {
0 commit comments