diff --git a/pkg/apiserver/devops/devops.go b/pkg/apiserver/devops/devops.go index 5245e3529..13314add0 100644 --- a/pkg/apiserver/devops/devops.go +++ b/pkg/apiserver/devops/devops.go @@ -104,11 +104,15 @@ func GetBranchStepLog(req *restful.Request, resp *restful.Response) { nodeId := req.PathParameter("nodeId") stepId := req.PathParameter("stepId") - res, err := devops.GetBranchStepLog(projectName, pipelineName, branchName, runId, nodeId, stepId, req.Request) + res, header, err := devops.GetBranchStepLog(projectName, pipelineName, branchName, runId, nodeId, stepId, req.Request) + if err != nil { parseErr(err, resp) return } + for k, v := range header { + resp.AddHeader(k, v[0]) + } resp.Write(res) } @@ -119,11 +123,14 @@ func GetStepLog(req *restful.Request, resp *restful.Response) { nodeId := req.PathParameter("nodeId") stepId := req.PathParameter("stepId") - res, err := devops.GetStepLog(projectName, pipelineName, runId, nodeId, stepId, req.Request) + res, header, err := devops.GetStepLog(projectName, pipelineName, runId, nodeId, stepId, req.Request) if err != nil { parseErr(err, resp) return } + for k, v := range header { + resp.AddHeader(k, v[0]) + } resp.Write(res) } diff --git a/pkg/models/devops/devops.go b/pkg/models/devops/devops.go index f91c5ef10..7cea449af 100644 --- a/pkg/models/devops/devops.go +++ b/pkg/models/devops/devops.go @@ -106,30 +106,31 @@ func GetPipelineRunNodesbyBranch(projectName, pipelineName, branchName, runId st return res, err } -func GetBranchStepLog(projectName, pipelineName, branchName, runId, nodeId, stepId string, req *http.Request) ([]byte, error) { +func GetBranchStepLog(projectName, pipelineName, branchName, runId, nodeId, stepId string, req *http.Request) ([]byte, http.Header, error) { baseUrl := fmt.Sprintf(jenkins.Server+GetBranchStepLogUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId, nodeId, stepId) log.Infof("Jenkins-url: " + baseUrl) - resBody, err := sendJenkinsRequest(baseUrl, req) + resBody, header, err := jenkinsClient(baseUrl, req) if err != nil { log.Error(err) - return nil, err + return nil, nil, err } - return resBody, err + return resBody, header, err } -func GetStepLog(projectName, pipelineName, runId, nodeId, stepId string, req *http.Request) ([]byte, error) { +func GetStepLog(projectName, pipelineName, runId, nodeId, stepId string, req *http.Request) ([]byte, http.Header, error) { baseUrl := fmt.Sprintf(jenkins.Server+GetStepLogUrl+req.URL.RawQuery, projectName, pipelineName, runId, nodeId, stepId) log.Infof("Jenkins-url: " + baseUrl) - resBody, err := sendJenkinsRequest(baseUrl, req) + resBody, header, err := jenkinsClient(baseUrl, req) if err != nil { log.Error(err) - return nil, err + return nil, nil, err } - return resBody, err + return resBody, header, err + } func Validate(scmId string, req *http.Request) ([]byte, error) { @@ -658,10 +659,15 @@ func GetNodesDetail(projectName, pipelineName, runId string, req *http.Request) // create jenkins request func sendJenkinsRequest(baseUrl string, req *http.Request) ([]byte, error) { + resBody, _, err := jenkinsClient(baseUrl, req) + return resBody, err +} + +func jenkinsClient(baseUrl string, req *http.Request) ([]byte, http.Header, error) { newReqUrl, err := url.Parse(baseUrl) if err != nil { log.Error(err) - return nil, err + return nil, nil, err } client := &http.Client{Timeout: 30 * time.Second} @@ -678,19 +684,21 @@ func sendJenkinsRequest(baseUrl string, req *http.Request) ([]byte, error) { resp, err := client.Do(newRequest) if err != nil { log.Error(err) - return nil, err + return nil, nil, err } - defer resp.Body.Close() resBody, _ := getRespBody(resp) + defer resp.Body.Close() + if resp.StatusCode >= http.StatusBadRequest { jkerr := new(JkError) jkerr.Code = resp.StatusCode jkerr.Message = http.StatusText(resp.StatusCode) - return nil, jkerr + return nil, nil, jkerr } - return resBody, err + return resBody, resp.Header, nil + } // Decompress response.body of JenkinsAPIResponse diff --git a/pkg/models/devops/urlconfig.go b/pkg/models/devops/urlconfig.go index a6df50c5f..fda36eb9e 100644 --- a/pkg/models/devops/urlconfig.go +++ b/pkg/models/devops/urlconfig.go @@ -19,29 +19,31 @@ package devops // Some apis for Jenkins. const ( - GetPipeBranchUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/?" + GetPipeBranchUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/?" GetBranchPipeUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/" - GetPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/" + GetPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/" SearchPipelineUrl = "/blue/rest/search/?" - RunBranchPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/" - RunPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/runs/" - GetPipelineRunUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/runs/%s/" - GetPipeBranchRunUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/" - SearchPipelineRunUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/runs/?" - GetBranchPipeRunNodesUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/nodes/?" - GetPipeRunNodesUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/runs/%s/nodes/?" - GetBranchRunLogUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/log/?" - GetRunLogUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/runs/%s/log/?" - GetBranchStepLogUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/nodes/%s/steps/%s/log/?" - GetStepLogUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/runs/%s/nodes/%s/steps/%s/log/?" + RunBranchPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/runs/" + RunPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/runs/" + GetPipelineRunUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/runs/%s/" + GetPipeBranchRunUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/runs/%s/" + SearchPipelineRunUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/runs/?" + GetBranchPipeRunNodesUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/runs/%s/nodes/?" + GetPipeRunNodesUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/runs/%s/nodes/?" + GetBranchRunLogUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/runs/%s/log/?" + GetRunLogUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/runs/%s/log/?" + GetBranchStepLogUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/runs/%s/nodes/%s/steps/%s/log/?" + GetStepLogUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/runs/%s/nodes/%s/steps/%s/log/?" StopBranchPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/runs/%s/stop/?" StopPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/runs/%s/stop/?" ReplayBranchPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/runs/%s/replay/" ReplayPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/runs/%s/replay/" - GetBranchArtifactsUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/artifacts/?" - GetArtifactsUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/runs/%s/artifacts/?" - GetBranchStepsStatusUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/nodes/%s/steps/?" + GetBranchArtifactsUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/runs/%s/artifacts/?" + GetArtifactsUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/runs/%s/artifacts/?" + GetBranchStepsStatusUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/runs/%s/nodes/%s/steps/?" + GetStepsStatusUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/runs/%s/nodes/%s/steps/?" + CheckBranchPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/runs/%s/nodes/%s/steps/%s/" CheckPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/runs/%s/nodes/%s/steps/%s/" GetBranchNodeStepsUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/nodes/%s/steps/?"