Skip to content

Commit

Permalink
Merge pull request #868 from diggerhq/feat/report-additional-output
Browse files Browse the repository at this point in the history
Add reporting for custom run commands

Former-commit-id: 7fbaf46
  • Loading branch information
ZIJ authored Dec 7, 2023
2 parents 6ba5d45 + 2b5aa8b commit 93104f6
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 7 deletions.
6 changes: 6 additions & 0 deletions cli/cmd/digger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ func gitHubCI(lock core_locking.Lock, policyChecker core_policy.Checker, backend
reportErrorAndExit(githubActor, "GITHUB_CONTEXT is not defined", 2)
}

diggerOutPath := os.Getenv("DIGGER_OUT")
if diggerOutPath == "" {
diggerOutPath = os.Getenv("RUNNER_TEMP") + "/digger-out.log"
os.Setenv("DIGGER_OUT", diggerOutPath)
}

runningMode := os.Getenv("INPUT_DIGGER_MODE")

parsedGhActionContext, err := github_models.GetGitHubContext(ghContext)
Expand Down
2 changes: 1 addition & 1 deletion cli/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/diggerhq/digger/cli

go 1.21.5
go 1.21.1

replace github.com/diggerhq/digger/libs => ../libs

Expand Down
41 changes: 36 additions & 5 deletions cli/pkg/core/execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package execution

import (
"fmt"
"log"
"os"
"path"
"regexp"
"strings"

"github.com/diggerhq/digger/cli/pkg/core/locking"
"github.com/diggerhq/digger/cli/pkg/core/reporting"
"github.com/diggerhq/digger/cli/pkg/core/runners"
Expand All @@ -10,11 +16,6 @@ import (
"github.com/diggerhq/digger/cli/pkg/core/utils"
configuration "github.com/diggerhq/digger/libs/digger_config"
"github.com/diggerhq/digger/libs/orchestrator"
"log"
"os"
"path"
"regexp"
"strings"
)

type Executor interface {
Expand Down Expand Up @@ -208,6 +209,7 @@ func (d DiggerExecutor) Plan() (bool, bool, string, string, error) {
}
}
}
reportAdditionalOutput(d.Reporter, d.projectId())
return true, isNonEmptyPlan, plan, terraformPlanOutput, nil
}

Expand Down Expand Up @@ -283,6 +285,7 @@ func (d DiggerExecutor) Apply() (bool, string, error) {
}
}
}
reportAdditionalOutput(d.Reporter, d.projectId())
return true, applyOutput, nil
}

Expand Down Expand Up @@ -328,6 +331,34 @@ 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 <b>" + projectId + "</b>")
} else {
formatter = utils.GetTerraformOutputAsComment("Additional output for " + projectId)
}
diggerOutPath := os.Getenv("DIGGER_OUT")
if _, err := os.Stat(diggerOutPath); err == nil {
output, _ := os.ReadFile(diggerOutPath)
outputStr := string(output)
if len(outputStr) > 0 {
commentErr := r.Report(outputStr, formatter)
if commentErr != nil {
log.Printf("error publishing comment: %v", commentErr)
}
} else {
log.Printf("empty $DIGGER_OUT file at: %v", diggerOutPath)
}
err = os.Remove(diggerOutPath)
if err != nil {
log.Printf("error removing $DIGGER_OUT file at: %v, %v", diggerOutPath, err)
}
} else {
log.Printf("no $DIGGER_OUT file at: %v", diggerOutPath)
}
}

func (d DiggerExecutor) Destroy() (bool, error) {

destroySteps := []configuration.Step{
Expand Down
Loading

0 comments on commit 93104f6

Please sign in to comment.