diff --git a/cli/cmd/digger/main.go b/cli/cmd/digger/main.go index 9ec47b1b2..e42de49b3 100644 --- a/cli/cmd/digger/main.go +++ b/cli/cmd/digger/main.go @@ -159,10 +159,17 @@ func gitHubCI(lock core_locking.Lock, policyChecker core_policy.Checker, backend planStorage := newPlanStorage(ghToken, repoOwner, repositoryName, githubActor, job.PullRequestNumber) + log.Printf("Warn: Overriding commenting strategy to Comments-per-run") + + strategy := &reporting.CommentPerRunStrategy{ + Project: job.ProjectName, + IsPlan: job.IsPlan(), + TimeOfRun: time.Now(), + } cireporter := &reporting.CiReporter{ CiService: &githubPrService, PrNumber: *job.PullRequestNumber, - ReportStrategy: reportingStrategy, + ReportStrategy: strategy, IsSupportMarkdown: true, } // using lazy reporter to be able to suppress empty plans diff --git a/cli/pkg/core/execution/execution.go b/cli/pkg/core/execution/execution.go index ef7652ed8..0b69c997d 100644 --- a/cli/pkg/core/execution/execution.go +++ b/cli/pkg/core/execution/execution.go @@ -271,7 +271,7 @@ func (d DiggerExecutor) Plan() (*terraform.PlanSummary, bool, bool, string, stri func reportError(r reporting.Reporter, stderr string) { if r.SupportsMarkdown() { - commentErr := r.Report(stderr, utils.AsCollapsibleComment("Error during init.")) + commentErr := r.Report(stderr, utils.AsCollapsibleComment("Error during init.", false)) if commentErr != nil { log.Printf("error publishing comment: %v", commentErr) } @@ -347,7 +347,7 @@ func (d DiggerExecutor) Apply() (bool, string, error) { func reportApplyError(r reporting.Reporter, err error) { if r.SupportsMarkdown() { - commentErr := r.Report(err.Error(), utils.AsCollapsibleComment("Error during applying.")) + commentErr := r.Report(err.Error(), utils.AsCollapsibleComment("Error during applying.", false)) if commentErr != nil { log.Printf("error publishing comment: %v", err) } @@ -362,9 +362,9 @@ func reportApplyError(r reporting.Reporter, err error) { func reportTerraformApplyOutput(r reporting.Reporter, projectId string, applyOutput string) { var formatter func(string) string if r.SupportsMarkdown() { - formatter = utils.GetTerraformOutputAsCollapsibleComment("Apply for " + projectId + "") + formatter = utils.GetTerraformOutputAsCollapsibleComment("Apply output", false) } else { - formatter = utils.GetTerraformOutputAsComment("Apply for " + projectId) + formatter = utils.GetTerraformOutputAsComment("Apply output") } commentErr := r.Report(applyOutput, formatter) @@ -375,7 +375,7 @@ func reportTerraformApplyOutput(r reporting.Reporter, projectId string, applyOut func reportTerraformError(r reporting.Reporter, stderr string) { if r.SupportsMarkdown() { - commentErr := r.Report(stderr, utils.GetTerraformOutputAsCollapsibleComment("Error during init.")) + commentErr := r.Report(stderr, utils.GetTerraformOutputAsCollapsibleComment("Error during init.", false)) if commentErr != nil { log.Printf("error publishing comment: %v", commentErr) } @@ -390,7 +390,7 @@ func reportTerraformError(r reporting.Reporter, stderr string) { func reportAdditionalOutput(r reporting.Reporter, projectId string) { var formatter func(string) string if r.SupportsMarkdown() { - formatter = utils.GetTerraformOutputAsCollapsibleComment("Additional output for " + projectId + "") + formatter = utils.GetTerraformOutputAsCollapsibleComment("Additional output for "+projectId+"", false) } else { formatter = utils.GetTerraformOutputAsComment("Additional output for " + projectId) } diff --git a/cli/pkg/core/utils/comments.go b/cli/pkg/core/utils/comments.go index 37bc9c73e..497461fff 100644 --- a/cli/pkg/core/utils/comments.go +++ b/cli/pkg/core/utils/comments.go @@ -1,14 +1,16 @@ package utils -func GetTerraformOutputAsCollapsibleComment(summary string) func(string) string { +import "fmt" + +func GetTerraformOutputAsCollapsibleComment(summary string, open bool) func(string) string { return func(comment string) string { - return `
` + summary + ` - -` + "```terraform" + ` -` + comment + ` - ` + "```" + ` -
` + return fmt.Sprintf(`
`+summary+` + +`+"```terraform"+` +`+comment+` +`+"```"+` +
`, open) } } @@ -18,12 +20,11 @@ func GetTerraformOutputAsComment(summary string) func(string) string { } } -func AsCollapsibleComment(summary string) func(string) string { - +func AsCollapsibleComment(summary string, open bool) func(string) string { return func(comment string) string { - return `
` + summary + ` + return fmt.Sprintf(`
` + summary + ` ` + comment + ` -
` +
`) } } diff --git a/cli/pkg/digger/digger.go b/cli/pkg/digger/digger.go index aeaaf4ca5..8dc3dec54 100644 --- a/cli/pkg/digger/digger.go +++ b/cli/pkg/digger/digger.go @@ -171,7 +171,7 @@ func RunJobs( func reportPolicyError(projectName string, command string, requestedBy string, reporter core_reporting.Reporter) string { msg := fmt.Sprintf("User %s is not allowed to perform action: %s. Check your policies :x:", requestedBy, command) if reporter.SupportsMarkdown() { - err := reporter.Report(msg, coreutils.AsCollapsibleComment(fmt.Sprintf("Policy violation for %v - %v", projectName, command))) + err := reporter.Report(msg, coreutils.AsCollapsibleComment(fmt.Sprintf("Policy violation for %v - %v", projectName, command), false)) if err != nil { log.Printf("Error publishing comment: %v", err) } @@ -287,7 +287,7 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org var planPolicyFormatter func(report string) string summary := fmt.Sprintf("Terraform plan validation check (%v)", job.ProjectName) if reporter.SupportsMarkdown() { - planPolicyFormatter = coreutils.AsCollapsibleComment(summary) + planPolicyFormatter = coreutils.AsCollapsibleComment(summary, false) } else { planPolicyFormatter = coreutils.AsComment(summary) } @@ -474,7 +474,7 @@ func reportApplyMergeabilityError(reporter core_reporting.Reporter) string { log.Println(comment) if reporter.SupportsMarkdown() { - err := reporter.Report(comment, coreutils.AsCollapsibleComment("Apply error")) + err := reporter.Report(comment, coreutils.AsCollapsibleComment("Apply error", false)) if err != nil { log.Printf("error publishing comment: %v\n", err) } @@ -491,9 +491,9 @@ func reportTerraformPlanOutput(reporter core_reporting.Reporter, projectId strin var formatter func(string) string if reporter.SupportsMarkdown() { - formatter = coreutils.GetTerraformOutputAsCollapsibleComment("Plan for " + projectId + "") + formatter = coreutils.GetTerraformOutputAsCollapsibleComment("Plan output", true) } else { - formatter = coreutils.GetTerraformOutputAsComment("Plan for " + projectId) + formatter = coreutils.GetTerraformOutputAsComment("Plan output") } err := reporter.Report(plan, formatter) diff --git a/cli/pkg/digger/digger_test.go b/cli/pkg/digger/digger_test.go index a18663bef..30fe0fbb1 100644 --- a/cli/pkg/digger/digger_test.go +++ b/cli/pkg/digger/digger_test.go @@ -276,7 +276,7 @@ func TestCorrectCommandExecutionWhenApplying(t *testing.T) { commandStrings := allCommandsInOrderWithParams(terraformExecutor, commandRunner, prManager, lock, planStorage, planPathProvider) - assert.Equal(t, []string{"RetrievePlan plan", "Init ", "Apply -lock-timeout=3m", "PublishComment 1
Apply for #\n \n```terraform\n\n ```\n
", "Run echo"}, commandStrings) + assert.Equal(t, []string{"RetrievePlan plan", "Init ", "Apply -lock-timeout=3m", "PublishComment 1
Apply output\n\n```terraform\n\n```\n
", "Run echo"}, commandStrings) } func TestCorrectCommandExecutionWhenDestroying(t *testing.T) { diff --git a/cli/pkg/locking/locking.go b/cli/pkg/locking/locking.go index d86bdd6db..0c044f430 100644 --- a/cli/pkg/locking/locking.go +++ b/cli/pkg/locking/locking.go @@ -97,7 +97,7 @@ func (projectLock *PullRequestLock) Lock() (bool, error) { func reportingLockingSuccess(r reporting.Reporter, comment string) { if r.SupportsMarkdown() { - err := r.Report(comment, utils.AsCollapsibleComment("Locking successful")) + err := r.Report(comment, utils.AsCollapsibleComment("Locking successful", false)) if err != nil { log.Println("failed to publish comment: " + err.Error()) } @@ -111,7 +111,7 @@ func reportingLockingSuccess(r reporting.Reporter, comment string) { func reportLockingFailed(r reporting.Reporter, comment string) { if r.SupportsMarkdown() { - err := r.Report(comment, utils.AsCollapsibleComment("Locking failed")) + err := r.Report(comment, utils.AsCollapsibleComment("Locking failed", false)) if err != nil { log.Println("failed to publish comment: " + err.Error()) } @@ -183,7 +183,7 @@ func (projectLock *PullRequestLock) Unlock() (bool, error) { func reportSuccessfulUnlocking(r reporting.Reporter, comment string) { if r.SupportsMarkdown() { - err := r.Report(comment, utils.AsCollapsibleComment("Unlocking successful")) + err := r.Report(comment, utils.AsCollapsibleComment("Unlocking successful", false)) if err != nil { log.Println("failed to publish comment: " + err.Error()) } diff --git a/cli/pkg/reporting/reporting.go b/cli/pkg/reporting/reporting.go index e6d201f2b..cd43d6045 100644 --- a/cli/pkg/reporting/reporting.go +++ b/cli/pkg/reporting/reporting.go @@ -102,6 +102,8 @@ type ReportStrategy interface { } type CommentPerRunStrategy struct { + IsPlan bool + Project string TimeOfRun time.Time } @@ -111,7 +113,16 @@ func (strategy *CommentPerRunStrategy) Report(ciService orchestrator.PullRequest return fmt.Errorf("error getting comments: %v", err) } - reportTitle := "Digger run report at " + strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)") + var reportTitle string + if strategy.Project != "" { + if strategy.IsPlan { + reportTitle = fmt.Sprintf("Plan for %v (%v)", strategy.Project, strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)")) + } else { + reportTitle = fmt.Sprintf("Apply for %v (%v)", strategy.Project, 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)") + } return upsertComment(ciService, PrNumber, report, reportFormatter, comments, reportTitle, supportsCollapsibleComment) } @@ -132,7 +143,7 @@ func upsertComment(ciService orchestrator.PullRequestService, PrNumber int, repo if !supportsCollapsible { comment = utils.AsComment(reportTitle)(report) } else { - comment = utils.AsCollapsibleComment(reportTitle)(report) + comment = utils.AsCollapsibleComment(reportTitle, false)(report) } _, err := ciService.PublishComment(PrNumber, comment) if err != nil { @@ -152,7 +163,7 @@ func upsertComment(ciService orchestrator.PullRequestService, PrNumber int, repo if !supportsCollapsible { completeComment = utils.AsComment(reportTitle)(commentBody) } else { - completeComment = utils.AsCollapsibleComment(reportTitle)(commentBody) + completeComment = utils.AsCollapsibleComment(reportTitle, false)(commentBody) } err := ciService.EditComment(PrNumber, commentIdForThisRun, completeComment) diff --git a/cli/pkg/reporting/reporting_test.go b/cli/pkg/reporting/reporting_test.go index c2e1a507b..a93388879 100644 --- a/cli/pkg/reporting/reporting_test.go +++ b/cli/pkg/reporting/reporting_test.go @@ -2,159 +2,9 @@ package reporting import ( "fmt" - "testing" - "time" - - "github.com/diggerhq/digger/cli/pkg/core/utils" "github.com/diggerhq/digger/libs/orchestrator" - - "github.com/stretchr/testify/assert" ) -func TestCommentPerRunStrategyReport(t *testing.T) { - timeOfRun := time.Now() - strategy := CommentPerRunStrategy{ - TimeOfRun: timeOfRun, - } - existingCommentForOtherRun := utils.AsCollapsibleComment("Digger run report at some other time")("") - - prNumber := 1 - ciService := &MockCiService{ - CommentsPerPr: map[int][]*orchestrator.Comment{ - prNumber: { - { - Id: 1, - Body: &existingCommentForOtherRun, - }, - }, - }, - } - - report := "resource \"null_resource\" \"test\" {}" - reportFormatter := utils.GetTerraformOutputAsCollapsibleComment("run1") - err := strategy.Report(ciService, prNumber, report, reportFormatter, true) - if err != nil { - t.Errorf("Error: %v", err) - } - report2 := "resource \"null_resource\" \"test\" {}" - reportFormatter2 := utils.GetTerraformOutputAsCollapsibleComment("run2") - err = strategy.Report(ciService, prNumber, report2, reportFormatter2, true) - if err != nil { - t.Errorf("Error: %v", err) - } - report3 := "resource \"null_resource\" \"test\" {}" - reportFormatter3 := utils.GetTerraformOutputAsCollapsibleComment("run3") - err = strategy.Report(ciService, prNumber, report3, reportFormatter3, true) - if err != nil { - t.Errorf("Error: %v", err) - } - report4 := "resource \"null_resource\" \"test\" {}" - reportFormatter4 := utils.GetTerraformOutputAsCollapsibleComment("run4") - err = strategy.Report(ciService, prNumber, report4, reportFormatter4, true) - - if err != nil { - t.Errorf("Error: %v", err) - } - - assert.Equal(t, 2, len(ciService.CommentsPerPr[prNumber])) - assert.Equal(t, "
Digger run report at "+timeOfRun.Format("2006-01-02 15:04:05 (MST)")+"\n
run1\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n
\n\n
run2\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n
\n\n\n
run3\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n
\n\n\n
run4\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n
\n\n
", *ciService.CommentsPerPr[prNumber][1].Body) -} - -func TestLatestCommentStrategyReport(t *testing.T) { - timeOfRun := time.Now() - strategy := LatestRunCommentStrategy{ - TimeOfRun: timeOfRun, - } - existingCommentForOtherRun := utils.AsCollapsibleComment("Digger run report at some other time")("") - - prNumber := 1 - ciService := &MockCiService{ - CommentsPerPr: map[int][]*orchestrator.Comment{ - prNumber: { - { - Id: 1, - Body: &existingCommentForOtherRun, - }, - }, - }, - } - - report := "resource \"null_resource\" \"test\" {}" - reportFormatter := utils.GetTerraformOutputAsCollapsibleComment("run1") - err := strategy.Report(ciService, prNumber, report, reportFormatter, true) - if err != nil { - t.Errorf("Error: %v", err) - } - report2 := "resource \"null_resource\" \"test\" {}" - reportFormatter2 := utils.GetTerraformOutputAsCollapsibleComment("run2") - err = strategy.Report(ciService, prNumber, report2, reportFormatter2, true) - if err != nil { - t.Errorf("Error: %v", err) - } - report3 := "resource \"null_resource\" \"test\" {}" - reportFormatter3 := utils.GetTerraformOutputAsCollapsibleComment("run3") - err = strategy.Report(ciService, prNumber, report3, reportFormatter3, true) - if err != nil { - t.Errorf("Error: %v", err) - } - report4 := "resource \"null_resource\" \"test\" {}" - reportFormatter4 := utils.GetTerraformOutputAsCollapsibleComment("run4") - err = strategy.Report(ciService, prNumber, report4, reportFormatter4, true) - - if err != nil { - t.Errorf("Error: %v", err) - } - - assert.Equal(t, 2, len(ciService.CommentsPerPr[prNumber])) - assert.Equal(t, "
Digger latest run report\n
run1\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n
\n\n
run2\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n
\n\n\n
run3\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n
\n\n\n
run4\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n
\n\n
", *ciService.CommentsPerPr[prNumber][1].Body) -} - -func TestMultipleCommentStrategyReport(t *testing.T) { - strategy := MultipleCommentsStrategy{} - existingCommentForOtherRun := utils.AsCollapsibleComment("Digger run report at some other time")("") - - prNumber := 1 - ciService := &MockCiService{ - CommentsPerPr: map[int][]*orchestrator.Comment{ - prNumber: { - { - Id: 1, - Body: &existingCommentForOtherRun, - }, - }, - }, - } - - report := "resource \"null_resource\" \"test\" {}" - reportFormatter := utils.GetTerraformOutputAsCollapsibleComment("run1") - err := strategy.Report(ciService, prNumber, report, reportFormatter, true) - if err != nil { - t.Errorf("Error: %v", err) - } - report2 := "resource \"null_resource\" \"test\" {}" - reportFormatter2 := utils.GetTerraformOutputAsCollapsibleComment("run2") - err = strategy.Report(ciService, prNumber, report2, reportFormatter2, true) - if err != nil { - t.Errorf("Error: %v", err) - } - report3 := "resource \"null_resource\" \"test\" {}" - reportFormatter3 := utils.GetTerraformOutputAsCollapsibleComment("run3") - err = strategy.Report(ciService, prNumber, report3, reportFormatter3, true) - if err != nil { - t.Errorf("Error: %v", err) - } - report4 := "resource \"null_resource\" \"test\" {}" - reportFormatter4 := utils.GetTerraformOutputAsCollapsibleComment("run4") - err = strategy.Report(ciService, prNumber, report4, reportFormatter4, true) - - if err != nil { - t.Errorf("Error: %v", err) - } - - assert.Equal(t, 5, len(ciService.CommentsPerPr[prNumber])) - assert.Equal(t, "
run4\n \n```terraform\nresource \"null_resource\" \"test\" {}\n ```\n
", *ciService.CommentsPerPr[prNumber][4].Body) -} - type MockCiService struct { CommentsPerPr map[int][]*orchestrator.Comment }