diff --git a/pkg/apis/devops/v1alpha2/register.go b/pkg/apis/devops/v1alpha2/register.go index 7e4cfef8c2d2f185249064d3c72fded3bcf2824d..468b8ab21013e76f830682e281a566f71fbf93f1 100644 --- a/pkg/apis/devops/v1alpha2/register.go +++ b/pkg/apis/devops/v1alpha2/register.go @@ -307,7 +307,7 @@ func addWebService(c *restful.Container) error { // match "/blue/rest/organizations/jenkins/pipelines/{projectName}/{pipelineName}/branches/{branchName}/runs/{runId}/nodes/{nodeId}/steps/{stepId}/log/?start=0" webservice.Route(webservice.GET("/devops/{projectName}/pipelines/{pipelineName}/branches/{branchName}/runs/{runId}/nodes/{nodeId}/steps/{stepId}/log"). - To(devopsapi.GetStepLog). + To(devopsapi.GetBranchStepLog). Metadata(restfulspec.KeyOpenAPITags, tags). Doc("Get pipelines step log."). Produces("text/plain; charset=utf-8"). @@ -322,6 +322,22 @@ func addWebService(c *restful.Container) error { DataFormat("start=%d"). DefaultValue("start=0"))) + // match "/blue/rest/organizations/jenkins/pipelines/{projectName}/{pipelineName}/runs/{runId}/nodes/{nodeId}/steps/{stepId}/log/?start=0" + webservice.Route(webservice.GET("/devops/{projectName}/pipelines/{pipelineName}/runs/{runId}/nodes/{nodeId}/steps/{stepId}/log"). + To(devopsapi.GetStepLog). + Metadata(restfulspec.KeyOpenAPITags, tags). + Doc("Get pipelines step log."). + Produces("text/plain; charset=utf-8"). + Param(webservice.PathParameter("projectName", "devops project name")). + Param(webservice.PathParameter("pipelineName", "pipeline name")). + Param(webservice.PathParameter("runId", "pipeline runs id")). + Param(webservice.PathParameter("nodeId", "pipeline runs node id")). + Param(webservice.PathParameter("stepId", "pipeline runs step id")). + Param(webservice.QueryParameter("start", "start"). + Required(true). + DataFormat("start=%d"). + DefaultValue("start=0"))) + // match "/blue/rest/organizations/jenkins/scm/github/validate/" webservice.Route(webservice.PUT("/devops/scm/{scmId}/validate"). To(devopsapi.Validate). @@ -364,7 +380,7 @@ func addWebService(c *restful.Container) error { // match /blue/rest/organizations/jenkins/pipelines/{projectName}/pipelines/{pipelineName}/branches/{branchName}/runs/{runId}/stop/ webservice.Route(webservice.PUT("/devops/{projectName}/pipelines/{pipelineName}/branches/{branchName}/runs/{runId}/stop"). - To(devopsapi.StopPipeline). + To(devopsapi.StopBranchPipeline). Metadata(restfulspec.KeyOpenAPITags, tags). Doc("Stop pipeline in running"). Param(webservice.PathParameter("projectName", "devops project name")). @@ -382,9 +398,28 @@ func addWebService(c *restful.Container) error { Returns(http.StatusOK, RespOK, devops.StopPipe{}). Writes(devops.StopPipe{})) + // match /blue/rest/organizations/jenkins/pipelines/{projectName}/pipelines/{pipelineName}/runs/{runId}/stop/ + webservice.Route(webservice.PUT("/devops/{projectName}/pipelines/{pipelineName}/runs/{runId}/stop"). + To(devopsapi.StopPipeline). + Metadata(restfulspec.KeyOpenAPITags, tags). + Doc("Stop pipeline in running"). + Param(webservice.PathParameter("projectName", "devops project name")). + Param(webservice.PathParameter("pipelineName", "pipeline name")). + Param(webservice.PathParameter("runId", "pipeline runs id")). + Param(webservice.QueryParameter("blocking", "stop and between each retries will sleep"). + Required(false). + DataFormat("blocking=%t"). + DefaultValue("blocking=false")). + Param(webservice.QueryParameter("timeOutInSecs", "the time of stop and between each retries sleep"). + Required(false). + DataFormat("timeOutInSecs=%d"). + DefaultValue("timeOutInSecs=10")). + Returns(http.StatusOK, RespOK, devops.StopPipe{}). + Writes(devops.StopPipe{})) + // match /blue/rest/organizations/jenkins/pipelines/{projectName}/pipelines/{pipelineName}/branches/{branchName}/runs/{runId}/Replay/ webservice.Route(webservice.POST("/devops/{projectName}/pipelines/{pipelineName}/branches/{branchName}/runs/{runId}/replay"). - To(devopsapi.ReplayPipeline). + To(devopsapi.ReplayBranchPipeline). Metadata(restfulspec.KeyOpenAPITags, tags). Doc("Replay pipeline"). Param(webservice.PathParameter("projectName", "devops project name")). @@ -394,9 +429,20 @@ func addWebService(c *restful.Container) error { Returns(http.StatusOK, RespOK, devops.ReplayPipe{}). Writes(devops.ReplayPipe{})) + // match /blue/rest/organizations/jenkins/pipelines/{projectName}/pipelines/{pipelineName}/runs/{runId}/Replay/ + webservice.Route(webservice.POST("/devops/{projectName}/pipelines/{pipelineName}/runs/{runId}/replay"). + To(devopsapi.ReplayPipeline). + Metadata(restfulspec.KeyOpenAPITags, tags). + Doc("Replay pipeline"). + Param(webservice.PathParameter("projectName", "devops project name")). + Param(webservice.PathParameter("pipelineName", "pipeline name")). + Param(webservice.PathParameter("runId", "pipeline runs id")). + Returns(http.StatusOK, RespOK, devops.ReplayPipe{}). + Writes(devops.ReplayPipe{})) + // match /blue/rest/organizations/jenkins/pipelines/{projectName}/{pipelineName}/branches/{branchName}/runs/{runId}/log/?start=0 webservice.Route(webservice.GET("/devops/{projectName}/pipelines/{pipelineName}/branches/{branchName}/runs/{runId}/log"). - To(devopsapi.GetRunLog). + To(devopsapi.GetBranchRunLog). Metadata(restfulspec.KeyOpenAPITags, tags). Doc("Get Pipelines run log."). Produces("text/plain; charset=utf-8"). @@ -409,9 +455,23 @@ func addWebService(c *restful.Container) error { DataFormat("start=%d"). DefaultValue("start=0"))) + // match /blue/rest/organizations/jenkins/pipelines/{projectName}/{pipelineName}/runs/{runId}/log/?start=0 + webservice.Route(webservice.GET("/devops/{projectName}/pipelines/{pipelineName}/runs/{runId}/log"). + To(devopsapi.GetRunLog). + Metadata(restfulspec.KeyOpenAPITags, tags). + Doc("Get Pipelines run log."). + Produces("text/plain; charset=utf-8"). + Param(webservice.PathParameter("projectName", "devops project name")). + Param(webservice.PathParameter("pipelineName", "pipeline name")). + Param(webservice.PathParameter("runId", "pipeline runs id")). + Param(webservice.QueryParameter("start", "start"). + Required(true). + DataFormat("start=%d"). + DefaultValue("start=0"))) + // match /blue/rest/organizations/jenkins/pipelines/{projectName}/{pipelineName}/branches/{branchName}/runs/{runId}/artifacts webservice.Route(webservice.GET("/devops/{projectName}/pipelines/{pipelineName}/branches/{branchName}/runs/{runId}/artifacts"). - To(devopsapi.GetArtifacts). + To(devopsapi.GetBranchArtifacts). Metadata(restfulspec.KeyOpenAPITags, tags). Doc("Get pipeline artifacts."). Param(webservice.PathParameter("projectName", "devops project name")). @@ -427,6 +487,23 @@ func addWebService(c *restful.Container) error { Returns(http.StatusOK, "The filed of \"Url\" in response can download artifacts", []devops.Artifacts{}). Writes([]devops.Artifacts{})) + // match /blue/rest/organizations/jenkins/pipelines/{projectName}/{pipelineName}/runs/{runId}/artifacts + webservice.Route(webservice.GET("/devops/{projectName}/pipelines/{pipelineName}/runs/{runId}/artifacts"). + To(devopsapi.GetArtifacts). + Metadata(restfulspec.KeyOpenAPITags, tags). + Doc("Get pipeline artifacts."). + Param(webservice.PathParameter("projectName", "devops project name")). + Param(webservice.PathParameter("pipelineName", "pipeline name")). + Param(webservice.PathParameter("runId", "pipeline runs id")). + Param(webservice.QueryParameter("start", "start page"). + Required(false). + DataFormat("start=%d")). + Param(webservice.QueryParameter("limit", "limit count"). + Required(false). + DataFormat("limit=%d")). + Returns(http.StatusOK, "The filed of \"Url\" in response can download artifacts", []devops.Artifacts{}). + Writes([]devops.Artifacts{})) + // match /blue/rest/organizations/jenkins/pipelines/{projectName}/{pipelineName}/branches/?filter=&start&limit= webservice.Route(webservice.GET("/devops/{projectName}/pipelines/{pipelineName}/branches"). To(devopsapi.GetPipeBranch). @@ -448,7 +525,7 @@ func addWebService(c *restful.Container) error { // /blue/rest/organizations/jenkins/pipelines/{projectName}/pipelines/{pipelineName}/branches/{branchName}/runs/{runId}/nodes/{nodeId}/steps/{stepId} webservice.Route(webservice.POST("/devops/{projectName}/pipelines/{pipelineName}/branches/{branchName}/runs/{runId}/nodes/{nodeId}/steps/{stepId}"). - To(devopsapi.CheckPipeline). + To(devopsapi.CheckBranchPipeline). Metadata(restfulspec.KeyOpenAPITags, tags). Doc("Pauses pipeline execution and allows the user to interact and control the flow of the build."). Reads(devops.CheckPlayload{}). @@ -460,6 +537,19 @@ func addWebService(c *restful.Container) error { Param(webservice.PathParameter("nodeId", "pipeline node id")). Param(webservice.PathParameter("stepId", "pipeline step id"))) + // match /blue/rest/organizations/jenkins/pipelines/{projectName}/pipelines/{pipelineName}/runs/{runId}/nodes/{nodeId}/steps/{stepId} + webservice.Route(webservice.POST("/devops/{projectName}/pipelines/{pipelineName}/runs/{runId}/nodes/{nodeId}/steps/{stepId}"). + To(devopsapi.CheckPipeline). + Metadata(restfulspec.KeyOpenAPITags, tags). + Doc("Pauses pipeline execution and allows the user to interact and control the flow of the build."). + Reads(devops.CheckPlayload{}). + Produces("text/plain; charset=utf-8"). + Param(webservice.PathParameter("projectName", "devops project name")). + Param(webservice.PathParameter("pipelineName", "pipeline name")). + Param(webservice.PathParameter("runId", "pipeline runs id")). + Param(webservice.PathParameter("nodeId", "pipeline node id")). + Param(webservice.PathParameter("stepId", "pipeline step id"))) + // match /job/project-8QnvykoJw4wZ/job/test-1/indexing/consoleText webservice.Route(webservice.GET("/devops/{projectName}/pipelines/{pipelineName}/console/log"). To(devopsapi.GetConsoleLog). @@ -495,7 +585,7 @@ func addWebService(c *restful.Container) error { // match /pipeline_status/blue/rest/organizations/jenkins/pipelines/{projectName}/{pipelineName}/branches/{branchName}/runs/{runId}/nodes/?limit= webservice.Route(webservice.GET("/devops/{projectName}/pipelines/{pipelineName}/branches/{branchName}/runs/{runId}/nodes/{nodeId}/steps/status"). - To(devopsapi.GetStepsStatus). + To(devopsapi.GetBranchStepsStatus). Metadata(restfulspec.KeyOpenAPITags, tags). Doc("Get pipeline steps status."). Param(webservice.PathParameter("projectName", "devops project name")). @@ -509,6 +599,21 @@ func addWebService(c *restful.Container) error { Returns(http.StatusOK, RespOK, []devops.QueuedBlueRun{}). Writes([]devops.QueuedBlueRun{})) + // match /pipeline_status/blue/rest/organizations/jenkins/pipelines/{projectName}/{pipelineName}/runs/{runId}/nodes/?limit= + webservice.Route(webservice.GET("/devops/{projectName}/pipelines/{pipelineName}/runs/{runId}/nodes/{nodeId}/steps/status"). + To(devopsapi.GetStepsStatus). + Metadata(restfulspec.KeyOpenAPITags, tags). + Doc("Get pipeline steps status."). + Param(webservice.PathParameter("projectName", "devops project name")). + Param(webservice.PathParameter("pipelineName", "pipeline name")). + Param(webservice.PathParameter("runId", "pipeline run name")). + Param(webservice.PathParameter("nodeId", "pipeline node id")). + Param(webservice.QueryParameter("limit", "limit count"). + Required(true). + DataFormat("limit=%d")). + Returns(http.StatusOK, RespOK, []devops.QueuedBlueRun{}). + Writes([]devops.QueuedBlueRun{})) + // match /crumbIssuer/api/json/ webservice.Route(webservice.GET("/devops/crumbissuer"). To(devopsapi.GetCrumb). @@ -579,7 +684,7 @@ func addWebService(c *restful.Container) error { // match /blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/nodes/%s/steps/?limit= webservice.Route(webservice.GET("/devops/{projectName}/pipelines/{pipelineName}/branches/{branchName}/runs/{runId}/nodes/{nodeId}/steps"). - To(devopsapi.GetNodeSteps). + To(devopsapi.GetBranchNodeSteps). Metadata(restfulspec.KeyOpenAPITags, tags). Doc("Get steps in node."). Param(webservice.PathParameter("projectName", "devops project name")). @@ -587,6 +692,21 @@ func addWebService(c *restful.Container) error { Param(webservice.PathParameter("branchName", "pipeline branch name")). Param(webservice.PathParameter("runId", "pipeline run id")). Param(webservice.PathParameter("nodeId", "pipeline node id")). + Param(webservice.QueryParameter("limit", "limit count"). + Required(false). + DataFormat("limit=%d")). + Returns(http.StatusOK, RespOK, []devops.NodeSteps{}). + Writes([]devops.NodeSteps{})) + + // match /blue/rest/organizations/jenkins/pipelines/%s/%s/runs/%s/nodes/%s/steps/?limit= + webservice.Route(webservice.GET("/devops/{projectName}/pipelines/{pipelineName}/runs/{runId}/nodes/{nodeId}/steps"). + To(devopsapi.GetNodeSteps). + Metadata(restfulspec.KeyOpenAPITags, tags). + Doc("Get steps in node."). + Param(webservice.PathParameter("projectName", "devops project name")). + Param(webservice.PathParameter("pipelineName", "pipeline name")). + Param(webservice.PathParameter("runId", "pipeline run id")). + Param(webservice.PathParameter("nodeId", "pipeline node id")). Param(webservice.QueryParameter("limit", "limit count"). Required(false). DataFormat("limit=%d")). @@ -601,7 +721,7 @@ func addWebService(c *restful.Container) error { Produces("application/json", "charset=utf-8"). Doc("Json to Jenkinsfile."). Reads(devops.ReqJson{}). - Returns(http.StatusOK, RespOK,devops.NodeSteps{}). + Returns(http.StatusOK, RespOK, devops.NodeSteps{}). Writes(devops.ResJenkinsfile{})) // match /pipeline-model-converter/toJson @@ -612,7 +732,7 @@ func addWebService(c *restful.Container) error { Produces("application/json", "charset=utf-8"). Doc("Jenkinsfile to Json."). Reads(devops.ReqJenkinsfile{}). - Returns(http.StatusOK, RespOK,devops.ResJson{}). + Returns(http.StatusOK, RespOK, devops.ResJson{}). Writes(devops.ResJson{})) // match /git/notifyCommit/?url= @@ -641,6 +761,36 @@ func addWebService(c *restful.Container) error { Metadata(restfulspec.KeyOpenAPITags, tags). Doc("receive webhook request.")) + // in scm get all steps in nodes. + webservice.Route(webservice.GET("/devops/{projectName}/pipelines/{pipelineName}/branches/{branchName}/runs/{runId}/nodesdetail"). + To(devopsapi.GetBranchNodesDetail). + Metadata(restfulspec.KeyOpenAPITags, tags). + Doc("Get pipeline nodes stages detail"). + Param(webservice.PathParameter("pipelineName", "pipeline name")). + Param(webservice.PathParameter("projectName", "devops project name")). + Param(webservice.PathParameter("branchName", "pipeline branch name")). + Param(webservice.PathParameter("runId", "pipeline runs id")). + Param(webservice.QueryParameter("limit", "limit count"). + Required(true). + DataFormat("limit=%d")). + Returns(http.StatusOK, RespOK,[]devops.NodesDetail{}). + Writes(devops.NodesDetail{})) + + // out of scm get all steps in nodes. + webservice.Route(webservice.GET("/devops/{projectName}/pipelines/{pipelineName}/runs/{runId}/nodesdetail"). + To(devopsapi.GetNodesDetail). + Metadata(restfulspec.KeyOpenAPITags, tags). + Doc("Get pipeline nodes stages detail"). + Param(webservice.PathParameter("pipelineName", "pipeline name")). + Param(webservice.PathParameter("projectName", "devops project name")). + Param(webservice.PathParameter("branchName", "pipeline branch name")). + Param(webservice.PathParameter("runId", "pipeline runs id")). + Param(webservice.QueryParameter("limit", "limit count"). + Required(true). + DataFormat("limit=%d")). + Returns(http.StatusOK, RespOK,[]devops.NodesDetail{}). + Writes(devops.NodesDetail{})) + c.Add(webservice) return nil diff --git a/pkg/apiserver/devops/devops.go b/pkg/apiserver/devops/devops.go index 870e91d096c3de165ee15ab9f61abd7733bc1f1b..1a5d2db14090dbd606921f82709774b94d083272 100644 --- a/pkg/apiserver/devops/devops.go +++ b/pkg/apiserver/devops/devops.go @@ -96,7 +96,7 @@ func GetPipelineRunNodesbyBranch(req *restful.Request, resp *restful.Response) { resp.Write(res) } -func GetStepLog(req *restful.Request, resp *restful.Response) { +func GetBranchStepLog(req *restful.Request, resp *restful.Response) { projectName := req.PathParameter("projectName") pipelineName := req.PathParameter("pipelineName") branchName := req.PathParameter("branchName") @@ -104,7 +104,22 @@ func GetStepLog(req *restful.Request, resp *restful.Response) { nodeId := req.PathParameter("nodeId") stepId := req.PathParameter("stepId") - res, err := devops.GetStepLog(projectName, pipelineName, branchName, runId, nodeId, stepId, req.Request) + res, err := devops.GetBranchStepLog(projectName, pipelineName, branchName, runId, nodeId, stepId, req.Request) + if err != nil { + parseErr(err, resp) + return + } + resp.Write(res) +} + +func GetStepLog(req *restful.Request, resp *restful.Response) { + projectName := req.PathParameter("projectName") + pipelineName := req.PathParameter("pipelineName") + runId := req.PathParameter("runId") + nodeId := req.PathParameter("nodeId") + stepId := req.PathParameter("stepId") + + res, err := devops.GetStepLog(projectName, pipelineName, runId, nodeId, stepId, req.Request) if err != nil { parseErr(err, resp) return @@ -152,13 +167,44 @@ func GetOrgRepo(req *restful.Request, resp *restful.Response) { resp.Write(res) } +func StopBranchPipeline(req *restful.Request, resp *restful.Response) { + projectName := req.PathParameter("projectName") + pipelineName := req.PathParameter("pipelineName") + branchName := req.PathParameter("branchName") + runId := req.PathParameter("runId") + + res, err := devops.StopBranchPipeline(projectName, pipelineName, branchName, runId, req.Request) + if err != nil { + parseErr(err, resp) + return + } + + resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON) + resp.Write(res) +} + func StopPipeline(req *restful.Request, resp *restful.Response) { + projectName := req.PathParameter("projectName") + pipelineName := req.PathParameter("pipelineName") + runId := req.PathParameter("runId") + + res, err := devops.StopPipeline(projectName, pipelineName, runId, req.Request) + if err != nil { + parseErr(err, resp) + return + } + + resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON) + resp.Write(res) +} + +func ReplayBranchPipeline(req *restful.Request, resp *restful.Response) { projectName := req.PathParameter("projectName") pipelineName := req.PathParameter("pipelineName") branchName := req.PathParameter("branchName") runId := req.PathParameter("runId") - res, err := devops.StopPipeline(projectName, pipelineName, branchName, runId, req.Request) + res, err := devops.ReplayBranchPipeline(projectName, pipelineName, branchName, runId, req.Request) if err != nil { parseErr(err, resp) return @@ -171,10 +217,9 @@ func StopPipeline(req *restful.Request, resp *restful.Response) { func ReplayPipeline(req *restful.Request, resp *restful.Response) { projectName := req.PathParameter("projectName") pipelineName := req.PathParameter("pipelineName") - branchName := req.PathParameter("branchName") runId := req.PathParameter("runId") - res, err := devops.ReplayPipeline(projectName, pipelineName, branchName, runId, req.Request) + res, err := devops.ReplayPipeline(projectName, pipelineName, runId, req.Request) if err != nil { parseErr(err, resp) return @@ -184,13 +229,13 @@ func ReplayPipeline(req *restful.Request, resp *restful.Response) { resp.Write(res) } -func GetRunLog(req *restful.Request, resp *restful.Response) { +func GetBranchRunLog(req *restful.Request, resp *restful.Response) { projectName := req.PathParameter("projectName") pipelineName := req.PathParameter("pipelineName") branchName := req.PathParameter("branchName") runId := req.PathParameter("runId") - res, err := devops.GetRunLog(projectName, pipelineName, branchName, runId, req.Request) + res, err := devops.GetBranchRunLog(projectName, pipelineName, branchName, runId, req.Request) if err != nil { parseErr(err, resp) return @@ -199,13 +244,41 @@ func GetRunLog(req *restful.Request, resp *restful.Response) { resp.Write(res) } -func GetArtifacts(req *restful.Request, resp *restful.Response) { +func GetRunLog(req *restful.Request, resp *restful.Response) { + projectName := req.PathParameter("projectName") + pipelineName := req.PathParameter("pipelineName") + runId := req.PathParameter("runId") + + res, err := devops.GetRunLog(projectName, pipelineName, runId, req.Request) + if err != nil { + parseErr(err, resp) + return + } + + resp.Write(res) +} + +func GetBranchArtifacts(req *restful.Request, resp *restful.Response) { projectName := req.PathParameter("projectName") pipelineName := req.PathParameter("pipelineName") branchName := req.PathParameter("branchName") runId := req.PathParameter("runId") - res, err := devops.GetArtifacts(projectName, pipelineName, branchName, runId, req.Request) + res, err := devops.GetBranchArtifacts(projectName, pipelineName, branchName, runId, req.Request) + if err != nil { + parseErr(err, resp) + return + } + resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON) + resp.Write(res) +} + +func GetArtifacts(req *restful.Request, resp *restful.Response) { + projectName := req.PathParameter("projectName") + pipelineName := req.PathParameter("pipelineName") + runId := req.PathParameter("runId") + + res, err := devops.GetArtifacts(projectName, pipelineName, runId, req.Request) if err != nil { parseErr(err, resp) return @@ -227,7 +300,7 @@ func GetPipeBranch(req *restful.Request, resp *restful.Response) { resp.Write(res) } -func CheckPipeline(req *restful.Request, resp *restful.Response) { +func CheckBranchPipeline(req *restful.Request, resp *restful.Response) { projectName := req.PathParameter("projectName") pipelineName := req.PathParameter("pipelineName") branchName := req.PathParameter("branchName") @@ -235,7 +308,23 @@ func CheckPipeline(req *restful.Request, resp *restful.Response) { nodeId := req.PathParameter("nodeId") stepId := req.PathParameter("stepId") - res, err := devops.CheckPipeline(projectName, pipelineName, branchName, runId, nodeId, stepId, req.Request) + res, err := devops.CheckBranchPipeline(projectName, pipelineName, branchName, runId, nodeId, stepId, req.Request) + if err != nil { + parseErr(err, resp) + return + } + + resp.Write(res) +} + +func CheckPipeline(req *restful.Request, resp *restful.Response) { + projectName := req.PathParameter("projectName") + pipelineName := req.PathParameter("pipelineName") + runId := req.PathParameter("runId") + nodeId := req.PathParameter("nodeId") + stepId := req.PathParameter("stepId") + + res, err := devops.CheckPipeline(projectName, pipelineName, runId, nodeId, stepId, req.Request) if err != nil { parseErr(err, resp) return @@ -285,14 +374,30 @@ func RunPipeline(req *restful.Request, resp *restful.Response) { resp.Write(res) } -func GetStepsStatus(req *restful.Request, resp *restful.Response) { +func GetBranchStepsStatus(req *restful.Request, resp *restful.Response) { projectName := req.PathParameter("projectName") pipelineName := req.PathParameter("pipelineName") branchName := req.PathParameter("branchName") runId := req.PathParameter("runId") nodeId := req.PathParameter("nodeId") - res, err := devops.GetStepsStatus(projectName, pipelineName, branchName, runId, nodeId, req.Request) + res, err := devops.GetBranchStepsStatus(projectName, pipelineName, branchName, runId, nodeId, req.Request) + if err != nil { + parseErr(err, resp) + return + } + + resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON) + resp.Write(res) +} + +func GetStepsStatus(req *restful.Request, resp *restful.Response) { + projectName := req.PathParameter("projectName") + pipelineName := req.PathParameter("pipelineName") + runId := req.PathParameter("runId") + nodeId := req.PathParameter("nodeId") + + res, err := devops.GetStepsStatus(projectName, pipelineName, runId, nodeId, req.Request) if err != nil { parseErr(err, resp) return @@ -392,14 +497,29 @@ func GetPipelineRunNodes(req *restful.Request, resp *restful.Response) { resp.Write(res) } -func GetNodeSteps(req *restful.Request, resp *restful.Response) { +func GetBranchNodeSteps(req *restful.Request, resp *restful.Response) { projectName := req.PathParameter("projectName") pipelineName := req.PathParameter("pipelineName") branchName := req.PathParameter("branchName") runId := req.PathParameter("runId") nodeId := req.PathParameter("nodeId") - res, err := devops.GetNodeSteps(projectName, pipelineName, branchName, runId, nodeId, req.Request) + res, err := devops.GetBranchNodeSteps(projectName, pipelineName, branchName, runId, nodeId, req.Request) + if err != nil { + parseErr(err, resp) + return + } + resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON) + resp.Write(res) +} + +func GetNodeSteps(req *restful.Request, resp *restful.Response) { + projectName := req.PathParameter("projectName") + pipelineName := req.PathParameter("pipelineName") + runId := req.PathParameter("runId") + nodeId := req.PathParameter("nodeId") + + res, err := devops.GetNodeSteps(projectName, pipelineName, runId, nodeId, req.Request) if err != nil { parseErr(err, resp) return @@ -446,6 +566,33 @@ func GithubWebhook(req *restful.Request, resp *restful.Response) { resp.Write(res) } +func GetBranchNodesDetail(req *restful.Request, resp *restful.Response) { + projectName := req.PathParameter("projectName") + pipelineName := req.PathParameter("pipelineName") + branchName := req.PathParameter("branchName") + runId := req.PathParameter("runId") + + res, err := devops.GetBranchNodesDetail(projectName, pipelineName, branchName, runId, req.Request) + if err != nil { + parseErr(err, resp) + return + } + resp.WriteAsJson(res) +} + +func GetNodesDetail(req *restful.Request, resp *restful.Response) { + projectName := req.PathParameter("projectName") + pipelineName := req.PathParameter("pipelineName") + runId := req.PathParameter("runId") + + res, err := devops.GetNodesDetail(projectName, pipelineName, runId, req.Request) + if err != nil { + parseErr(err, resp) + return + } + resp.WriteAsJson(res) +} + func parseErr(err error, resp *restful.Response) { log.Error(err) if jErr, ok := err.(*devops.JkError); ok { diff --git a/pkg/models/devops/devops.go b/pkg/models/devops/devops.go index 8e78af07ff29d2e6b414b158c03a38f5c8ebb6df..9d77a3de55115a6f97692631b5fc0f8960fb3f56 100644 --- a/pkg/models/devops/devops.go +++ b/pkg/models/devops/devops.go @@ -19,6 +19,7 @@ package devops import ( "compress/gzip" + "encoding/json" "fmt" "github.com/PuerkitoBio/goquery" log "github.com/golang/glog" @@ -28,9 +29,12 @@ import ( "kubesphere.io/kubesphere/pkg/simple/client/admin_jenkins" "net/http" "net/url" + "sync" "time" ) +const channelMaxCapacity = 100 + var jenkins *gojenkins.Jenkins func PreCheckJenkins() { @@ -102,8 +106,21 @@ func GetPipelineRunNodesbyBranch(projectName, pipelineName, branchName, runId st return res, err } -func GetStepLog(projectName, pipelineName, branchName, runId, nodeId, stepId string, req *http.Request) ([]byte, error) { - baseUrl := fmt.Sprintf(jenkins.Server+GetStepLogUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId, nodeId, stepId) +func GetBranchStepLog(projectName, pipelineName, branchName, runId, nodeId, stepId string, req *http.Request) ([]byte, 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) + if err != nil { + log.Error(err) + return nil, err + } + + return resBody, err +} + +func GetStepLog(projectName, pipelineName, runId, nodeId, stepId string, req *http.Request) ([]byte, 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) @@ -154,8 +171,21 @@ func GetOrgRepo(scmId, organizationId string, req *http.Request) ([]byte, error) return res, err } -func StopPipeline(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) { - baseUrl := fmt.Sprintf(jenkins.Server+StopPipelineUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId) +func StopBranchPipeline(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) { + baseUrl := fmt.Sprintf(jenkins.Server+StopBranchPipelineUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId) + log.Infof("Jenkins-url: " + baseUrl) + + res, err := sendJenkinsRequest(baseUrl, req) + if err != nil { + log.Error(err) + return nil, err + } + + return res, err +} + +func StopPipeline(projectName, pipelineName, runId string, req *http.Request) ([]byte, error) { + baseUrl := fmt.Sprintf(jenkins.Server+StopPipelineUrl+req.URL.RawQuery, projectName, pipelineName, runId) log.Infof("Jenkins-url: " + baseUrl) res, err := sendJenkinsRequest(baseUrl, req) @@ -167,8 +197,8 @@ func StopPipeline(projectName, pipelineName, branchName, runId string, req *http return res, err } -func ReplayPipeline(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) { - baseUrl := fmt.Sprintf(jenkins.Server+ReplayPipelineUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId) +func ReplayBranchPipeline(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) { + baseUrl := fmt.Sprintf(jenkins.Server+ReplayBranchPipelineUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId) log.Infof("Jenkins-url: " + baseUrl) res, err := sendJenkinsRequest(baseUrl, req) @@ -180,8 +210,8 @@ func ReplayPipeline(projectName, pipelineName, branchName, runId string, req *ht return res, err } -func GetRunLog(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) { - baseUrl := fmt.Sprintf(jenkins.Server+GetRunLogUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId) +func ReplayPipeline(projectName, pipelineName, runId string, req *http.Request) ([]byte, error) { + baseUrl := fmt.Sprintf(jenkins.Server+ReplayPipelineUrl+req.URL.RawQuery, projectName, pipelineName, runId) log.Infof("Jenkins-url: " + baseUrl) res, err := sendJenkinsRequest(baseUrl, req) @@ -193,8 +223,47 @@ func GetRunLog(projectName, pipelineName, branchName, runId string, req *http.Re return res, err } -func GetArtifacts(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) { - baseUrl := fmt.Sprintf(jenkins.Server+GetArtifactsUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId) +func GetBranchRunLog(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) { + baseUrl := fmt.Sprintf(jenkins.Server+GetBranchRunLogUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId) + log.Infof("Jenkins-url: " + baseUrl) + + res, err := sendJenkinsRequest(baseUrl, req) + if err != nil { + log.Error(err) + return nil, err + } + + return res, err +} + +func GetRunLog(projectName, pipelineName, runId string, req *http.Request) ([]byte, error) { + baseUrl := fmt.Sprintf(jenkins.Server+GetRunLogUrl+req.URL.RawQuery, projectName, pipelineName, runId) + log.Infof("Jenkins-url: " + baseUrl) + + res, err := sendJenkinsRequest(baseUrl, req) + if err != nil { + log.Error(err) + return nil, err + } + + return res, err +} + +func GetBranchArtifacts(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) { + baseUrl := fmt.Sprintf(jenkins.Server+GetBranchArtifactsUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId) + log.Infof("Jenkins-url: " + baseUrl) + + res, err := sendJenkinsRequest(baseUrl, req) + if err != nil { + log.Error(err) + return nil, err + } + + return res, err +} + +func GetArtifacts(projectName, pipelineName, runId string, req *http.Request) ([]byte, error) { + baseUrl := fmt.Sprintf(jenkins.Server+GetArtifactsUrl+req.URL.RawQuery, projectName, pipelineName, runId) log.Infof("Jenkins-url: " + baseUrl) res, err := sendJenkinsRequest(baseUrl, req) @@ -219,8 +288,21 @@ func GetPipeBranch(projectName, pipelineName string, req *http.Request) ([]byte, return res, err } -func CheckPipeline(projectName, pipelineName, branchName, runId, nodeId, stepId string, req *http.Request) ([]byte, error) { - baseUrl := fmt.Sprintf(jenkins.Server+CheckPipelineUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId, nodeId, stepId) +func CheckBranchPipeline(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.Infof("Jenkins-url: " + baseUrl) + + resBody, err := sendJenkinsRequest(baseUrl, req) + if err != nil { + log.Error(err) + return nil, err + } + + return resBody, err +} + +func CheckPipeline(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.Infof("Jenkins-url: " + baseUrl) resBody, err := sendJenkinsRequest(baseUrl, req) @@ -271,8 +353,21 @@ func RunPipeline(projectName, pipelineName, branchName string, req *http.Request return res, err } -func GetStepsStatus(projectName, pipelineName, branchName, runId, nodeId string, req *http.Request) ([]byte, error) { - baseUrl := fmt.Sprintf(jenkins.Server+GetStepsStatusUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId, nodeId) +func GetBranchStepsStatus(projectName, pipelineName, branchName, runId, nodeId string, req *http.Request) ([]byte, error) { + baseUrl := fmt.Sprintf(jenkins.Server+GetBranchStepsStatusUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId, nodeId) + log.Infof("Jenkins-url: " + baseUrl) + + res, err := sendJenkinsRequest(baseUrl, req) + if err != nil { + log.Error(err) + return nil, err + } + + return res, err +} + +func GetStepsStatus(projectName, pipelineName, runId, nodeId string, req *http.Request) ([]byte, error) { + baseUrl := fmt.Sprintf(jenkins.Server+GetStepsStatusUrl+req.URL.RawQuery, projectName, pipelineName, runId, nodeId) log.Infof("Jenkins-url: " + baseUrl) res, err := sendJenkinsRequest(baseUrl, req) @@ -375,8 +470,21 @@ func GetPipelineRunNodes(projectName, pipelineName, runId string, req *http.Requ return res, err } -func GetNodeSteps(projectName, pipelineName, branchName, runId, nodeId string, req *http.Request) ([]byte, error) { - baseUrl := fmt.Sprintf(jenkins.Server+GetNodeStepsUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId, nodeId) +func GetBranchNodeSteps(projectName, pipelineName, branchName, runId, nodeId string, req *http.Request) ([]byte, error) { + baseUrl := fmt.Sprintf(jenkins.Server+GetBranchNodeStepsUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId, nodeId) + log.Infof("Jenkins-url: " + baseUrl) + + res, err := sendJenkinsRequest(baseUrl, req) + if err != nil { + log.Error(err) + return nil, err + } + + return res, err +} + +func GetNodeSteps(projectName, pipelineName, runId, nodeId string, req *http.Request) ([]byte, error) { + baseUrl := fmt.Sprintf(jenkins.Server+GetNodeStepsUrl+req.URL.RawQuery, projectName, pipelineName, runId, nodeId) log.Infof("Jenkins-url: " + baseUrl) res, err := sendJenkinsRequest(baseUrl, req) @@ -441,6 +549,100 @@ func GithubWebhook(req *http.Request) ([]byte, error) { return res, err } +func GetBranchNodesDetail(projectName, pipelineName, branchName, runId string, req *http.Request) ([]NodesDetail, error) { + getNodesUrl := fmt.Sprintf(jenkins.Server+GetBranchPipeRunNodesUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId) + log.Infof("getNodesUrl: " + getNodesUrl) + var wg sync.WaitGroup + var nodesDetails []NodesDetail + stepChan := make(chan *NodesStepsIndex, channelMaxCapacity) + + respNodes, err := GetPipelineRunNodesbyBranch(projectName, pipelineName, branchName, runId, req) + if err != nil { + log.Error(err) + return nil, err + } + err = json.Unmarshal(respNodes, &nodesDetails) + if err != nil { + log.Error(err) + return nil, err + } + + // get all steps in nodes. + for i, v := range nodesDetails { + wg.Add(1) + go func(nodeId string, index int) { + var steps []NodeSteps + respSteps, err := GetBranchNodeSteps(projectName, pipelineName, branchName, runId, nodeId, req) + if err != nil { + log.Error(err) + return + } + err = json.Unmarshal(respSteps, &steps) + + stepChan <- &NodesStepsIndex{index, steps} + wg.Done() + }(v.ID, i) + } + + wg.Wait() + close(stepChan) + + for oneNodeSteps := range stepChan { + if oneNodeSteps != nil { + nodesDetails[oneNodeSteps.Id].Steps = append(nodesDetails[oneNodeSteps.Id].Steps, oneNodeSteps.Steps...) + } + } + + return nodesDetails, err +} + +func GetNodesDetail(projectName, pipelineName, runId string, req *http.Request) ([]NodesDetail, error) { + getNodesUrl := fmt.Sprintf(jenkins.Server+GetPipeRunNodesUrl+req.URL.RawQuery, projectName, pipelineName, runId) + log.Infof("getNodesUrl: " + getNodesUrl) + var wg sync.WaitGroup + var nodesDetails []NodesDetail + stepChan := make(chan *NodesStepsIndex, channelMaxCapacity) + + respNodes, err := GetPipelineRunNodes(projectName, pipelineName, runId, req) + if err != nil { + log.Error(err) + return nil, err + } + err = json.Unmarshal(respNodes, &nodesDetails) + if err != nil { + log.Error(err) + return nil, err + } + + // get all steps in nodes. + for i, v := range nodesDetails { + wg.Add(1) + go func(nodeId string, index int) { + var steps []NodeSteps + respSteps, err := GetNodeSteps(projectName, pipelineName, runId, nodeId, req) + if err != nil { + log.Error(err) + return + } + err = json.Unmarshal(respSteps, &steps) + + stepChan <- &NodesStepsIndex{index, steps} + wg.Done() + }(v.ID, i) + } + + wg.Wait() + close(stepChan) + + for oneNodeSteps := range stepChan { + if oneNodeSteps != nil { + nodesDetails[oneNodeSteps.Id].Steps = append(nodesDetails[oneNodeSteps.Id].Steps, oneNodeSteps.Steps...) + } + } + + return nodesDetails, err +} + // create jenkins request func sendJenkinsRequest(baseUrl string, req *http.Request) ([]byte, error) { newReqUrl, err := url.Parse(baseUrl) diff --git a/pkg/models/devops/json.go b/pkg/models/devops/json.go index 2e9e22908b714b3e188c817acfcc2e00f811f1a7..2c87620a62191c05a5ab8e7ba0f832e0a3cb4ad0 100644 --- a/pkg/models/devops/json.go +++ b/pkg/models/devops/json.go @@ -946,7 +946,7 @@ type BranchPipeline struct { } `json:"branch,omitempty"` } -// GetPipeRunNodes +// GetPipelineRunNodes type PipelineRunNodes struct { Class string `json:"_class,omitempty"` Links struct { @@ -1056,3 +1056,45 @@ type ResJson struct { } `json:"json,omitempty"` } `json:"data,omitempty"` } + +type NodesDetail struct { + Class string `json:"_class,omitempty"` + Links struct { + Self struct { + Class string `json:"_class,omitempty"` + Href string `json:"href,omitempty"` + } `json:"self,omitempty"` + Actions struct { + Class string `json:"_class,omitempty"` + Href string `json:"href,omitempty"` + } `json:"actions,omitempty"` + Steps struct { + Class string `json:"_class,omitempty"` + Href string `json:"href,omitempty"` + } `json:"steps,omitempty"` + } `json:"_links,omitempty"` + Actions []interface{} `json:"actions,omitempty"` + DisplayDescription interface{} `json:"displayDescription,omitempty"` + DisplayName string `json:"displayName,omitempty"` + DurationInMillis int `json:"durationInMillis,omitempty"` + ID string `json:"id,omitempty"` + Input interface{} `json:"input,omitempty"` + Result string `json:"result,omitempty"` + StartTime string `json:"startTime,omitempty"` + State string `json:"state,omitempty"` + Type string `json:"type,omitempty"` + CauseOfBlockage interface{} `json:"causeOfBlockage,omitempty"` + Edges []struct { + Class string `json:"_class,omitempty"` + ID string `json:"id,omitempty"` + Type string `json:"type,omitempty"` + } `json:"edges,omitempty"` + FirstParent interface{} `json:"firstParent,omitempty"` + Restartable bool `json:"restartable,omitempty"` + Steps []NodeSteps `json:"steps,omitempty"` +} + +type NodesStepsIndex struct { + Id int `json:"id,omitempty"` + Steps []NodeSteps `json:"steps,omitempty"` +} diff --git a/pkg/models/devops/urlconfig.go b/pkg/models/devops/urlconfig.go index 545b6f3fc12a815653e37d4240f06fac7b5d8e46..d5ccdd797d9556091587c2ef99c3aa2b9403afe8 100644 --- a/pkg/models/devops/urlconfig.go +++ b/pkg/models/devops/urlconfig.go @@ -19,32 +19,40 @@ package devops // Some apis for Jenkins. const ( + GetPipeBranchUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/?" + GetBranchPipeUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/" GetPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/" SearchPipelineUrl = "/blue/rest/search/?" - SearchPipelineRunUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/runs/?" + RunPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%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/?" - GetStepLogUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/nodes/%s/steps/%s/log/?" + 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/?" + 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/?" + 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/?" + GetNodeStepsUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/runs/%s/nodes/%s/steps/?" ValidateUrl = "/blue/rest/organizations/jenkins/scm/%s/validate" GetSCMOrgUrl = "/blue/rest/organizations/jenkins/scm/%s/organizations/?" GetOrgRepoUrl = "/blue/rest/organizations/jenkins/scm/%s/organizations/%s/repositories/?" - StopPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/runs/%s/stop/?" - ReplayPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/runs/%s/replay/" - GetRunLogUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/log/?" - GetArtifactsUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/artifacts/?" - GetPipeBranchUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/?" GetConsoleLogUrl = "/job/%s/job/%s/indexing/consoleText" ScanBranchUrl = "/job/%s/job/%s/build?" - RunPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/" - GetStepsStatusUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/nodes/%s/steps/?" - CheckPipelineUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/runs/%s/nodes/%s/steps/%s/" GetCrumbUrl = "/crumbIssuer/api/json/" CheckScriptCompileUrl = "/job/init-job/descriptorByName/org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition/checkScriptCompile" CheckCronUrl = "/job/init-job/descriptorByName/hudson.triggers.TimerTrigger/checkSpec?" - GetPipelineRunUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/runs/%s/" - GetBranchPipeUrl = "/blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/" - GetPipeRunNodesUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/runs/%s/nodes/?" - GetNodeStepsUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/nodes/%s/steps/?" ToJenkinsfileUrl = "/pipeline-model-converter/toJenkinsfile" ToJsonUrl = "/pipeline-model-converter/toJson" GetNotifyCommitUrl = "/git/notifyCommit/?" diff --git a/pkg/simple/client/admin_jenkins/jenkins.go b/pkg/simple/client/admin_jenkins/jenkins.go index cd147b5ba8d739cb274625c9667d9284a5f0802a..13e6e6f27718f7b5a8617d610016251bcc365e59 100644 --- a/pkg/simple/client/admin_jenkins/jenkins.go +++ b/pkg/simple/client/admin_jenkins/jenkins.go @@ -21,7 +21,7 @@ import ( ) var ( - jenkinsClientOnce sync.Once + jenkinsInitMutex sync.Mutex jenkinsClient *gojenkins.Jenkins jenkinsAdminAddress string jenkinsAdminUsername string @@ -41,35 +41,36 @@ func init() { } func Client() *gojenkins.Jenkins { - jenkinsClientOnce.Do(func() { - jenkins := gojenkins.CreateJenkins(nil, jenkinsAdminAddress, jenkinsMaxConn, jenkinsAdminUsername, jenkinsAdminPassword) - jenkins, err := jenkins.Init() - if err != nil { - glog.Error("failed to connect jenkins") - return - } - jenkinsClient = jenkins - globalRole, err := jenkins.GetGlobalRole(JenkinsAllUserRoleName) - if err != nil { - glog.Error("failed to get jenkins role") - } - if globalRole == nil { - _, err := jenkins.AddGlobalRole(JenkinsAllUserRoleName, gojenkins.GlobalPermissionIds{ - GlobalRead: true, + if jenkinsClient == nil { + jenkinsInitMutex.Lock() + defer jenkinsInitMutex.Unlock() + if jenkinsClient == nil { + jenkins := gojenkins.CreateJenkins(nil, jenkinsAdminAddress, jenkinsMaxConn, jenkinsAdminUsername, jenkinsAdminPassword) + jenkins, err := jenkins.Init() + if err != nil { + glog.Errorf("failed to connect jenkins, %+v", err) + } + globalRole, err := jenkins.GetGlobalRole(JenkinsAllUserRoleName) + if err != nil { + glog.Errorf("failed to get jenkins role, %+v", err) + } + if globalRole == nil { + _, err := jenkins.AddGlobalRole(JenkinsAllUserRoleName, gojenkins.GlobalPermissionIds{ + GlobalRead: true, + }, true) + if err != nil { + glog.Errorf("failed to create jenkins global role, %+v", err) + } + } + _, err = jenkins.AddProjectRole(JenkinsAllUserRoleName, "\\n\\s*\\r", gojenkins.ProjectPermissionIds{ + SCMTag: true, }, true) if err != nil { - glog.Error("failed to create jenkins global role") - return + glog.Errorf("failed to create jenkins project role, %+v", err) } + jenkinsClient = jenkins } - _, err = jenkins.AddProjectRole(JenkinsAllUserRoleName, "\\n\\s*\\r", gojenkins.ProjectPermissionIds{ - SCMTag: true, - }, true) - if err != nil { - glog.Error("failed to create jenkins project role") - return - } - }) + } return jenkinsClient