提交 78f2dab1 编写于 作者: S soulseen 提交者: zryfish

add jenkins api & log

Signed-off-by: Nsoulseen <sunzhu@yunify.com>
上级 e64e8bb9
......@@ -53,13 +53,17 @@ func addWebService(c *restful.Container) error {
Metadata(restfulspec.KeyOpenAPITags, tags).
Doc("Search DevOps resource.").
Param(webservice.QueryParameter("q", "query pipelines").
Required(false)).
Required(false).
DataFormat("q=%s")).
Param(webservice.QueryParameter("filter", "filter resource").
Required(false)).
Required(false).
DataFormat("filter=%s")).
Param(webservice.QueryParameter("start", "start page").
Required(false)).
Required(false).
DataFormat("start=%d")).
Param(webservice.QueryParameter("limit", "limit count").
Required(false)))
Required(false).
DataFormat("limit=%d")))
// match Jenkisn api "/blue/rest/organizations/jenkins/pipelines/{projectName}/{pipelineName}/runs/"
webservice.Route(webservice.GET("/devops/{projectName}/pipelines/{pipelineName}/runs").
......@@ -69,9 +73,11 @@ func addWebService(c *restful.Container) error {
Param(webservice.PathParameter("pipelineName", "pipeline name")).
Param(webservice.PathParameter("projectName", "devops project name")).
Param(webservice.QueryParameter("start", "start page").
Required(false)).
Required(false).
DataFormat("start=%d")).
Param(webservice.QueryParameter("limit", "limit count").
Required(false)))
Required(false).
DataFormat("limit=%d")))
// match Jenkins api "/blue/rest/organizations/jenkins/pipelines/{projectName}/{pipelineName}/branches/{branchName}/runs/{runId}/"
webservice.Route(webservice.GET("/devops/{projectName}/pipelines/{pipelineName}/branches/{branchName}/runs/{runId}").
......@@ -113,6 +119,23 @@ func addWebService(c *restful.Container) error {
DataFormat("start=%d").
DefaultValue("start=0")))
// match "/blue/rest/organizations/jenkins/scm/github/validate/"
webservice.Route(webservice.PUT("/devops/scm/{scmId}/validate").
To(devops.Validate).
Metadata(restfulspec.KeyOpenAPITags, tags).
Doc("Validate Github personal access token.").
Param(webservice.PathParameter("scmId", "SCM id")))
// match "/blue/rest/organizations/jenkins/scm/{scmId}/organizations/?credentialId=github"
webservice.Route(webservice.GET("/devops/scm/{scmId}/organizations").
To(devops.GetOrgSCM).
Metadata(restfulspec.KeyOpenAPITags, tags).
Doc("List organizations of SCM").
Param(webservice.PathParameter("scmId", "SCM id")).
Param(webservice.QueryParameter("credentialId", "credentialId for SCM").
Required(true).
DataFormat("credentialId=%s")))
c.Add(webservice)
return nil
......
......@@ -19,6 +19,7 @@ package devops
import (
"github.com/emicklei/go-restful"
log "github.com/golang/glog"
"kubesphere.io/kubesphere/pkg/errors"
"kubesphere.io/kubesphere/pkg/models/devops"
"net/http"
......@@ -59,7 +60,6 @@ func SearchPipelineRuns(req *restful.Request, resp *restful.Response) {
}
_ = resp.WriteAsJson(res)
}
func GetPipelineRun(req *restful.Request, resp *restful.Response) {
......@@ -90,7 +90,6 @@ func GetPipelineRunNodes(req *restful.Request, resp *restful.Response) {
}
_ = resp.WriteAsJson(res)
}
func GetStepLog(req *restful.Request, resp *restful.Response) {
......@@ -107,10 +106,34 @@ func GetStepLog(req *restful.Request, resp *restful.Response) {
return
}
_, _ = resp.Write(res)
}
func Validate(req *restful.Request, resp *restful.Response) {
scmId := req.PathParameter("scmId")
res, err := devops.Validate(scmId, req.Request)
if err != nil {
parseErr(err, resp)
return
}
_, _ = resp.Write(res)
}
func GetOrgSCM(req *restful.Request, resp *restful.Response) {
scmId := req.PathParameter("scmId")
res, err := devops.GetOrgSCM(scmId, 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 {
_ = resp.WriteHeaderAndEntity(jErr.Code, err)
} else {
......
......@@ -42,6 +42,7 @@ func GetPipeline(projectName, pipelineName string, req *http.Request) (*Pipeline
resBody, err := jenkinsClient(baseUrl, req)
if err != nil {
log.Error(err)
return nil, err
}
......@@ -61,6 +62,7 @@ func SearchPipelines(req *http.Request) ([]interface{}, error) {
resBody, err := jenkinsClient(baseUrl, req)
if err != nil {
log.Error(err)
return nil, err
}
......@@ -80,6 +82,7 @@ func SearchPipelineRuns(projectName, pipelineName string, req *http.Request) ([]
resBody, err := jenkinsClient(baseUrl, req)
if err != nil {
log.Error(err)
return nil, err
}
......@@ -99,6 +102,7 @@ func GetPipelineRun(projectName, pipelineName, branchName, runId string, req *ht
resBody, err := jenkinsClient(baseUrl, req)
if err != nil {
log.Error(err)
return nil, err
}
......@@ -118,6 +122,7 @@ func GetPipelineRunNodes(projectName, pipelineName, branchName, runId string, re
resBody, err := jenkinsClient(baseUrl, req)
if err != nil {
log.Error(err)
return nil, err
}
......@@ -137,12 +142,46 @@ func GetStepLog(projectName, pipelineName, branchName, runId, nodeId, stepId str
resBody, err := jenkinsClient(baseUrl, req)
if err != nil {
log.Error(err)
return nil, err
}
return resBody, err
}
func Validate(scmId string, req *http.Request) ([]byte, error) {
baseUrl := fmt.Sprintf(JenkinsUrl+ValidateUrl, scmId)
log.Infof("Jenkins-url: " + baseUrl)
resBody, err := jenkinsClient(baseUrl, req)
if err != nil {
log.Error(err)
return nil, err
}
return resBody, err
}
func GetOrgSCM(scmId string, req *http.Request) ([]interface{}, error) {
baseUrl := fmt.Sprintf(JenkinsUrl+GetOrgSCMUrl+req.URL.RawQuery, scmId)
log.Infof("Jenkins-url: " + baseUrl)
resBody, err := jenkinsClient(baseUrl, req)
if err != nil {
log.Error(err)
return nil, err
}
var res []interface{}
err = json.Unmarshal(resBody, &res)
if err != nil {
log.Error(err)
return nil, err
}
return res, err
}
// create jenkins request
func jenkinsClient(baseUrl string, req *http.Request) ([]byte, error) {
newReqUrl, err := url.Parse(baseUrl)
......@@ -168,10 +207,14 @@ func jenkinsClient(baseUrl string, req *http.Request) ([]byte, error) {
defer resp.Body.Close()
resBody, _ := getRespBody(resp)
log.Info(string(resBody))
if resp.StatusCode >= http.StatusBadRequest {
jkerr := new(JkError)
_ = json.Unmarshal(resBody, jkerr)
log.Error(nil, jkerr)
err = json.Unmarshal(resBody, jkerr)
if err != nil {
log.Error(err)
return nil, err
}
return nil, jkerr
}
......
......@@ -25,4 +25,6 @@ const (
GetPipelineRunUrl = "/blue/rest/organizations/jenkins/pipelines/%s/%s/branches/%s/runs/%s/"
GetPipelineRunNodesUrl = "/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/?"
ValidateUrl = "/blue/rest/organizations/jenkins/scm/%s/validate"
GetOrgSCMUrl = "/blue/rest/organizations/jenkins/scm/%s/organizations/?"
)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册