Skip to content

Commit dfe91e9

Browse files
committed
refactor strategies
1 parent ffa6e55 commit dfe91e9

File tree

11 files changed

+49
-68
lines changed

11 files changed

+49
-68
lines changed

cli/cmd/digger/root.go

-4
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,6 @@ func PreRun(cmd *cobra.Command, args []string) {
9696
ReportStrategy = &reporting.CommentPerRunStrategy{
9797
TimeOfRun: time.Now(),
9898
}
99-
} else if os.Getenv("REPORTING_STRATEGY") == "latest_run_comment" {
100-
ReportStrategy = &reporting.LatestRunCommentStrategy{
101-
TimeOfRun: time.Now(),
102-
}
10399
} else {
104100
ReportStrategy = &reporting.MultipleCommentsStrategy{}
105101
}

cli/pkg/digger/digger.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,12 @@ func RunJobs(jobs []orchestrator.Job, prService ci.PullRequestService, orgServic
170170
func reportPolicyError(projectName string, command string, requestedBy string, reporter reporting.Reporter) string {
171171
msg := fmt.Sprintf("User %s is not allowed to perform action: %s. Check your policies :x:", requestedBy, command)
172172
if reporter.SupportsMarkdown() {
173-
_, _, err := reporter.Report(msg, coreutils.AsCollapsibleComment(fmt.Sprintf("Policy violation for <b>%v - %v</b>", projectName, command), false))
173+
err := reporter.Report(msg, coreutils.AsCollapsibleComment(fmt.Sprintf("Policy violation for <b>%v - %v</b>", projectName, command), false))
174174
if err != nil {
175175
log.Printf("Error publishing comment: %v", err)
176176
}
177177
} else {
178-
_, _, err := reporter.Report(msg, coreutils.AsComment(fmt.Sprintf("Policy violation for %v - %v", projectName, command)))
178+
err := reporter.Report(msg, coreutils.AsComment(fmt.Sprintf("Policy violation for %v - %v", projectName, command)))
179179
if err != nil {
180180
log.Printf("Error publishing comment: %v", err)
181181
}
@@ -311,7 +311,7 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org
311311
preformattedMessaged = append(preformattedMessaged, fmt.Sprintf(" %v", message))
312312
}
313313
planReportMessage = planReportMessage + strings.Join(preformattedMessaged, "<br>")
314-
_, _, err = reporter.Report(planReportMessage, planPolicyFormatter)
314+
err = reporter.Report(planReportMessage, planPolicyFormatter)
315315

316316
if err != nil {
317317
log.Printf("Failed to report plan. %v", err)
@@ -320,7 +320,7 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org
320320
log.Printf(msg)
321321
return nil, msg, fmt.Errorf(msg)
322322
} else {
323-
_, _, err := reporter.Report("Terraform plan validation checks succeeded :white_check_mark:", planPolicyFormatter)
323+
err := reporter.Report("Terraform plan validation checks succeeded :white_check_mark:", planPolicyFormatter)
324324
if err != nil {
325325
log.Printf("Failed to report plan. %v", err)
326326
}
@@ -496,12 +496,12 @@ func reportApplyMergeabilityError(reporter reporting.Reporter) string {
496496
log.Println(comment)
497497

498498
if reporter.SupportsMarkdown() {
499-
_, _, err := reporter.Report(comment, coreutils.AsCollapsibleComment("Apply error", false))
499+
err := reporter.Report(comment, coreutils.AsCollapsibleComment("Apply error", false))
500500
if err != nil {
501501
log.Printf("error publishing comment: %v\n", err)
502502
}
503503
} else {
504-
_, _, err := reporter.Report(comment, coreutils.AsComment("Apply error"))
504+
err := reporter.Report(comment, coreutils.AsComment("Apply error"))
505505
if err != nil {
506506
log.Printf("error publishing comment: %v\n", err)
507507
}
@@ -518,7 +518,7 @@ func reportTerraformPlanOutput(reporter reporting.Reporter, projectId string, pl
518518
formatter = coreutils.GetTerraformOutputAsComment("Plan output")
519519
}
520520

521-
_, _, err := reporter.Report(plan, formatter)
521+
err := reporter.Report(plan, formatter)
522522
if err != nil {
523523
log.Printf("Failed to report plan. %v", err)
524524
}
@@ -533,7 +533,7 @@ func reportPlanSummary(reporter reporting.Reporter, summary string) {
533533
formatter = coreutils.AsComment("Plan summary")
534534
}
535535

536-
_, _, err := reporter.Report("\n"+summary, formatter)
536+
err := reporter.Report("\n"+summary, formatter)
537537
if err != nil {
538538
log.Printf("Failed to report plan summary. %v", err)
539539
}
@@ -543,7 +543,7 @@ func reportEmptyPlanOutput(reporter reporting.Reporter, projectId string) {
543543
identityFormatter := func(comment string) string {
544544
return comment
545545
}
546-
_, _, err := reporter.Report("→ No changes in terraform output for "+projectId, identityFormatter)
546+
err := reporter.Report("→ No changes in terraform output for "+projectId, identityFormatter)
547547
// suppress the comment (if reporter is suppressible)
548548
reporter.Suppress()
549549
if err != nil {

ee/cli/cmd/digger/root.go

-4
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,6 @@ func PreRun(cmd *cobra.Command, args []string) {
9090
ReportStrategy = &reporting.CommentPerRunStrategy{
9191
TimeOfRun: time.Now(),
9292
}
93-
} else if os.Getenv("REPORTING_STRATEGY") == "latest_run_comment" {
94-
ReportStrategy = &reporting.LatestRunCommentStrategy{
95-
TimeOfRun: time.Now(),
96-
}
9793
} else {
9894
ReportStrategy = &reporting.MultipleCommentsStrategy{}
9995
}

libs/comment_utils/reporting/core.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package reporting
22

33
type Reporter interface {
4-
Report(report string, reportFormatting func(report string) string) (commentId string, commentUrl string, error error)
4+
Report(report string, reportFormatting func(report string) string) (error error)
55
Flush() (string, string, error)
66
Suppress() error
77
SupportsMarkdown() bool

libs/comment_utils/reporting/mock.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ type MockReporter struct {
44
commands []string
55
}
66

7-
func (mockReporter *MockReporter) Report(report string, reportFormatting func(report string) string) (string, string, error) {
7+
func (mockReporter *MockReporter) Report(report string, reportFormatting func(report string) string) error {
88
mockReporter.commands = append(mockReporter.commands, "Report")
9-
return "", "", nil
9+
return nil
1010
}
1111

1212
func (mockReporter *MockReporter) Flush() (string, string, error) {

libs/comment_utils/reporting/noop.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package reporting
22

33
type NoopReporter struct{}
44

5-
func (reporter NoopReporter) Report(report string, reportFormatting func(report string) string) (string, string, error) {
6-
return "", "", nil
5+
func (reporter NoopReporter) Report(report string, reportFormatting func(report string) string) error {
6+
return nil
77
}
88

99
func (reporter NoopReporter) Flush() (string, string, error) {

libs/comment_utils/reporting/reporting.go

+14-20
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ type CiReporter struct {
1616
ReportStrategy ReportStrategy
1717
}
1818

19-
func (ciReporter CiReporter) Report(report string, reportFormatting func(report string) string) (string, string, error) {
20-
commentId, commentUrl, err := ciReporter.ReportStrategy.Report(ciReporter.CiService, ciReporter.PrNumber, report, reportFormatting, ciReporter.SupportsMarkdown())
21-
return commentId, commentUrl, err
19+
func (ciReporter CiReporter) Report(report string, reportFormatting func(report string) string) error {
20+
_, _, err := ciReporter.ReportStrategy.Report(ciReporter.CiService, ciReporter.PrNumber, report, reportFormatting, ciReporter.SupportsMarkdown())
21+
return err
2222
}
2323

2424
func (ciReporter CiReporter) Flush() (string, string, error) {
@@ -35,9 +35,9 @@ func (ciReporter CiReporter) SupportsMarkdown() bool {
3535

3636
type StdOutReporter struct{}
3737

38-
func (reporter StdOutReporter) Report(report string, reportFormatting func(report string) string) (string, string, error) {
38+
func (reporter StdOutReporter) Report(report string, reportFormatting func(report string) string) error {
3939
log.Printf("Info: %v", report)
40-
return "", "", nil
40+
return nil
4141
}
4242

4343
func (reporter StdOutReporter) Flush() (string, string, error) {
@@ -54,6 +54,7 @@ func (reporter StdOutReporter) Suppress() error {
5454

5555
type ReportStrategy interface {
5656
Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (commentId string, commentUrl string, error error)
57+
Flush() (string, string, error)
5758
}
5859

5960
type CommentPerRunStrategy struct {
@@ -77,6 +78,10 @@ func (strategy CommentPerRunStrategy) Report(ciService ci.PullRequestService, Pr
7778
return commentId, commentUrl, err
7879
}
7980

81+
func (s CommentPerRunStrategy) Flush() (string, string, error) {
82+
return "", "", nil
83+
}
84+
8085
func upsertComment(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, comments []ci.Comment, reportTitle string, supportsCollapsible bool) (string, string, error) {
8186
report = reportFormatter(report)
8287
commentIdForThisRun := ""
@@ -127,24 +132,13 @@ func upsertComment(ciService ci.PullRequestService, PrNumber int, report string,
127132
return fmt.Sprintf("%v", commentIdForThisRun), commentUrl, nil
128133
}
129134

130-
type LatestRunCommentStrategy struct {
131-
TimeOfRun time.Time
132-
}
133-
134-
func (strategy LatestRunCommentStrategy) Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) {
135-
comments, err := ciService.GetComments(PrNumber)
136-
if err != nil {
137-
return "", "", fmt.Errorf("error getting comments: %v", err)
138-
}
139-
140-
reportTitle := "Digger latest run report"
141-
commentId, commentUrl, err := upsertComment(ciService, PrNumber, report, reportFormatter, comments, reportTitle, supportsCollapsibleComment)
142-
return commentId, commentUrl, err
143-
}
144-
145135
type MultipleCommentsStrategy struct{}
146136

147137
func (strategy MultipleCommentsStrategy) Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) {
148138
_, err := ciService.PublishComment(PrNumber, reportFormatter(report))
149139
return "", "", err
150140
}
141+
142+
func (s MultipleCommentsStrategy) Flush() (string, string, error) {
143+
return "", "", nil
144+
}

libs/comment_utils/reporting/utils.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ func PostInitialSourceComments(ghService ci.PullRequestService, prNumber int, im
3030
CiService: ghService,
3131
ReportStrategy: CommentPerRunStrategy{fmt.Sprintf("Report for location: %v", location), time.Now()},
3232
}
33-
commentId, _, err := reporter.Report("Comment Reporter", func(report string) string { return "" })
33+
err := reporter.Report("Comment Reporter", func(report string) string { return "" })
34+
if err != nil {
35+
log.Printf("Error reporting source module comment: %v", err)
36+
return nil, fmt.Errorf("error reporting source module comment: %v", err)
37+
}
38+
39+
commentId, _, err := reporter.Flush()
3440
if err != nil {
3541
log.Printf("Error reporting source module comment: %v", err)
3642
return nil, fmt.Errorf("error reporting source module comment: %v", err)

libs/execution/execution.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,12 @@ func (d DiggerExecutor) Plan() (*iac_utils.IacSummary, bool, bool, string, strin
294294

295295
func reportError(r reporting.Reporter, stderr string) {
296296
if r.SupportsMarkdown() {
297-
_, _, commentErr := r.Report(stderr, utils.AsCollapsibleComment("Error during init.", false))
297+
commentErr := r.Report(stderr, utils.AsCollapsibleComment("Error during init.", false))
298298
if commentErr != nil {
299299
log.Printf("error publishing comment: %v", commentErr)
300300
}
301301
} else {
302-
_, _, commentErr := r.Report(stderr, utils.AsComment("Error during init."))
302+
commentErr := r.Report(stderr, utils.AsComment("Error during init."))
303303
if commentErr != nil {
304304
log.Printf("error publishing comment: %v", commentErr)
305305
}
@@ -376,12 +376,12 @@ func (d DiggerExecutor) Apply() (*iac_utils.IacSummary, bool, string, error) {
376376

377377
func reportApplyError(r reporting.Reporter, err error) {
378378
if r.SupportsMarkdown() {
379-
_, _, commentErr := r.Report(err.Error(), utils.AsCollapsibleComment("Error during applying.", false))
379+
commentErr := r.Report(err.Error(), utils.AsCollapsibleComment("Error during applying.", false))
380380
if commentErr != nil {
381381
log.Printf("error publishing comment: %v", err)
382382
}
383383
} else {
384-
_, _, commentErr := r.Report(err.Error(), utils.AsComment("Error during applying."))
384+
commentErr := r.Report(err.Error(), utils.AsComment("Error during applying."))
385385
if commentErr != nil {
386386
log.Printf("error publishing comment: %v", err)
387387
}
@@ -396,20 +396,20 @@ func reportTerraformApplyOutput(r reporting.Reporter, projectId string, applyOut
396396
formatter = utils.GetTerraformOutputAsComment("Apply output")
397397
}
398398

399-
_, _, commentErr := r.Report(applyOutput, formatter)
399+
commentErr := r.Report(applyOutput, formatter)
400400
if commentErr != nil {
401401
log.Printf("error publishing comment: %v", commentErr)
402402
}
403403
}
404404

405405
func reportTerraformError(r reporting.Reporter, stderr string) {
406406
if r.SupportsMarkdown() {
407-
_, _, commentErr := r.Report(stderr, utils.GetTerraformOutputAsCollapsibleComment("Error during init.", false))
407+
commentErr := r.Report(stderr, utils.GetTerraformOutputAsCollapsibleComment("Error during init.", false))
408408
if commentErr != nil {
409409
log.Printf("error publishing comment: %v", commentErr)
410410
}
411411
} else {
412-
_, _, commentErr := r.Report(stderr, utils.GetTerraformOutputAsComment("Error during init."))
412+
commentErr := r.Report(stderr, utils.GetTerraformOutputAsComment("Error during init."))
413413
if commentErr != nil {
414414
log.Printf("error publishing comment: %v", commentErr)
415415
}
@@ -428,7 +428,7 @@ func reportAdditionalOutput(r reporting.Reporter, projectId string) {
428428
output, _ := os.ReadFile(diggerOutPath)
429429
outputStr := string(output)
430430
if len(outputStr) > 0 {
431-
_, _, commentErr := r.Report(outputStr, formatter)
431+
commentErr := r.Report(outputStr, formatter)
432432
if commentErr != nil {
433433
log.Printf("error publishing comment: %v", commentErr)
434434
}

libs/locking/locking.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ func (projectLock *PullRequestLock) Lock() (bool, error) {
9696

9797
func reportingLockingSuccess(r reporting.Reporter, comment string) {
9898
if r.SupportsMarkdown() {
99-
_, _, err := r.Report(comment, utils.AsCollapsibleComment("Locking successful", false))
99+
err := r.Report(comment, utils.AsCollapsibleComment("Locking successful", false))
100100
if err != nil {
101101
log.Println("failed to publish comment: " + err.Error())
102102
}
103103
} else {
104-
_, _, err := r.Report(comment, utils.AsComment("Locking successful"))
104+
err := r.Report(comment, utils.AsComment("Locking successful"))
105105
if err != nil {
106106
log.Println("failed to publish comment: " + err.Error())
107107
}
@@ -110,12 +110,12 @@ func reportingLockingSuccess(r reporting.Reporter, comment string) {
110110

111111
func reportLockingFailed(r reporting.Reporter, comment string) {
112112
if r.SupportsMarkdown() {
113-
_, _, err := r.Report(comment, utils.AsCollapsibleComment("Locking failed", false))
113+
err := r.Report(comment, utils.AsCollapsibleComment("Locking failed", false))
114114
if err != nil {
115115
log.Println("failed to publish comment: " + err.Error())
116116
}
117117
} else {
118-
_, _, err := r.Report(comment, utils.AsComment("Locking failed"))
118+
err := r.Report(comment, utils.AsComment("Locking failed"))
119119
if err != nil {
120120
log.Println("failed to publish comment: " + err.Error())
121121
}
@@ -183,12 +183,12 @@ func (projectLock *PullRequestLock) Unlock() (bool, error) {
183183

184184
func reportSuccessfulUnlocking(r reporting.Reporter, comment string) {
185185
if r.SupportsMarkdown() {
186-
_, _, err := r.Report(comment, utils.AsCollapsibleComment("Unlocking successful", false))
186+
err := r.Report(comment, utils.AsCollapsibleComment("Unlocking successful", false))
187187
if err != nil {
188188
log.Println("failed to publish comment: " + err.Error())
189189
}
190190
} else {
191-
_, _, err := r.Report(comment, utils.AsComment("Unlocking successful"))
191+
err := r.Report(comment, utils.AsComment("Unlocking successful"))
192192
if err != nil {
193193
log.Println("failed to publish comment: " + err.Error())
194194
}

libs/spec/providers.go

-11
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,6 @@ func (r ReporterProvider) GetReporter(title string, reporterSpec ReporterSpec, c
121121
Title: title,
122122
TimeOfRun: time.Now(),
123123
}
124-
case "latest_run_comment":
125-
return reporting.LatestRunCommentStrategy{
126-
TimeOfRun: time.Now(),
127-
}
128-
case "always_same_comment":
129-
return reporting.AlwaysSameCommentStrategy{
130-
Title: title,
131-
TimeOfRun: time.Now(),
132-
CommentId: *reporterSpec.ReportCommentId,
133-
}
134-
135124
default:
136125
return reporting.MultipleCommentsStrategy{}
137126
}

0 commit comments

Comments
 (0)