Skip to content

Commit

Permalink
Added support for other types of instance var values besides strings (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ericpp authored Apr 11, 2023
1 parent a333396 commit 8778767
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
18 changes: 14 additions & 4 deletions concourse/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/url"
"os"
"reflect"
"strings"
)

Expand All @@ -17,7 +18,7 @@ type Build struct {
Job string `json:"job_name"`
APIURL string `json:"api_url"`
Pipeline string `json:"pipeline_name"`
InstanceVars map[string]string `json:"pipeline_instance_vars,omitempty"`
InstanceVars map[string]interface{} `json:"pipeline_instance_vars,omitempty"`
StartTime int `json:"start_time"`
EndTime int `json:"end_time"`
}
Expand Down Expand Up @@ -54,7 +55,7 @@ func NewBuildMetadata(atcurl string) BuildMetadata {

instanceVars := ""
if metadata.InstanceVars != "" {
jsonMap := make(map[string]string)
jsonMap := make(map[string]interface{})
err := json.Unmarshal([]byte(metadata.InstanceVars), &jsonMap)
if err != nil {
panic(fmt.Sprintf("could not unmarshall $BUILD_PIPELINE_INSTANCE_VARS: %s", metadata.InstanceVars))
Expand All @@ -63,8 +64,17 @@ func NewBuildMetadata(atcurl string) BuildMetadata {
query := &url.Values{}
for key, value := range jsonMap {
key = fmt.Sprintf("vars.%s", key)
value = fmt.Sprintf(`"%s"`, value)
query.Set(key, value)

switch val := value.(type) {
case int:
query.Set(key, fmt.Sprintf(`%v`, val))
case float64:
query.Set(key, fmt.Sprintf(`%v`, val))
case string:
query.Set(key, fmt.Sprintf(`"%v"`, val))
default:
panic(fmt.Sprintf("unexpected type for instance var %v: %v", key, reflect.TypeOf(val)))
}
}
instanceVars = fmt.Sprintf("?%s", query.Encode())
}
Expand Down
15 changes: 12 additions & 3 deletions concourse/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func TestJobBuild(t *testing.T) {
Job: "test",
APIURL: "/api/v1/builds/1",
Pipeline: "demo",
InstanceVars: map[string]string{
InstanceVars: map[string]interface{}{
"image_name": "my-image",
"pr_number": "1234",
},
Expand Down Expand Up @@ -193,8 +193,17 @@ func TestJobBuild(t *testing.T) {
query := &url.Values{}
for key, value := range c.build.InstanceVars {
key = fmt.Sprintf("vars.%s", key)
value = fmt.Sprintf(`"%s"`, value)
query.Set(key, value)

switch val := value.(type) {
case int:
query.Set(key, fmt.Sprintf(`%v`, val))
case float64:
query.Set(key, fmt.Sprintf(`%v`, val))
case string:
query.Set(key, fmt.Sprintf(`"%v"`, val))
default:
panic(fmt.Sprintf("unexpected type for instance var %v: %v", key, reflect.TypeOf(val)))
}
}
instanceVars = fmt.Sprintf("?%s", query.Encode())
}
Expand Down

0 comments on commit 8778767

Please sign in to comment.