get runs & pipelines pageable resposne

Signed-off-by: runzexia <runzexia@yunify.com>
This commit is contained in:
runzexia
2019-09-10 11:06:25 +08:00
parent 8e66cea42b
commit 60220d9487
4 changed files with 151 additions and 10 deletions

File diff suppressed because one or more lines are too long

4
go.mod
View File

@@ -348,6 +348,10 @@ replace (
github.com/src-d/gcfg => github.com/src-d/gcfg v1.4.0
github.com/stretchr/objx => github.com/stretchr/objx v0.1.1
github.com/stretchr/testify => github.com/stretchr/testify v1.3.0
github.com/tidwall/gjson => github.com/tidwall/gjson v1.3.2
github.com/tidwall/match => github.com/tidwall/match v1.0.1
github.com/tidwall/pretty => github.com/tidwall/pretty v1.0.0
github.com/tidwall/sjson => github.com/tidwall/sjson v1.0.4
github.com/tmc/grpc-websocket-proxy => github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5
github.com/xanzy/ssh-agent => github.com/xanzy/ssh-agent v0.2.1
github.com/xiang90/probing => github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2

View File

@@ -247,8 +247,14 @@ The last one is encrypted info, such as the password of the username-password ty
Param(webservice.QueryParameter("limit", "the limit item count of the search.").
Required(false).
DataFormat("limit=%d")).
Returns(http.StatusOK, RespOK, []devops.Pipeline{}).
Writes([]devops.Pipeline{}))
Returns(http.StatusOK, RespOK, struct {
Items []devops.Pipeline `json:"items"`
Total int `json:"total_count"`
}{}).
Writes(struct {
Items []devops.Pipeline `json:"items"`
Total int `json:"total_count"`
}{}))
// match Jenkisn api "/blue/rest/organizations/jenkins/pipelines/{devops}/{pipeline}/runs/"
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/runs").
@@ -266,8 +272,22 @@ The last one is encrypted info, such as the password of the username-password ty
Param(webservice.QueryParameter("branch", "the name of branch, same as repository branch, will be filtered by branch.").
Required(false).
DataFormat("branch=%s")).
Returns(http.StatusOK, RespOK, []devops.BranchPipelineRun{}).
Writes([]devops.BranchPipelineRun{}))
Returns(http.StatusOK, RespOK, struct {
Items []devops.BranchPipelineRun `json:"items"`
Total int `json:"total_count"`
}{}).
Writes(struct {
Items []devops.BranchPipelineRun `json:"items"`
Total int `json:"total_count"`
}{}).
Writes(struct {
Items []devops.BranchPipelineRun `json:"items"`
Total int `json:"total_count"`
}{}).
Writes(struct {
Items []devops.BranchPipelineRun `json:"items"`
Total int `json:"total_count"`
}{}))
// match Jenkins api "/blue/rest/organizations/jenkins/pipelines/{devops}/{pipeline}/branches/{branch}/runs/{run}/"
webservice.Route(webservice.GET("/devops/{devops}/pipelines/{pipeline}/branches/{branch}/runs/{run}").

View File

@@ -26,10 +26,13 @@ import (
log "github.com/golang/glog"
"io"
"io/ioutil"
"k8s.io/klog"
"kubesphere.io/kubesphere/pkg/gojenkins"
"kubesphere.io/kubesphere/pkg/models"
"kubesphere.io/kubesphere/pkg/simple/client/admin_jenkins"
"net/http"
"net/url"
"strings"
"sync"
"time"
)
@@ -57,28 +60,104 @@ func GetPipeline(projectName, pipelineName string, req *http.Request) ([]byte, e
func SearchPipelines(req *http.Request) ([]byte, error) {
baseUrl := jenkins.Server + SearchPipelineUrl + req.URL.RawQuery
log.Infof("Jenkins-url: " + baseUrl)
klog.V(3).Infof("Jenkins-url: " + baseUrl)
res, err := sendJenkinsRequest(baseUrl, req)
if err != nil {
log.Error(err)
klog.Error(err)
return nil, err
}
count, err := searchPipelineCount(req)
if err != nil {
klog.Error(err)
return nil, err
}
responseStruct := models.PageableResponse{TotalCount: count}
err = json.Unmarshal(res, &responseStruct.Items)
if err != nil {
klog.Error(err)
return nil, err
}
res, err = json.Marshal(responseStruct)
if err != nil {
klog.Error(err)
return nil, err
}
return res, err
}
func searchPipelineCount(req *http.Request) (int, error) {
query, _ := parseJenkinsQuery(req.URL.RawQuery)
query.Set("start", "0")
query.Set("limit", "1000")
query.Set("depth", "-1")
baseUrl := jenkins.Server + SearchPipelineUrl + query.Encode()
klog.Infof("Jenkins-url: " + baseUrl)
res, err := sendJenkinsRequest(baseUrl, req)
if err != nil {
klog.Error(err)
return 0, err
}
var pipelines []Pipeline
err = json.Unmarshal(res, &pipelines)
if err != nil {
klog.Error(err)
return 0, err
}
return len(pipelines), nil
}
func searchPipelineRunsCount(projectName, pipelineName string, req *http.Request) (int, error) {
query, _ := parseJenkinsQuery(req.URL.RawQuery)
query.Set("start", "0")
query.Set("limit", "1000")
query.Set("depth", "-1")
baseUrl := fmt.Sprintf(jenkins.Server+SearchPipelineRunUrl, projectName, pipelineName)
klog.V(3).Info("Jenkins-url: " + baseUrl)
res, err := sendJenkinsRequest(baseUrl+query.Encode(), req)
if err != nil {
klog.Error(err)
return 0, err
}
var runs []PipelineRun
err = json.Unmarshal(res, &runs)
if err != nil {
klog.Error(err)
return 0, err
}
return len(runs), nil
}
func SearchPipelineRuns(projectName, pipelineName string, req *http.Request) ([]byte, error) {
baseUrl := fmt.Sprintf(jenkins.Server+SearchPipelineRunUrl, projectName, pipelineName)
log.Info("Jenkins-url: " + baseUrl)
klog.V(3).Info("Jenkins-url: " + baseUrl)
res, err := sendJenkinsRequest(baseUrl+req.URL.RawQuery, req)
if err != nil {
log.Error(err)
return nil, err
}
count, err := searchPipelineRunsCount(projectName, pipelineName, req)
if err != nil {
klog.Error(err)
return nil, err
}
responseStruct := models.PageableResponse{TotalCount: count}
err = json.Unmarshal(res, &responseStruct.Items)
if err != nil {
klog.Error(err)
return nil, err
}
res, err = json.Marshal(responseStruct)
if err != nil {
klog.Error(err)
return nil, err
}
return res, err
}
@@ -804,3 +883,41 @@ func getRespBody(resp *http.Response) ([]byte, error) {
return resBody, err
}
// parseJenkinsQuery Parse the special query of jenkins.
// ParseQuery in the standard library makes the query not re-encode
func parseJenkinsQuery(query string) (url.Values, error) {
m := make(url.Values)
err := error(nil)
for query != "" {
key := query
if i := strings.IndexAny(key, "&"); i >= 0 {
key, query = key[:i], key[i+1:]
} else {
query = ""
}
if key == "" {
continue
}
value := ""
if i := strings.Index(key, "="); i >= 0 {
key, value = key[:i], key[i+1:]
}
key, err1 := url.QueryUnescape(key)
if err1 != nil {
if err == nil {
err = err1
}
continue
}
value, err1 = url.QueryUnescape(value)
if err1 != nil {
if err == nil {
err = err1
}
continue
}
m[key] = append(m[key], value)
}
return m, err
}