Skip to content

Commit

Permalink
handle an empty plan summary in the case of digger apply (#1140)
Browse files Browse the repository at this point in the history
* handle an empty plan summary
  • Loading branch information
motatoes authored Feb 9, 2024
1 parent 7ef5d0c commit 50b9944
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
11 changes: 8 additions & 3 deletions cli/pkg/digger/digger.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,12 @@ func RunJobs(
currentJob := jobs[0]
repoNameForBackendReporting := strings.ReplaceAll(currentJob.Namespace, "/", "-")
projectNameForBackendReporting := currentJob.ProjectName
planSummary := exectorResults[0].PlanResult.PlanSummary
// TODO: handle the apply result summary as well to report it to backend. Possibly reporting changed resources as well
// Some kind of generic terraform operation summary might need to be introduced
planSummary := terraform.PlanSummary{}
if exectorResults[0].PlanResult != nil {
planSummary = exectorResults[0].PlanResult.PlanSummary
}
prNumber := *currentJob.PullRequestNumber
batchResult, err := backendApi.ReportProjectJobStatus(repoNameForBackendReporting, projectNameForBackendReporting, batchId, "succeeded", time.Now(), &planSummary)
if err != nil {
Expand All @@ -158,15 +163,15 @@ func UpdateStatusComment(jobs []scheduler.SerializedJob, prNumber int, prService

message := ":construction_worker: Jobs status:\n\n"
for _, job := range jobs {

var jobSpec orchestrator.JobJson
err := json.Unmarshal(job.JobString, &jobSpec)
if err != nil {
log.Printf("Failed to convert unmarshall Serialized job")
}
isPlan := jobSpec.IsPlan()

message = message + fmt.Sprintf("<!-- PROJECTHOLDER %v -->\n", job.ProjectName)
message = message + fmt.Sprintf("%v **%v** <a href='%v'>%v</a>%v\n", job.Status.ToEmoji(), jobSpec.ProjectName, *job.WorkflowRunUrl, job.Status.ToString(), job.ResourcesSummaryString())
message = message + fmt.Sprintf("%v **%v** <a href='%v'>%v</a>%v\n", job.Status.ToEmoji(), jobSpec.ProjectName, *job.WorkflowRunUrl, job.Status.ToString(), job.ResourcesSummaryString(isPlan))
message = message + fmt.Sprintf("<!-- PROJECTHOLDEREND %v -->\n", job.ProjectName)
}

Expand Down
2 changes: 1 addition & 1 deletion libs/orchestrator/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func populateKeys(envs map[string]string, provider stscreds.WebIdentityRoleProvi
func (job *Job) PopulateAwsCredentialsEnvVarsForJob() error {

if job.StateEnvProvider != nil {
log.Printf("Project-level AWS role detected, Assuming role: %v for project run: %v", job.ProjectName)
log.Printf("Project-level AWS role detected, Assuming role for project: %v", job.ProjectName)
var err error
backendConfigArgs, err := populateretrieveBackendConfigArgs(*job.StateEnvProvider)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions libs/orchestrator/json_models.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package orchestrator

import "slices"

type StepJson struct {
Action string `json:"action"`
ExtraArgs []string `json:"extraArgs"`
Expand All @@ -25,6 +27,14 @@ type JobJson struct {
CommandEnvVars map[string]string `json:"commandEnvVars"`
}

func (j *JobJson) IsPlan() bool {
return slices.Contains(j.Commands, "digger plan")
}

func (j *JobJson) IsApply() bool {
return slices.Contains(j.Commands, "digger apply")
}

func JobToJson(job Job) JobJson {
return JobJson{
ProjectName: job.ProjectName,
Expand Down
32 changes: 32 additions & 0 deletions libs/orchestrator/json_models_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package orchestrator

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestIsPlanForDiggerPlanJobCorrect(t *testing.T) {
j := JobJson{
ProjectName: "project.Name",
ProjectDir: "project.Dir",
ProjectWorkspace: "workspace",
Terragrunt: false,
Commands: []string{"run echo 'hello", "digger plan"},
EventName: "issue_comment",
}
assert.True(t, j.IsPlan())
assert.False(t, j.IsApply())
}

func TestIsApplyForDiggerApplyJobCorrect(t *testing.T) {
j := JobJson{
ProjectName: "project.Name",
ProjectDir: "project.Dir",
ProjectWorkspace: "workspace",
Terragrunt: false,
Commands: []string{"digger apply"},
EventName: "issue_comment",
}
assert.True(t, j.IsApply())
assert.False(t, j.IsPlan())
}
6 changes: 5 additions & 1 deletion libs/orchestrator/scheduler/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ type SerializedBatch struct {
Jobs []SerializedJob `json:"jobs"`
}

func (s *SerializedJob) ResourcesSummaryString() string {
func (s *SerializedJob) ResourcesSummaryString(isPlan bool) string {
if !isPlan {
return ""
}

if s.Status == DiggerJobSucceeded {
return fmt.Sprintf(" [Resources: %v to create, %v to update, %v to delete]", s.ResourcesCreated, s.ResourcesUpdated, s.ResourcesDeleted)
} else {
Expand Down

0 comments on commit 50b9944

Please sign in to comment.