diff --git a/pkg/apis/devops/v1alpha2/register.go b/pkg/apis/devops/v1alpha2/register.go index 49f56651cdfd9b0b6e2dc22fbf85f1d28b532a91..ee90d28f2975572739c1c24b59f08e003225db77 100644 --- a/pkg/apis/devops/v1alpha2/register.go +++ b/pkg/apis/devops/v1alpha2/register.go @@ -516,7 +516,7 @@ The last one is encrypted info, such as the password of the username-password ty // /blue/rest/organizations/jenkins/pipelines/{devops}/pipelines/{pipeline}/branches/{branch}/runs/{run}/nodes/{node}/steps/{step} webservice.Route(webservice.POST("/devops/{devops}/pipelines/{pipeline}/branches/{branch}/runs/{run}/nodes/{node}/steps/{step}"). - To(devopsapi.CheckBranchPipeline). + To(devopsapi.SubmitBranchInputStep). Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}). Doc("(MultiBranchesPipeline) Proceed or Break the paused pipeline which waiting for user input."). Param(webservice.PathParameter("devops", "DevOps project's ID, e.g. project-RRRRAzLBlLEm")). @@ -530,7 +530,7 @@ The last one is encrypted info, such as the password of the username-password ty // match /blue/rest/organizations/jenkins/pipelines/{devops}/pipelines/{pipeline}/runs/{run}/nodes/{node}/steps/{step} webservice.Route(webservice.POST("/devops/{devops}/pipelines/{pipeline}/runs/{run}/nodes/{node}/steps/{step}"). - To(devopsapi.CheckPipeline). + To(devopsapi.SubmitInputStep). Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsPipelineTag}). Doc("Proceed or Break the paused pipeline which waiting for user input."). Reads(devops.CheckPlayload{}). diff --git a/pkg/apiserver/devops/devops.go b/pkg/apiserver/devops/devops.go index 1f9edae0ff680da4a277871a09afbb258c930145..8e41d9fd2a943413521292c8a8965030a59fd573 100644 --- a/pkg/apiserver/devops/devops.go +++ b/pkg/apiserver/devops/devops.go @@ -314,7 +314,7 @@ func GetPipeBranch(req *restful.Request, resp *restful.Response) { resp.Write(res) } -func CheckBranchPipeline(req *restful.Request, resp *restful.Response) { +func SubmitBranchInputStep(req *restful.Request, resp *restful.Response) { projectName := req.PathParameter("devops") pipelineName := req.PathParameter("pipeline") branchName := req.PathParameter("branch") @@ -322,7 +322,7 @@ func CheckBranchPipeline(req *restful.Request, resp *restful.Response) { nodeId := req.PathParameter("node") stepId := req.PathParameter("step") - res, err := devops.CheckBranchPipeline(projectName, pipelineName, branchName, runId, nodeId, stepId, req.Request) + res, err := devops.SubmitBranchInputStep(projectName, pipelineName, branchName, runId, nodeId, stepId, req.Request) if err != nil { parseErr(err, resp) return @@ -331,14 +331,14 @@ func CheckBranchPipeline(req *restful.Request, resp *restful.Response) { resp.Write(res) } -func CheckPipeline(req *restful.Request, resp *restful.Response) { +func SubmitInputStep(req *restful.Request, resp *restful.Response) { projectName := req.PathParameter("devops") pipelineName := req.PathParameter("pipeline") runId := req.PathParameter("run") nodeId := req.PathParameter("node") stepId := req.PathParameter("step") - res, err := devops.CheckPipeline(projectName, pipelineName, runId, nodeId, stepId, req.Request) + res, err := devops.SubmitInputStep(projectName, pipelineName, runId, nodeId, stepId, req.Request) if err != nil { parseErr(err, resp) return diff --git a/pkg/models/devops/devops.go b/pkg/models/devops/devops.go index 308e86cc893b88b6a72ad7bdde8f7a678a503157..824752463cd4f18d8535dc67b62f0d1689e2b592 100644 --- a/pkg/models/devops/devops.go +++ b/pkg/models/devops/devops.go @@ -18,6 +18,7 @@ package devops import ( + "bytes" "compress/gzip" "encoding/json" "fmt" @@ -293,10 +294,16 @@ func GetPipeBranch(projectName, pipelineName string, req *http.Request) ([]byte, return res, err } -func CheckBranchPipeline(projectName, pipelineName, branchName, runId, nodeId, stepId string, req *http.Request) ([]byte, error) { +func SubmitBranchInputStep(projectName, pipelineName, branchName, runId, nodeId, stepId string, req *http.Request) ([]byte, error) { baseUrl := fmt.Sprintf(jenkins.Server+CheckBranchPipelineUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId, nodeId, stepId) log.Info("Jenkins-url: " + baseUrl) + newBody, err := getInputReqBody(req.Body) + if err != nil { + log.Error(err) + return nil, err + } + req.Body = newBody resBody, err := sendJenkinsRequest(baseUrl, req) if err != nil { log.Error(err) @@ -306,10 +313,16 @@ func CheckBranchPipeline(projectName, pipelineName, branchName, runId, nodeId, s return resBody, err } -func CheckPipeline(projectName, pipelineName, runId, nodeId, stepId string, req *http.Request) ([]byte, error) { +func SubmitInputStep(projectName, pipelineName, runId, nodeId, stepId string, req *http.Request) ([]byte, error) { baseUrl := fmt.Sprintf(jenkins.Server+CheckPipelineUrl+req.URL.RawQuery, projectName, pipelineName, runId, nodeId, stepId) log.Info("Jenkins-url: " + baseUrl) + newBody, err := getInputReqBody(req.Body) + if err != nil { + log.Error(err) + return nil, err + } + req.Body = newBody resBody, err := sendJenkinsRequest(baseUrl, req) if err != nil { log.Error(err) @@ -319,6 +332,45 @@ func CheckPipeline(projectName, pipelineName, runId, nodeId, stepId string, req return resBody, err } +func getInputReqBody(reqBody io.ReadCloser) (newReqBody io.ReadCloser, err error) { + var checkBody CheckPlayload + var jsonBody []byte + var workRound struct { + ID string `json:"id,omitempty" description:"id"` + Parameters []CheckPlayloadParameters `json:"parameters"` + Abort bool `json:"abort,omitempty" description:"abort or not"` + } + + Body, err := ioutil.ReadAll(reqBody) + if err != nil { + log.Error(err) + return nil, err + } + + err = json.Unmarshal(Body, &checkBody) + + if checkBody.Abort != true && checkBody.Parameters == nil { + workRound.Parameters = []CheckPlayloadParameters{} + workRound.ID = checkBody.ID + jsonBody, _ = json.Marshal(workRound) + } else { + jsonBody, _ = json.Marshal(checkBody) + } + + newReqBody = parseBody(bytes.NewBuffer(jsonBody)) + + return newReqBody, nil + +} + +func parseBody(body io.Reader) (newReqBody io.ReadCloser) { + rc, ok := body.(io.ReadCloser) + if !ok && body != nil { + rc = ioutil.NopCloser(body) + } + return rc +} + func GetConsoleLog(projectName, pipelineName string, req *http.Request) ([]byte, error) { baseUrl := fmt.Sprintf(jenkins.Server+GetConsoleLogUrl+req.URL.RawQuery, projectName, pipelineName) log.Info("Jenkins-url: " + baseUrl) diff --git a/pkg/models/devops/json.go b/pkg/models/devops/json.go index 9895e62114405b62272db7bd918d50af61f97db1..6822d54998752d24278cf11f23e2449f60ceef18 100644 --- a/pkg/models/devops/json.go +++ b/pkg/models/devops/json.go @@ -716,12 +716,14 @@ type NodeStatus struct { // CheckPipeline type CheckPlayload struct { - ID string `json:"id,omitempty" description:"id"` - Parameters []struct { - Name string `json:"name,omitempty" description:"name"` - Value string `json:"value,omitempty" description:"value"` - } `json:"parameters,omitempty"` - Abort bool `json:"abort,omitempty" description:"abort or not"` + ID string `json:"id,omitempty" description:"id"` + Parameters []CheckPlayloadParameters `json:"parameters,omitempty"` + Abort bool `json:"abort,omitempty" description:"abort or not"` +} + +type CheckPlayloadParameters struct { + Name string `json:"name,omitempty" description:"name"` + Value string `json:"value,omitempty" description:"value"` } // Getcrumb