Skip to content

Commit

Permalink
Merge pull request #645 from CircleCI-Public/fix-server-compatibility
Browse files Browse the repository at this point in the history
Only pass pipelineValuesJson if non-empty, revert pipelineValues
  • Loading branch information
aengelberg authored Nov 24, 2021
2 parents 0aee802 + 7a9f8e1 commit 7ce5387
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 58 deletions.
41 changes: 19 additions & 22 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,34 +523,31 @@ func ConfigQuery(cl *graphql.Client, configPath string, orgSlug string, params p
return nil, err
}

// GraphQL isn't forwards-compatible, so we are unusually selective here about
// passing only non-empty fields on to the API, to minimize user impact if the
// backend is out of date.
var fieldAddendums string
if orgSlug != "" {
query = `query ValidateConfig ($config: String!, $pipelineParametersJson: String, $pipelineValuesJson: String, $orgSlug: String) {
buildConfig(configYaml: $config, pipelineParametersJson: $pipelineParametersJson, pipelineValuesJson: $pipelineValuesJson, orgSlug: $orgSlug) {
valid,
errors { message },
sourceYaml,
outputYaml
}
}`
} else {
query = `query ValidateConfig ($config: String!, $pipelineParametersJson: String, $pipelineValuesJson: String) {
buildConfig(configYaml: $config, pipelineParametersJson: $pipelineParametersJson, pipelineValuesJson: $pipelineValuesJson) {
valid,
errors { message },
sourceYaml,
outputYaml
}
}`
fieldAddendums += ", orgSlug: $orgSlug"
}
if len(params) > 0 {
fieldAddendums += ", pipelineParametersJson: $pipelineParametersJson"
}
query = fmt.Sprintf(
`query ValidateConfig ($config: String!, $pipelineParametersJson: String, $pipelineValues: [StringKeyVal!], $orgSlug: String) {
buildConfig(configYaml: $config, pipelineValues: $pipelineValues%s) {
valid,
errors { message },
sourceYaml,
outputYaml
}
}`,
fieldAddendums)

request := graphql.NewRequest(query)
request.Var("config", config)
if values != nil {
pipelineValues, err := json.Marshal(values)
if err != nil {
return nil, fmt.Errorf("unable to serialize pipeline values: %s", err.Error())
}
request.Var("pipelineValuesJson", string(pipelineValues))
request.Var("pipelineValues", pipeline.PrepareForGraphQL(values))
}
if params != nil {
pipelineParameters, err := json.Marshal(params)
Expand Down
63 changes: 27 additions & 36 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,18 @@ var _ = Describe("Config", func() {
Expect(err).ToNot(HaveOccurred())
stdin.Close()

query := `query ValidateConfig ($config: String!, $pipelineParametersJson: String, $pipelineValuesJson: String) {
buildConfig(configYaml: $config, pipelineParametersJson: $pipelineParametersJson, pipelineValuesJson: $pipelineValuesJson) {
valid,
errors { message },
sourceYaml,
outputYaml
}
}`
query := `query ValidateConfig ($config: String!, $pipelineParametersJson: String, $pipelineValues: [StringKeyVal!], $orgSlug: String) {
buildConfig(configYaml: $config, pipelineValues: $pipelineValues) {
valid,
errors { message },
sourceYaml,
outputYaml
}
}`

r := graphql.NewRequest(query)
r.Variables["config"] = config

pipelineValues, err := json.Marshal(pipeline.LocalPipelineValues())
Expect(err).ToNot(HaveOccurred())
r.Variables["pipelineValuesJson"] = string(pipelineValues)
r.Variables["pipelineValues"] = pipeline.PrepareForGraphQL(pipeline.LocalPipelineValues())

req, err := r.Encode()
Expect(err).ShouldNot(HaveOccurred())
Expand Down Expand Up @@ -246,21 +243,18 @@ var _ = Describe("Config", func() {
Expect(err).ToNot(HaveOccurred())
stdin.Close()

query := `query ValidateConfig ($config: String!, $pipelineParametersJson: String, $pipelineValuesJson: String) {
buildConfig(configYaml: $config, pipelineParametersJson: $pipelineParametersJson, pipelineValuesJson: $pipelineValuesJson) {
valid,
errors { message },
sourceYaml,
outputYaml
}
}`
query := `query ValidateConfig ($config: String!, $pipelineParametersJson: String, $pipelineValues: [StringKeyVal!], $orgSlug: String) {
buildConfig(configYaml: $config, pipelineValues: $pipelineValues, pipelineParametersJson: $pipelineParametersJson) {
valid,
errors { message },
sourceYaml,
outputYaml
}
}`

r := graphql.NewRequest(query)
r.Variables["config"] = config

pipelineValues, err := json.Marshal(pipeline.LocalPipelineValues())
Expect(err).ToNot(HaveOccurred())
r.Variables["pipelineValuesJson"] = string(pipelineValues)
r.Variables["pipelineValues"] = pipeline.PrepareForGraphQL(pipeline.LocalPipelineValues())

pipelineParams, err := json.Marshal(pipeline.Parameters{
"foo": "test",
Expand Down Expand Up @@ -313,22 +307,19 @@ var _ = Describe("Config", func() {
Expect(err).ToNot(HaveOccurred())
stdin.Close()

query := `query ValidateConfig ($config: String!, $pipelineParametersJson: String, $pipelineValuesJson: String, $orgSlug: String) {
buildConfig(configYaml: $config, pipelineParametersJson: $pipelineParametersJson, pipelineValuesJson: $pipelineValuesJson, orgSlug: $orgSlug) {
valid,
errors { message },
sourceYaml,
outputYaml
}
}`
query := `query ValidateConfig ($config: String!, $pipelineParametersJson: String, $pipelineValues: [StringKeyVal!], $orgSlug: String) {
buildConfig(configYaml: $config, pipelineValues: $pipelineValues, orgSlug: $orgSlug) {
valid,
errors { message },
sourceYaml,
outputYaml
}
}`

r := graphql.NewRequest(query)
r.Variables["config"] = config
r.Variables["orgSlug"] = orgSlug

pipelineValues, err := json.Marshal(pipeline.LocalPipelineValues())
Expect(err).ToNot(HaveOccurred())
r.Variables["pipelineValuesJson"] = string(pipelineValues)
r.Variables["pipelineValues"] = pipeline.PrepareForGraphQL(pipeline.LocalPipelineValues())

req, err := r.Encode()
Expect(err).ShouldNot(HaveOccurred())
Expand Down

0 comments on commit 7ce5387

Please sign in to comment.