Skip to content

Commit 0e8db99

Browse files
authored
instructions per project comments (#2091)
* update instructions for project specific commands
1 parent 8cca41e commit 0e8db99

File tree

7 files changed

+87
-36
lines changed

7 files changed

+87
-36
lines changed

cli/pkg/digger/digger.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/diggerhq/digger/libs/backendapi"
1414
"github.com/diggerhq/digger/libs/ci"
1515
comment_updater "github.com/diggerhq/digger/libs/comment_utils/summary"
16-
coreutils "github.com/diggerhq/digger/libs/comment_utils/utils"
1716
"github.com/diggerhq/digger/libs/execution"
1817
locking2 "github.com/diggerhq/digger/libs/locking"
1918
"github.com/diggerhq/digger/libs/policy"
@@ -170,12 +169,12 @@ func RunJobs(jobs []orchestrator.Job, prService ci.PullRequestService, orgServic
170169
func reportPolicyError(projectName string, command string, requestedBy string, reporter reporting.Reporter) string {
171170
msg := fmt.Sprintf("User %s is not allowed to perform action: %s. Check your policies :x:", requestedBy, command)
172171
if reporter.SupportsMarkdown() {
173-
_, _, err := reporter.Report(msg, coreutils.AsCollapsibleComment(fmt.Sprintf("Policy violation for <b>%v - %v</b>", projectName, command), false))
172+
_, _, err := reporter.Report(msg, reporting.AsCollapsibleComment(fmt.Sprintf("Policy violation for <b>%v - %v</b>", projectName, command), false))
174173
if err != nil {
175174
slog.Error("Error publishing comment", "error", err)
176175
}
177176
} else {
178-
_, _, err := reporter.Report(msg, coreutils.AsComment(fmt.Sprintf("Policy violation for %v - %v", projectName, command)))
177+
_, _, err := reporter.Report(msg, reporting.AsComment(fmt.Sprintf("Policy violation for %v - %v", projectName, command)))
179178
if err != nil {
180179
slog.Error("Error publishing comment", "error", err)
181180
}
@@ -295,9 +294,9 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org
295294
var planPolicyFormatter func(report string) string
296295
summary := fmt.Sprintf("Terraform plan validation check (%v)", job.ProjectName)
297296
if reporter.SupportsMarkdown() {
298-
planPolicyFormatter = coreutils.AsCollapsibleComment(summary, false)
297+
planPolicyFormatter = reporting.AsCollapsibleComment(summary, false)
299298
} else {
300-
planPolicyFormatter = coreutils.AsComment(summary)
299+
planPolicyFormatter = reporting.AsComment(summary)
301300
}
302301

303302
planSummary, err := iacUtils.GetSummarizePlan(planJsonOutput)
@@ -326,6 +325,10 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org
326325
slog.Error("Failed to report plan.", "error", err)
327326
}
328327
reportPlanSummary(reporter, planSummary)
328+
329+
if err := reporting.FormatAndReportExampleCommands(job.ProjectName, reporter); err != nil {
330+
slog.Error("Failed to report example commands.", "error", err)
331+
}
329332
}
330333
} else {
331334
reportEmptyPlanOutput(reporter, projectLock.LockId())
@@ -497,12 +500,12 @@ func reportApplyMergeabilityError(reporter reporting.Reporter) string {
497500
slog.Error(comment)
498501

499502
if reporter.SupportsMarkdown() {
500-
_, _, err := reporter.Report(comment, coreutils.AsCollapsibleComment("Apply error", false))
503+
_, _, err := reporter.Report(comment, reporting.AsCollapsibleComment("Apply error", false))
501504
if err != nil {
502505
slog.Error("error publishing comment", "error", err)
503506
}
504507
} else {
505-
_, _, err := reporter.Report(comment, coreutils.AsComment("Apply error"))
508+
_, _, err := reporter.Report(comment, reporting.AsComment("Apply error"))
506509
if err != nil {
507510
slog.Error("error publishing comment", "error", err)
508511
}
@@ -514,9 +517,9 @@ func reportTerraformPlanOutput(reporter reporting.Reporter, projectId string, pl
514517
var formatter func(string) string
515518

516519
if reporter.SupportsMarkdown() {
517-
formatter = coreutils.GetTerraformOutputAsCollapsibleComment("Plan output", true)
520+
formatter = reporting.GetTerraformOutputAsCollapsibleComment("Plan output", true)
518521
} else {
519-
formatter = coreutils.GetTerraformOutputAsComment("Plan output")
522+
formatter = reporting.GetTerraformOutputAsComment("Plan output")
520523
}
521524

522525
_, _, err := reporter.Report(plan, formatter)
@@ -529,9 +532,9 @@ func reportPlanSummary(reporter reporting.Reporter, summary string) {
529532
var formatter func(string) string
530533

531534
if reporter.SupportsMarkdown() {
532-
formatter = coreutils.AsCollapsibleComment("Plan summary", false)
535+
formatter = reporting.AsCollapsibleComment("Plan summary", false)
533536
} else {
534-
formatter = coreutils.AsComment("Plan summary")
537+
formatter = reporting.AsComment("Plan summary")
535538
}
536539

537540
_, _, err := reporter.Report("\n"+summary, formatter)

libs/comment_utils/utils/comments.go renamed to libs/comment_utils/reporting/comments.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
package utils
1+
package reporting
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
)
46

57
func GetTerraformOutputAsCollapsibleComment(summary string, open bool) func(string) string {
68
var openTag string
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package reporting
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
func FormatAndReportExampleCommands(projectName string, reporter Reporter) error {
9+
// Escape special shell characters to prevent command injection
10+
escapedProjectName := strings.NewReplacer(
11+
"`", "\\`",
12+
" ", "\\ ",
13+
"\"", "\\\"",
14+
"'", "\\'",
15+
"$", "\\$",
16+
"&", "\\&",
17+
"|", "\\|",
18+
";", "\\;",
19+
"(", "\\(",
20+
")", "\\)",
21+
).Replace(projectName)
22+
23+
commands := fmt.Sprintf(`
24+
▶️ To apply these changes, run the following command:
25+
26+
`+"```"+`bash
27+
digger apply -p %s
28+
`+"```"+`
29+
30+
⏩ To apply all changes in this PR:
31+
`+"```"+`bash
32+
digger apply
33+
`+"```"+`
34+
35+
🚮 To unlock all projects in this PR:
36+
`+"```"+`bash
37+
digger unlock
38+
`+"```"+`
39+
`, escapedProjectName)
40+
41+
var formatter func(string) string
42+
if reporter.SupportsMarkdown() {
43+
formatter = AsCollapsibleComment("Instructions", false)
44+
} else {
45+
formatter = AsComment("Instructions")
46+
}
47+
48+
_, _, err := reporter.Report(commands, formatter)
49+
return err
50+
}

libs/comment_utils/reporting/reporting.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"time"
88

99
"github.com/diggerhq/digger/libs/ci"
10-
"github.com/diggerhq/digger/libs/comment_utils/utils"
1110
)
1211

1312
type CiReporter struct {
@@ -147,9 +146,9 @@ func upsertComment(ciService ci.PullRequestService, PrNumber int, report string,
147146
if commentIdForThisRun == "" {
148147
var commentMessage string
149148
if !supportsCollapsible {
150-
commentMessage = utils.AsComment(reportTitle)(report)
149+
commentMessage = AsComment(reportTitle)(report)
151150
} else {
152-
commentMessage = utils.AsCollapsibleComment(reportTitle, false)(report)
151+
commentMessage = AsCollapsibleComment(reportTitle, false)(report)
153152
}
154153
comment, err := ciService.PublishComment(PrNumber, commentMessage)
155154
if err != nil {
@@ -168,9 +167,9 @@ func upsertComment(ciService ci.PullRequestService, PrNumber int, report string,
168167

169168
var completeComment string
170169
if !supportsCollapsible {
171-
completeComment = utils.AsComment(reportTitle)(commentBody)
170+
completeComment = AsComment(reportTitle)(commentBody)
172171
} else {
173-
completeComment = utils.AsCollapsibleComment(reportTitle, false)(commentBody)
172+
completeComment = AsCollapsibleComment(reportTitle, false)(commentBody)
174173
}
175174

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

libs/comment_utils/reporting/source_grouping.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"log/slog"
77

88
"github.com/diggerhq/digger/libs/ci"
9-
"github.com/diggerhq/digger/libs/comment_utils/utils"
109
"github.com/diggerhq/digger/libs/digger_config"
1110
"github.com/diggerhq/digger/libs/iac_utils"
1211
"github.com/diggerhq/digger/libs/scheduler"
@@ -94,7 +93,7 @@ func (r SourceGroupingReporter) UpdateComment(sourceDetails []SourceDetails, loc
9493
continue
9594
}
9695
expanded := i == 0 || !allSimilarInGroup
97-
commenter := utils.GetTerraformOutputAsCollapsibleComment(fmt.Sprintf("Plan for %v", project), expanded)
96+
commenter := GetTerraformOutputAsCollapsibleComment(fmt.Sprintf("Plan for %v", project), expanded)
9897
message = message + commenter(terraformOutputs[project]) + "\n"
9998
}
10099

libs/execution/execution.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"strconv"
1111
"strings"
1212

13-
"github.com/diggerhq/digger/libs/comment_utils/utils"
1413
"github.com/diggerhq/digger/libs/iac_utils"
1514
"github.com/diggerhq/digger/libs/locking"
1615
"github.com/diggerhq/digger/libs/scheduler"
@@ -345,12 +344,12 @@ func (d DiggerExecutor) postProcessPlan(stdout string) (string, string, *iac_uti
345344

346345
func reportError(r reporting.Reporter, stderr string) {
347346
if r.SupportsMarkdown() {
348-
_, _, commentErr := r.Report(stderr, utils.AsCollapsibleComment("Error during init.", false))
347+
_, _, commentErr := r.Report(stderr, reporting.AsCollapsibleComment("Error during init.", false))
349348
if commentErr != nil {
350349
slog.Error("error publishing comment", "error", commentErr)
351350
}
352351
} else {
353-
_, _, commentErr := r.Report(stderr, utils.AsComment("Error during init."))
352+
_, _, commentErr := r.Report(stderr, reporting.AsComment("Error during init."))
354353
if commentErr != nil {
355354
slog.Error("error publishing comment", "error", commentErr)
356355
}
@@ -439,12 +438,12 @@ func (d DiggerExecutor) Apply() (*iac_utils.IacSummary, bool, string, error) {
439438

440439
func reportApplyError(r reporting.Reporter, err error) {
441440
if r.SupportsMarkdown() {
442-
_, _, commentErr := r.Report(err.Error(), utils.AsCollapsibleComment("Error during applying.", false))
441+
_, _, commentErr := r.Report(err.Error(), reporting.AsCollapsibleComment("Error during applying.", false))
443442
if commentErr != nil {
444443
slog.Error("error publishing comment", "error", err)
445444
}
446445
} else {
447-
_, _, commentErr := r.Report(err.Error(), utils.AsComment("Error during applying."))
446+
_, _, commentErr := r.Report(err.Error(), reporting.AsComment("Error during applying."))
448447
if commentErr != nil {
449448
slog.Error("error publishing comment", "error", err)
450449
}
@@ -454,9 +453,9 @@ func reportApplyError(r reporting.Reporter, err error) {
454453
func reportTerraformApplyOutput(r reporting.Reporter, projectId string, applyOutput string) {
455454
var formatter func(string) string
456455
if r.SupportsMarkdown() {
457-
formatter = utils.GetTerraformOutputAsCollapsibleComment("Apply output", false)
456+
formatter = reporting.GetTerraformOutputAsCollapsibleComment("Apply output", false)
458457
} else {
459-
formatter = utils.GetTerraformOutputAsComment("Apply output")
458+
formatter = reporting.GetTerraformOutputAsComment("Apply output")
460459
}
461460

462461
_, _, commentErr := r.Report(applyOutput, formatter)
@@ -467,12 +466,12 @@ func reportTerraformApplyOutput(r reporting.Reporter, projectId string, applyOut
467466

468467
func reportTerraformError(r reporting.Reporter, stderr string) {
469468
if r.SupportsMarkdown() {
470-
_, _, commentErr := r.Report(stderr, utils.GetTerraformOutputAsCollapsibleComment("Error during init.", false))
469+
_, _, commentErr := r.Report(stderr, reporting.GetTerraformOutputAsCollapsibleComment("Error during init.", false))
471470
if commentErr != nil {
472471
slog.Error("error publishing comment", "error", commentErr)
473472
}
474473
} else {
475-
_, _, commentErr := r.Report(stderr, utils.GetTerraformOutputAsComment("Error during init."))
474+
_, _, commentErr := r.Report(stderr, reporting.GetTerraformOutputAsComment("Error during init."))
476475
if commentErr != nil {
477476
slog.Error("error publishing comment", "error", commentErr)
478477
}
@@ -482,9 +481,9 @@ func reportTerraformError(r reporting.Reporter, stderr string) {
482481
func reportAdditionalOutput(r reporting.Reporter, projectId string) {
483482
var formatter func(string) string
484483
if r.SupportsMarkdown() {
485-
formatter = utils.GetTerraformOutputAsCollapsibleComment("Additional output for <b>"+projectId+"</b>", false)
484+
formatter = reporting.GetTerraformOutputAsCollapsibleComment("Additional output for <b>"+projectId+"</b>", false)
486485
} else {
487-
formatter = utils.GetTerraformOutputAsComment("Additional output for " + projectId)
486+
formatter = reporting.GetTerraformOutputAsComment("Additional output for " + projectId)
488487
}
489488
diggerOutPath := os.Getenv("DIGGER_OUT")
490489
if _, err := os.Stat(diggerOutPath); err == nil {

libs/locking/locking.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"strings"
1111

1212
"github.com/diggerhq/digger/libs/ci"
13-
"github.com/diggerhq/digger/libs/comment_utils/utils"
1413
"github.com/diggerhq/digger/libs/locking/aws"
1514
"github.com/diggerhq/digger/libs/locking/azure"
1615
"github.com/diggerhq/digger/libs/locking/gcp"
@@ -96,12 +95,12 @@ func (projectLock *PullRequestLock) Lock() (bool, error) {
9695

9796
func reportLockingFailed(r reporting.Reporter, comment string) {
9897
if r.SupportsMarkdown() {
99-
_, _, err := r.Report(comment, utils.AsCollapsibleComment("Locking failed", false))
98+
_, _, err := r.Report(comment, reporting.AsCollapsibleComment("Locking failed", false))
10099
if err != nil {
101100
slog.Error("Failed to publish comment", "error", err)
102101
}
103102
} else {
104-
_, _, err := r.Report(comment, utils.AsComment("Locking failed"))
103+
_, _, err := r.Report(comment, reporting.AsComment("Locking failed"))
105104
if err != nil {
106105
slog.Error("Failed to publish comment", "error", err)
107106
}
@@ -173,12 +172,12 @@ func (projectLock *PullRequestLock) Unlock() (bool, error) {
173172

174173
func reportSuccessfulUnlocking(r reporting.Reporter, comment string) {
175174
if r.SupportsMarkdown() {
176-
_, _, err := r.Report(comment, utils.AsCollapsibleComment("Unlocking successful", false))
175+
_, _, err := r.Report(comment, reporting.AsCollapsibleComment("Unlocking successful", false))
177176
if err != nil {
178177
slog.Error("Failed to publish comment", "error", err)
179178
}
180179
} else {
181-
_, _, err := r.Report(comment, utils.AsComment("Unlocking successful"))
180+
_, _, err := r.Report(comment, reporting.AsComment("Unlocking successful"))
182181
if err != nil {
183182
slog.Error("Failed to publish comment", "error", err)
184183
}

0 commit comments

Comments
 (0)