Skip to content

Commit

Permalink
Properly merge "env" and "environment" fields of different types (#229)
Browse files Browse the repository at this point in the history
Otherwise they get overwritten with each other.
  • Loading branch information
edigaryev authored Jan 13, 2021
1 parent 10ccb85 commit 5a79bef
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
33 changes: 33 additions & 0 deletions pkg/parser/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,42 @@ func (node *Node) Deduplicate() {
node.Children = unique
}

func (node *Node) MergeListOfMapsToSingleMap() {
_, ok := node.Value.(*ListValue)
if !ok {
return
}

var virtualNode Node

for _, child := range node.Children {
if _, ok := child.Value.(*MapValue); !ok {
return
}

virtualNode.MergeFrom(child)
}

node.Children = virtualNode.Children

// Rewrite parents from virtualNode to node
for _, child := range virtualNode.Children {
child.Parent = node
}

// This is now a map
node.Value = &MapValue{}
}

func (node *Node) MergeFrom(other *Node) {
node.Name = other.Name

// Special treatment for environment variables since they can also be represented as a list of maps
if node.Name == "env" || node.Name == "environment" {
node.MergeListOfMapsToSingleMap()
other.MergeListOfMapsToSingleMap()
}

if reflect.TypeOf(node.Value) != reflect.TypeOf(other.Value) {
node.Value = other.Value

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[
{
"commands": [
{
"cloneInstruction": {},
"name": "clone"
}
],
"environment": {
"A": "B",
"C": "D",
"CIRRUS_OS": "linux"
},
"instance": {
"@type": "type.googleapis.com/org.cirruslabs.ci.services.cirruscigrpc.ContainerInstance",
"cpu": 2,
"image": "golang:latest",
"memory": 4096
},
"metadata": {
"properties": {
"allow_failures": "false",
"experimental": "false",
"indexWithinBuild": "0",
"timeout_in": "3600",
"trigger_type": "AUTOMATIC"
}
},
"name": "main"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
container:
image: golang:latest

env:
- A: B

task:
env:
C: D
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[
{
"commands": [
{
"cloneInstruction": {},
"name": "clone"
}
],
"environment": {
"A": "B",
"C": "D",
"CIRRUS_OS": "linux"
},
"instance": {
"@type": "type.googleapis.com/org.cirruslabs.ci.services.cirruscigrpc.ContainerInstance",
"cpu": 2,
"image": "golang:latest",
"memory": 4096
},
"metadata": {
"properties": {
"allow_failures": "false",
"experimental": "false",
"indexWithinBuild": "0",
"timeout_in": "3600",
"trigger_type": "AUTOMATIC"
}
},
"name": "main"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
container:
image: golang:latest

env:
A: B

task:
env:
- C: D

0 comments on commit 5a79bef

Please sign in to comment.