Skip to content

Commit 9c1ce5d

Browse files
committed
Blocks were not returned in order, so we need to match steps and actions by name.
1 parent cdb1961 commit 9c1ce5d

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

cmd/internal/converters/deployment_process_converter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ func (c DeploymentProcessConverter) toHcl(resource octopus.DeploymentProcess, re
142142
file := hclwrite.NewEmptyFile()
143143
block := gohcl.EncodeAsBlock(terraformResource, "resource")
144144

145-
for i, s := range resource.Steps {
146-
for j, a := range s.Actions {
145+
for _, s := range resource.Steps {
146+
for _, a := range s.Actions {
147147
properties := a.Properties
148148
sanitizedProperties := sanitizer2.SanitizeMap(properties)
149149
sanitizedProperties = c.escapeDollars(sanitizedProperties)
150150
sanitizedProperties = c.escapePercents(sanitizedProperties)
151151
sanitizedProperties = c.replaceIds(sanitizedProperties, dependencies)
152152
sanitizedProperties = c.removeUnnecessaryActionFields(sanitizedProperties)
153-
hcl.WriteActionProperties(block, i, j, sanitizedProperties)
153+
hcl.WriteActionProperties(block, *s.Name, *a.Name, sanitizedProperties)
154154
}
155155
}
156156

cmd/internal/hcl/unquoted_attribute_writer.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"github.com/hashicorp/hcl2/hcl"
7+
"github.com/hashicorp/hcl2/hcl/hclsyntax"
78
"github.com/hashicorp/hcl2/hclwrite"
89
"regexp"
910
"strings"
@@ -19,10 +20,32 @@ func WriteUnquotedAttribute(block *hclwrite.Block, attrName string, attrValue st
1920

2021
// WriteActionProperties is used to pretty print the properties of an action, writing a multiline map for the properties,
2122
// and extracting JSON blobs as maps for easy reading.
22-
func WriteActionProperties(block *hclwrite.Block, step int, action int, properties map[string]string) {
23-
block.Body().Blocks()[step].Body().Blocks()[action].Body().SetAttributeTraversal("properties", hcl.Traversal{
24-
hcl.TraverseRoot{Name: extractJsonAsMap(properties)},
25-
})
23+
func WriteActionProperties(block *hclwrite.Block, stepName string, actionName string, properties map[string]string) {
24+
for _, stepBlock := range block.Body().Blocks() {
25+
stepNameTokens := hclwrite.Tokens{}
26+
blockStepName := getAttributeValue(stepBlock.Body().GetAttribute("name").BuildTokens(stepNameTokens))
27+
if blockStepName == stepName {
28+
for _, actionBlock := range stepBlock.Body().Blocks() {
29+
actionNameTokens := hclwrite.Tokens{}
30+
blockActionName := getAttributeValue(actionBlock.Body().GetAttribute("name").BuildTokens(actionNameTokens))
31+
if blockActionName == actionName {
32+
actionBlock.Body().SetAttributeTraversal("properties", hcl.Traversal{
33+
hcl.TraverseRoot{Name: extractJsonAsMap(properties)},
34+
})
35+
}
36+
}
37+
}
38+
}
39+
}
40+
41+
func getAttributeValue(tokens hclwrite.Tokens) string {
42+
for _, token := range tokens {
43+
if token.Type == hclsyntax.TokenQuotedLit {
44+
return string(token.Bytes)
45+
}
46+
}
47+
48+
return ""
2649
}
2750

2851
func extractJsonAsMap(properties map[string]string) string {

wasm/convert_project.wasm

4.17 KB
Binary file not shown.

0 commit comments

Comments
 (0)