Skip to content

Commit 11ca773

Browse files
authored
fix(pipeline/save): save pipeline works for 1.21.x (spinnaker#290)
1 parent 10b6687 commit 11ca773

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

cmd/pipeline/save.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package pipeline
1616

1717
import (
1818
"fmt"
19+
"io/ioutil"
1920
"net/http"
2021

2122
"github.com/spf13/cobra"
@@ -80,26 +81,27 @@ func savePipeline(cmd *cobra.Command, options *saveOptions) error {
8081
if !valid {
8182
return fmt.Errorf("Submitted pipeline is invalid: %s\n", pipelineJson)
8283
}
84+
8385
application := pipelineJson["application"].(string)
8486
pipelineName := pipelineJson["name"].(string)
8587

8688
foundPipeline, queryResp, _ := options.GateClient.ApplicationControllerApi.GetPipelineConfigUsingGET(options.GateClient.Context, application, pipelineName)
87-
88-
_, exists := pipelineJson["id"].(string)
89-
var foundPipelineId string
90-
if queryResp.StatusCode == http.StatusOK && len(foundPipeline) > 0 {
91-
foundPipelineId = foundPipeline["id"].(string)
92-
}
93-
if !exists && foundPipelineId != "" {
94-
pipelineJson["id"] = foundPipelineId
89+
if queryResp.StatusCode == http.StatusOK {
90+
// pipeline found, let's use Spinnaker's known Pipeline ID, otherwise we'll get one created for us
91+
if len(foundPipeline) > 0 {
92+
pipelineJson["id"] = foundPipeline["id"].(string)
93+
}
94+
} else if queryResp.StatusCode == http.StatusNotFound {
95+
// pipeline doesn't exists, let's create a new one
96+
} else {
97+
b, _ := ioutil.ReadAll(queryResp.Body)
98+
return fmt.Errorf("unhandled response %d: %s", queryResp.StatusCode, b)
9599
}
96100

97101
saveResp, saveErr := options.GateClient.PipelineControllerApi.SavePipelineUsingPOST(options.GateClient.Context, pipelineJson)
98-
99102
if saveErr != nil {
100103
return saveErr
101-
}
102-
if saveResp.StatusCode != http.StatusOK {
104+
} else if saveResp.StatusCode != http.StatusOK {
103105
return fmt.Errorf("Encountered an error saving pipeline, status code: %d\n", saveResp.StatusCode)
104106
}
105107

0 commit comments

Comments
 (0)