fix jenkins header

Signed-off-by: soulseen <sunzhu@yunify.com>
This commit is contained in:
soulseen
2019-05-16 16:10:34 +08:00
parent 273aa0102a
commit 157c1c37a7
3 changed files with 48 additions and 31 deletions

View File

@@ -104,11 +104,15 @@ func GetBranchStepLog(req *restful.Request, resp *restful.Response) {
nodeId := req.PathParameter("nodeId") nodeId := req.PathParameter("nodeId")
stepId := req.PathParameter("stepId") 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 { if err != nil {
parseErr(err, resp) parseErr(err, resp)
return return
} }
for k, v := range header {
resp.AddHeader(k, v[0])
}
resp.Write(res) resp.Write(res)
} }
@@ -119,11 +123,14 @@ func GetStepLog(req *restful.Request, resp *restful.Response) {
nodeId := req.PathParameter("nodeId") nodeId := req.PathParameter("nodeId")
stepId := req.PathParameter("stepId") 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 { if err != nil {
parseErr(err, resp) parseErr(err, resp)
return return
} }
for k, v := range header {
resp.AddHeader(k, v[0])
}
resp.Write(res) resp.Write(res)
} }

View File

@@ -106,30 +106,31 @@ func GetPipelineRunNodesbyBranch(projectName, pipelineName, branchName, runId st
return res, err 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) baseUrl := fmt.Sprintf(jenkins.Server+GetBranchStepLogUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId, nodeId, stepId)
log.Infof("Jenkins-url: " + baseUrl) log.Infof("Jenkins-url: " + baseUrl)
resBody, err := sendJenkinsRequest(baseUrl, req) resBody, header, err := jenkinsClient(baseUrl, req)
if err != nil { if err != nil {
log.Error(err) 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) baseUrl := fmt.Sprintf(jenkins.Server+GetStepLogUrl+req.URL.RawQuery, projectName, pipelineName, runId, nodeId, stepId)
log.Infof("Jenkins-url: " + baseUrl) log.Infof("Jenkins-url: " + baseUrl)
resBody, err := sendJenkinsRequest(baseUrl, req) resBody, header, err := jenkinsClient(baseUrl, req)
if err != nil { if err != nil {
log.Error(err) 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) { 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 // create jenkins request
func sendJenkinsRequest(baseUrl string, req *http.Request) ([]byte, error) { 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) newReqUrl, err := url.Parse(baseUrl)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return nil, err return nil, nil, err
} }
client := &http.Client{Timeout: 30 * time.Second} 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) resp, err := client.Do(newRequest)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return nil, err return nil, nil, err
} }
defer resp.Body.Close()
resBody, _ := getRespBody(resp) resBody, _ := getRespBody(resp)
defer resp.Body.Close()
if resp.StatusCode >= http.StatusBadRequest { if resp.StatusCode >= http.StatusBadRequest {
jkerr := new(JkError) jkerr := new(JkError)
jkerr.Code = resp.StatusCode jkerr.Code = resp.StatusCode
jkerr.Message = http.StatusText(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 // Decompress response.body of JenkinsAPIResponse

View File

@@ -19,29 +19,31 @@ package devops
// Some apis for Jenkins. // Some apis for Jenkins.
const ( 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/" 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/?" SearchPipelineUrl = "/blue/rest/search/?"
RunBranchPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/" RunBranchPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/runs/"
RunPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/runs/" RunPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/runs/"
GetPipelineRunUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/runs/%s/" GetPipelineRunUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/runs/%s/"
GetPipeBranchRunUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/" GetPipeBranchRunUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/runs/%s/"
SearchPipelineRunUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/runs/?" SearchPipelineRunUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/runs/?"
GetBranchPipeRunNodesUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/nodes/?" GetBranchPipeRunNodesUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/runs/%s/nodes/?"
GetPipeRunNodesUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/runs/%s/nodes/?" GetPipeRunNodesUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/runs/%s/nodes/?"
GetBranchRunLogUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/log/?" GetBranchRunLogUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/runs/%s/log/?"
GetRunLogUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/runs/%s/log/?" GetRunLogUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/runs/%s/log/?"
GetBranchStepLogUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/nodes/%s/steps/%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/%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/?" 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/?" 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/" 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/" 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/?" GetBranchArtifactsUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/runs/%s/artifacts/?"
GetArtifactsUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/runs/%s/artifacts/?" GetArtifactsUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/runs/%s/artifacts/?"
GetBranchStepsStatusUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/nodes/%s/steps/?" 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/?" 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/" 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/" 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/?" GetBranchNodeStepsUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/nodes/%s/steps/?"