Skip to content

Commit 00125e7

Browse files
authored
Merge pull request #1478 from diggerhq/feat/override-reporting-strategy
Reporting: add project tile in report by digger
2 parents 1f8ae9f + abfe1e6 commit 00125e7

File tree

8 files changed

+49
-180
lines changed

8 files changed

+49
-180
lines changed

cli/cmd/digger/main.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,17 @@ func gitHubCI(lock core_locking.Lock, policyChecker core_policy.Checker, backend
159159

160160
planStorage := newPlanStorage(ghToken, repoOwner, repositoryName, githubActor, job.PullRequestNumber)
161161

162+
log.Printf("Warn: Overriding commenting strategy to Comments-per-run")
163+
164+
strategy := &reporting.CommentPerRunStrategy{
165+
Project: job.ProjectName,
166+
IsPlan: job.IsPlan(),
167+
TimeOfRun: time.Now(),
168+
}
162169
cireporter := &reporting.CiReporter{
163170
CiService: &githubPrService,
164171
PrNumber: *job.PullRequestNumber,
165-
ReportStrategy: reportingStrategy,
172+
ReportStrategy: strategy,
166173
IsSupportMarkdown: true,
167174
}
168175
// using lazy reporter to be able to suppress empty plans

cli/pkg/core/execution/execution.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func (d DiggerExecutor) Plan() (*terraform.PlanSummary, bool, bool, string, stri
271271

272272
func reportError(r reporting.Reporter, stderr string) {
273273
if r.SupportsMarkdown() {
274-
commentErr := r.Report(stderr, utils.AsCollapsibleComment("Error during init."))
274+
commentErr := r.Report(stderr, utils.AsCollapsibleComment("Error during init.", false))
275275
if commentErr != nil {
276276
log.Printf("error publishing comment: %v", commentErr)
277277
}
@@ -347,7 +347,7 @@ func (d DiggerExecutor) Apply() (bool, string, error) {
347347

348348
func reportApplyError(r reporting.Reporter, err error) {
349349
if r.SupportsMarkdown() {
350-
commentErr := r.Report(err.Error(), utils.AsCollapsibleComment("Error during applying."))
350+
commentErr := r.Report(err.Error(), utils.AsCollapsibleComment("Error during applying.", false))
351351
if commentErr != nil {
352352
log.Printf("error publishing comment: %v", err)
353353
}
@@ -362,9 +362,9 @@ func reportApplyError(r reporting.Reporter, err error) {
362362
func reportTerraformApplyOutput(r reporting.Reporter, projectId string, applyOutput string) {
363363
var formatter func(string) string
364364
if r.SupportsMarkdown() {
365-
formatter = utils.GetTerraformOutputAsCollapsibleComment("Apply for <b>" + projectId + "</b>")
365+
formatter = utils.GetTerraformOutputAsCollapsibleComment("Apply output", false)
366366
} else {
367-
formatter = utils.GetTerraformOutputAsComment("Apply for " + projectId)
367+
formatter = utils.GetTerraformOutputAsComment("Apply output")
368368
}
369369

370370
commentErr := r.Report(applyOutput, formatter)
@@ -375,7 +375,7 @@ func reportTerraformApplyOutput(r reporting.Reporter, projectId string, applyOut
375375

376376
func reportTerraformError(r reporting.Reporter, stderr string) {
377377
if r.SupportsMarkdown() {
378-
commentErr := r.Report(stderr, utils.GetTerraformOutputAsCollapsibleComment("Error during init."))
378+
commentErr := r.Report(stderr, utils.GetTerraformOutputAsCollapsibleComment("Error during init.", false))
379379
if commentErr != nil {
380380
log.Printf("error publishing comment: %v", commentErr)
381381
}
@@ -390,7 +390,7 @@ func reportTerraformError(r reporting.Reporter, stderr string) {
390390
func reportAdditionalOutput(r reporting.Reporter, projectId string) {
391391
var formatter func(string) string
392392
if r.SupportsMarkdown() {
393-
formatter = utils.GetTerraformOutputAsCollapsibleComment("Additional output for <b>" + projectId + "</b>")
393+
formatter = utils.GetTerraformOutputAsCollapsibleComment("Additional output for <b>"+projectId+"</b>", false)
394394
} else {
395395
formatter = utils.GetTerraformOutputAsComment("Additional output for " + projectId)
396396
}

cli/pkg/core/utils/comments.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package utils
22

3-
func GetTerraformOutputAsCollapsibleComment(summary string) func(string) string {
3+
import "fmt"
4+
5+
func GetTerraformOutputAsCollapsibleComment(summary string, open bool) func(string) string {
46

57
return func(comment string) string {
6-
return `<details><summary>` + summary + `</summary>
7-
8-
` + "```terraform" + `
9-
` + comment + `
10-
` + "```" + `
11-
</details>`
8+
return fmt.Sprintf(`<details open="%v"><summary>`+summary+`</summary>
9+
10+
`+"```terraform"+`
11+
`+comment+`
12+
`+"```"+`
13+
</details>`, open)
1214
}
1315
}
1416

@@ -18,12 +20,11 @@ func GetTerraformOutputAsComment(summary string) func(string) string {
1820
}
1921
}
2022

21-
func AsCollapsibleComment(summary string) func(string) string {
22-
23+
func AsCollapsibleComment(summary string, open bool) func(string) string {
2324
return func(comment string) string {
24-
return `<details><summary>` + summary + `</summary>
25+
return fmt.Sprintf(`<details><summary>` + summary + `</summary>
2526
` + comment + `
26-
</details>`
27+
</details>`)
2728
}
2829
}
2930

cli/pkg/digger/digger.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func RunJobs(
171171
func reportPolicyError(projectName string, command string, requestedBy string, reporter core_reporting.Reporter) string {
172172
msg := fmt.Sprintf("User %s is not allowed to perform action: %s. Check your policies :x:", requestedBy, command)
173173
if reporter.SupportsMarkdown() {
174-
err := reporter.Report(msg, coreutils.AsCollapsibleComment(fmt.Sprintf("Policy violation for <b>%v - %v</b>", projectName, command)))
174+
err := reporter.Report(msg, coreutils.AsCollapsibleComment(fmt.Sprintf("Policy violation for <b>%v - %v</b>", projectName, command), false))
175175
if err != nil {
176176
log.Printf("Error publishing comment: %v", err)
177177
}
@@ -287,7 +287,7 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org
287287
var planPolicyFormatter func(report string) string
288288
summary := fmt.Sprintf("Terraform plan validation check (%v)", job.ProjectName)
289289
if reporter.SupportsMarkdown() {
290-
planPolicyFormatter = coreutils.AsCollapsibleComment(summary)
290+
planPolicyFormatter = coreutils.AsCollapsibleComment(summary, false)
291291
} else {
292292
planPolicyFormatter = coreutils.AsComment(summary)
293293
}
@@ -474,7 +474,7 @@ func reportApplyMergeabilityError(reporter core_reporting.Reporter) string {
474474
log.Println(comment)
475475

476476
if reporter.SupportsMarkdown() {
477-
err := reporter.Report(comment, coreutils.AsCollapsibleComment("Apply error"))
477+
err := reporter.Report(comment, coreutils.AsCollapsibleComment("Apply error", false))
478478
if err != nil {
479479
log.Printf("error publishing comment: %v\n", err)
480480
}
@@ -491,9 +491,9 @@ func reportTerraformPlanOutput(reporter core_reporting.Reporter, projectId strin
491491
var formatter func(string) string
492492

493493
if reporter.SupportsMarkdown() {
494-
formatter = coreutils.GetTerraformOutputAsCollapsibleComment("Plan for <b>" + projectId + "</b>")
494+
formatter = coreutils.GetTerraformOutputAsCollapsibleComment("Plan output", true)
495495
} else {
496-
formatter = coreutils.GetTerraformOutputAsComment("Plan for " + projectId)
496+
formatter = coreutils.GetTerraformOutputAsComment("Plan output")
497497
}
498498

499499
err := reporter.Report(plan, formatter)

cli/pkg/digger/digger_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ func TestCorrectCommandExecutionWhenApplying(t *testing.T) {
276276

277277
commandStrings := allCommandsInOrderWithParams(terraformExecutor, commandRunner, prManager, lock, planStorage, planPathProvider)
278278

279-
assert.Equal(t, []string{"RetrievePlan plan", "Init ", "Apply -lock-timeout=3m", "PublishComment 1 <details><summary>Apply for <b>#</b></summary>\n \n```terraform\n\n ```\n</details>", "Run echo"}, commandStrings)
279+
assert.Equal(t, []string{"RetrievePlan plan", "Init ", "Apply -lock-timeout=3m", "PublishComment 1 <details open=\"false\"><summary>Apply output</summary>\n\n```terraform\n\n```\n</details>", "Run echo"}, commandStrings)
280280
}
281281

282282
func TestCorrectCommandExecutionWhenDestroying(t *testing.T) {

cli/pkg/locking/locking.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (projectLock *PullRequestLock) Lock() (bool, error) {
9797

9898
func reportingLockingSuccess(r reporting.Reporter, comment string) {
9999
if r.SupportsMarkdown() {
100-
err := r.Report(comment, utils.AsCollapsibleComment("Locking successful"))
100+
err := r.Report(comment, utils.AsCollapsibleComment("Locking successful", false))
101101
if err != nil {
102102
log.Println("failed to publish comment: " + err.Error())
103103
}
@@ -111,7 +111,7 @@ func reportingLockingSuccess(r reporting.Reporter, comment string) {
111111

112112
func reportLockingFailed(r reporting.Reporter, comment string) {
113113
if r.SupportsMarkdown() {
114-
err := r.Report(comment, utils.AsCollapsibleComment("Locking failed"))
114+
err := r.Report(comment, utils.AsCollapsibleComment("Locking failed", false))
115115
if err != nil {
116116
log.Println("failed to publish comment: " + err.Error())
117117
}
@@ -183,7 +183,7 @@ 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"))
186+
err := r.Report(comment, utils.AsCollapsibleComment("Unlocking successful", false))
187187
if err != nil {
188188
log.Println("failed to publish comment: " + err.Error())
189189
}

cli/pkg/reporting/reporting.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ type ReportStrategy interface {
102102
}
103103

104104
type CommentPerRunStrategy struct {
105+
IsPlan bool
106+
Project string
105107
TimeOfRun time.Time
106108
}
107109

@@ -111,7 +113,16 @@ func (strategy *CommentPerRunStrategy) Report(ciService orchestrator.PullRequest
111113
return fmt.Errorf("error getting comments: %v", err)
112114
}
113115

114-
reportTitle := "Digger run report at " + strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)")
116+
var reportTitle string
117+
if strategy.Project != "" {
118+
if strategy.IsPlan {
119+
reportTitle = fmt.Sprintf("Plan for %v (%v)", strategy.Project, strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)"))
120+
} else {
121+
reportTitle = fmt.Sprintf("Apply for %v (%v)", strategy.Project, strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)"))
122+
}
123+
} else {
124+
reportTitle = "Digger run report at " + strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)")
125+
}
115126
return upsertComment(ciService, PrNumber, report, reportFormatter, comments, reportTitle, supportsCollapsibleComment)
116127
}
117128

@@ -132,7 +143,7 @@ func upsertComment(ciService orchestrator.PullRequestService, PrNumber int, repo
132143
if !supportsCollapsible {
133144
comment = utils.AsComment(reportTitle)(report)
134145
} else {
135-
comment = utils.AsCollapsibleComment(reportTitle)(report)
146+
comment = utils.AsCollapsibleComment(reportTitle, false)(report)
136147
}
137148
_, err := ciService.PublishComment(PrNumber, comment)
138149
if err != nil {
@@ -152,7 +163,7 @@ func upsertComment(ciService orchestrator.PullRequestService, PrNumber int, repo
152163
if !supportsCollapsible {
153164
completeComment = utils.AsComment(reportTitle)(commentBody)
154165
} else {
155-
completeComment = utils.AsCollapsibleComment(reportTitle)(commentBody)
166+
completeComment = utils.AsCollapsibleComment(reportTitle, false)(commentBody)
156167
}
157168

158169
err := ciService.EditComment(PrNumber, commentIdForThisRun, completeComment)

cli/pkg/reporting/reporting_test.go

-150
Original file line numberDiff line numberDiff line change
@@ -2,159 +2,9 @@ package reporting
22

33
import (
44
"fmt"
5-
"testing"
6-
"time"
7-
8-
"github.com/diggerhq/digger/cli/pkg/core/utils"
95
"github.com/diggerhq/digger/libs/orchestrator"
10-
11-
"github.com/stretchr/testify/assert"
126
)
137

14-
func TestCommentPerRunStrategyReport(t *testing.T) {
15-
timeOfRun := time.Now()
16-
strategy := CommentPerRunStrategy{
17-
TimeOfRun: timeOfRun,
18-
}
19-
existingCommentForOtherRun := utils.AsCollapsibleComment("Digger run report at some other time")("")
20-
21-
prNumber := 1
22-
ciService := &MockCiService{
23-
CommentsPerPr: map[int][]*orchestrator.Comment{
24-
prNumber: {
25-
{
26-
Id: 1,
27-
Body: &existingCommentForOtherRun,
28-
},
29-
},
30-
},
31-
}
32-
33-
report := "resource \"null_resource\" \"test\" {}"
34-
reportFormatter := utils.GetTerraformOutputAsCollapsibleComment("run1")
35-
err := strategy.Report(ciService, prNumber, report, reportFormatter, true)
36-
if err != nil {
37-
t.Errorf("Error: %v", err)
38-
}
39-
report2 := "resource \"null_resource\" \"test\" {}"
40-
reportFormatter2 := utils.GetTerraformOutputAsCollapsibleComment("run2")
41-
err = strategy.Report(ciService, prNumber, report2, reportFormatter2, true)
42-
if err != nil {
43-
t.Errorf("Error: %v", err)
44-
}
45-
report3 := "resource \"null_resource\" \"test\" {}"
46-
reportFormatter3 := utils.GetTerraformOutputAsCollapsibleComment("run3")
47-
err = strategy.Report(ciService, prNumber, report3, reportFormatter3, true)
48-
if err != nil {
49-
t.Errorf("Error: %v", err)
50-
}
51-
report4 := "resource \"null_resource\" \"test\" {}"
52-
reportFormatter4 := utils.GetTerraformOutputAsCollapsibleComment("run4")
53-
err = strategy.Report(ciService, prNumber, report4, reportFormatter4, true)
54-
55-
if err != nil {
56-
t.Errorf("Error: %v", err)
57-
}
58-
59-
assert.Equal(t, 2, len(ciService.CommentsPerPr[prNumber]))
60-
assert.Equal(t, "<details><summary>Digger run report at "+timeOfRun.Format("2006-01-02 15:04:05 (MST)")+"</summary>\n <details><summary>run1</summary>\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n</details>\n\n<details><summary>run2</summary>\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n</details>\n\n\n<details><summary>run3</summary>\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n</details>\n\n\n<details><summary>run4</summary>\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n</details>\n\n</details>", *ciService.CommentsPerPr[prNumber][1].Body)
61-
}
62-
63-
func TestLatestCommentStrategyReport(t *testing.T) {
64-
timeOfRun := time.Now()
65-
strategy := LatestRunCommentStrategy{
66-
TimeOfRun: timeOfRun,
67-
}
68-
existingCommentForOtherRun := utils.AsCollapsibleComment("Digger run report at some other time")("")
69-
70-
prNumber := 1
71-
ciService := &MockCiService{
72-
CommentsPerPr: map[int][]*orchestrator.Comment{
73-
prNumber: {
74-
{
75-
Id: 1,
76-
Body: &existingCommentForOtherRun,
77-
},
78-
},
79-
},
80-
}
81-
82-
report := "resource \"null_resource\" \"test\" {}"
83-
reportFormatter := utils.GetTerraformOutputAsCollapsibleComment("run1")
84-
err := strategy.Report(ciService, prNumber, report, reportFormatter, true)
85-
if err != nil {
86-
t.Errorf("Error: %v", err)
87-
}
88-
report2 := "resource \"null_resource\" \"test\" {}"
89-
reportFormatter2 := utils.GetTerraformOutputAsCollapsibleComment("run2")
90-
err = strategy.Report(ciService, prNumber, report2, reportFormatter2, true)
91-
if err != nil {
92-
t.Errorf("Error: %v", err)
93-
}
94-
report3 := "resource \"null_resource\" \"test\" {}"
95-
reportFormatter3 := utils.GetTerraformOutputAsCollapsibleComment("run3")
96-
err = strategy.Report(ciService, prNumber, report3, reportFormatter3, true)
97-
if err != nil {
98-
t.Errorf("Error: %v", err)
99-
}
100-
report4 := "resource \"null_resource\" \"test\" {}"
101-
reportFormatter4 := utils.GetTerraformOutputAsCollapsibleComment("run4")
102-
err = strategy.Report(ciService, prNumber, report4, reportFormatter4, true)
103-
104-
if err != nil {
105-
t.Errorf("Error: %v", err)
106-
}
107-
108-
assert.Equal(t, 2, len(ciService.CommentsPerPr[prNumber]))
109-
assert.Equal(t, "<details><summary>Digger latest run report</summary>\n <details><summary>run1</summary>\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n</details>\n\n<details><summary>run2</summary>\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n</details>\n\n\n<details><summary>run3</summary>\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n</details>\n\n\n<details><summary>run4</summary>\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n</details>\n\n</details>", *ciService.CommentsPerPr[prNumber][1].Body)
110-
}
111-
112-
func TestMultipleCommentStrategyReport(t *testing.T) {
113-
strategy := MultipleCommentsStrategy{}
114-
existingCommentForOtherRun := utils.AsCollapsibleComment("Digger run report at some other time")("")
115-
116-
prNumber := 1
117-
ciService := &MockCiService{
118-
CommentsPerPr: map[int][]*orchestrator.Comment{
119-
prNumber: {
120-
{
121-
Id: 1,
122-
Body: &existingCommentForOtherRun,
123-
},
124-
},
125-
},
126-
}
127-
128-
report := "resource \"null_resource\" \"test\" {}"
129-
reportFormatter := utils.GetTerraformOutputAsCollapsibleComment("run1")
130-
err := strategy.Report(ciService, prNumber, report, reportFormatter, true)
131-
if err != nil {
132-
t.Errorf("Error: %v", err)
133-
}
134-
report2 := "resource \"null_resource\" \"test\" {}"
135-
reportFormatter2 := utils.GetTerraformOutputAsCollapsibleComment("run2")
136-
err = strategy.Report(ciService, prNumber, report2, reportFormatter2, true)
137-
if err != nil {
138-
t.Errorf("Error: %v", err)
139-
}
140-
report3 := "resource \"null_resource\" \"test\" {}"
141-
reportFormatter3 := utils.GetTerraformOutputAsCollapsibleComment("run3")
142-
err = strategy.Report(ciService, prNumber, report3, reportFormatter3, true)
143-
if err != nil {
144-
t.Errorf("Error: %v", err)
145-
}
146-
report4 := "resource \"null_resource\" \"test\" {}"
147-
reportFormatter4 := utils.GetTerraformOutputAsCollapsibleComment("run4")
148-
err = strategy.Report(ciService, prNumber, report4, reportFormatter4, true)
149-
150-
if err != nil {
151-
t.Errorf("Error: %v", err)
152-
}
153-
154-
assert.Equal(t, 5, len(ciService.CommentsPerPr[prNumber]))
155-
assert.Equal(t, "<details><summary>run4</summary>\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n</details>", *ciService.CommentsPerPr[prNumber][4].Body)
156-
}
157-
1588
type MockCiService struct {
1599
CommentsPerPr map[int][]*orchestrator.Comment
16010
}

0 commit comments

Comments
 (0)