-
Notifications
You must be signed in to change notification settings - Fork 89
/
handler.go
96 lines (80 loc) · 3.06 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package pipeline
import (
"context"
"encoding/json"
"fmt"
"github.com/emicklei/go-restful"
"github.com/jenkins-zh/jenkins-client/pkg/job"
"k8s.io/klog"
"kubesphere.io/devops/pkg/api"
"kubesphere.io/devops/pkg/api/devops/v1alpha3"
"kubesphere.io/devops/pkg/apiserver/query"
modelpipeline "kubesphere.io/devops/pkg/models/pipeline"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type apiHandlerOption struct {
client client.Client
}
type apiHandler struct {
apiHandlerOption
}
func newAPIHandler(option apiHandlerOption) *apiHandler {
return &apiHandler{
apiHandlerOption: option,
}
}
func (h *apiHandler) getBranches(request *restful.Request, response *restful.Response) {
namespaceName := request.PathParameter("namespace")
pipelineName := request.PathParameter("pipeline")
filter := job.Filter(request.QueryParameter("filter"))
// get pipelinerun
pipeline := &v1alpha3.Pipeline{}
if err := h.client.Get(context.Background(), client.ObjectKey{Namespace: namespaceName, Name: pipelineName}, pipeline); err != nil {
api.HandleError(request, response, err)
return
}
if pipeline.Spec.Type != v1alpha3.MultiBranchPipelineType {
api.HandleBadRequest(response, request, fmt.Errorf("invalid multi-branch Pipeline provided"))
return
}
branchesJSON := pipeline.Annotations[v1alpha3.PipelineJenkinsBranchesAnnoKey]
branches := []modelpipeline.Branch{}
if err := json.Unmarshal([]byte(branchesJSON), &branches); err != nil {
// ignore this error
klog.Errorf("unable to unmarshal branches JSON: %s, and err = %v", branchesJSON, err)
}
// filter branches with filter
branches = filterBranches(branches, filter)
queryParam := query.ParseQueryParameter(request)
total := len(branches)
startIndex, endIndex := queryParam.Pagination.GetValidPagination(total)
_ = response.WriteEntity(api.NewListResult(branchSlice(branches[startIndex:endIndex]).toGenericSlice(), total))
}
func (h *apiHandler) getBranch(request *restful.Request, response *restful.Response) {
namespaceName := request.PathParameter("namespace")
pipelineName := request.PathParameter("pipeline")
branch := request.PathParameter("branch")
// get pipelinerun
pipeline := &v1alpha3.Pipeline{}
if err := h.client.Get(context.Background(), client.ObjectKey{Namespace: namespaceName, Name: pipelineName}, pipeline); err != nil {
api.HandleError(request, response, err)
return
}
if pipeline.Spec.Type != v1alpha3.MultiBranchPipelineType {
api.HandleBadRequest(response, request, fmt.Errorf("invalid multi-branch Pipeline provided"))
return
}
branchesJSON := pipeline.Annotations[v1alpha3.PipelineJenkinsBranchesAnnoKey]
branches := []modelpipeline.Branch{}
if err := json.Unmarshal([]byte(branchesJSON), &branches); err != nil {
// ignore this error
klog.Errorf("unable to unmarshal branches JSON: %s, and err = %v", branchesJSON, err)
}
exist, searchedBranch := modelpipeline.BranchSlice(branches).SearchByName(branch)
if !exist {
// branch was not found
api.HandleNotFound(response, request, fmt.Errorf("Branch %s was not found", branch))
return
}
_ = response.WriteEntity(searchedBranch)
}