Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sgterraform): add support for "forget" action in terraform summary #653

Merged
merged 1 commit into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions tools/sgterraform/tfplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ const (

// ActionDelete denotes a delete operation.
ActionDelete Action = "delete"

// ActionForget denotes a forget operation.
ActionForget Action = "forget"
)

// Actions denotes a valid change type.
Expand Down Expand Up @@ -124,3 +127,12 @@ func (a Actions) CreateBeforeDestroy() bool {
func (a Actions) Replace() bool {
return a.DestroyBeforeCreate() || a.CreateBeforeDestroy()
}

// Forget is true if this set of Actions denotes a forget operation.
func (a Actions) Forget() bool {
if len(a) != 1 {
return false
}

return a[0] == ActionForget
}
10 changes: 8 additions & 2 deletions tools/sgterraform/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func getCommentSummary(ctx context.Context, planFilePath string) (statusIcon, su
destroy := TfChange{actionName: "Destroy", changes: make(map[string]int), actionCount: 0}
update := TfChange{actionName: "Update", changes: make(map[string]int), actionCount: 0}
replace := TfChange{actionName: "Replace", changes: make(map[string]int), actionCount: 0}
forget := TfChange{actionName: "Forget", changes: make(map[string]int), actionCount: 0}
for _, res := range jsonPlan.ResourceChanges {
actions := res.Change.Actions
resourceType := res.Type
Expand All @@ -193,13 +194,17 @@ func getCommentSummary(ctx context.Context, planFilePath string) (statusIcon, su
update.add(resourceType)
case actions.Replace():
replace.add(resourceType)
case actions.Forget():
forget.add(resourceType)
case
actions.NoOp(),
actions.Read():
// Do nothing
continue
default:
sg.Logger(ctx).Fatal(fmt.Errorf("unable to determine resource operation: %v", res))
sg.Logger(ctx).Fatal(
fmt.Errorf("unable to build a terraform plan summary due to unknown actions (%v) on resource: %v", actions, res),
)
}
}

Expand All @@ -212,14 +217,15 @@ func getCommentSummary(ctx context.Context, planFilePath string) (statusIcon, su
}

summary = fmt.Sprintf(`
Plan Summary: %d to create, %d to update, %d to replace, %d to destroy.
Plan Summary: %d to create, %d to update, %d to replace, %d to destroy, %d to forget.
<br/>
%s
`,
create.actionCount,
update.actionCount,
replace.actionCount,
destroy.actionCount,
forget.actionCount,
mapToHTMLList([]TfChange{create, destroy, update, replace}),
)

Expand Down