-
Notifications
You must be signed in to change notification settings - Fork 89
/
handler.go
188 lines (164 loc) · 5.59 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package pipelinerun
import (
"context"
"encoding/json"
"io"
"strconv"
"github.com/emicklei/go-restful"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/klog"
"kubesphere.io/devops/pkg/api"
"kubesphere.io/devops/pkg/api/devops/v1alpha3"
"kubesphere.io/devops/pkg/apiserver/query"
"kubesphere.io/devops/pkg/client/devops"
"kubesphere.io/devops/pkg/models/pipelinerun"
resourcesV1alpha3 "kubesphere.io/devops/pkg/models/resources/v1alpha3"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// apiHandlerOption holds some useful tools for API handler.
type apiHandlerOption struct {
client client.Client
}
// apiHandler contains functions to handle coming request and give a response.
type apiHandler struct {
apiHandlerOption
}
// newAPIHandler creates an APIHandler.
func newAPIHandler(o apiHandlerOption) *apiHandler {
return &apiHandler{o}
}
func (h *apiHandler) listPipelineRuns(request *restful.Request, response *restful.Response) {
nsName := request.PathParameter("namespace")
pipName := request.PathParameter("pipeline")
branchName := request.QueryParameter("branch")
backward, err := strconv.ParseBool(request.QueryParameter("backward"))
if err != nil {
// by default, we have to guarantee backward compatibility
backward = true
}
queryParam := query.ParseQueryParameter(request)
// validate the Pipeline
pipeline := &v1alpha3.Pipeline{}
err = h.client.Get(context.Background(), client.ObjectKey{Namespace: nsName, Name: pipName}, pipeline)
if err != nil {
api.HandleError(request, response, err)
return
}
// build label selector
labelSelector, err := buildLabelSelector(queryParam, pipeline.Name, branchName)
if err != nil {
api.HandleError(request, response, err)
return
}
var prs v1alpha3.PipelineRunList
// fetch PipelineRuns
if err := h.client.List(context.Background(), &prs,
client.InNamespace(pipeline.Namespace),
client.MatchingLabelsSelector{Selector: labelSelector}); err != nil {
api.HandleError(request, response, err)
return
}
var listHandler resourcesV1alpha3.ListHandler
if backward {
listHandler = backwardListHandler{}
}
apiResult := resourcesV1alpha3.ToListResult(convertPipelineRunsToObject(prs.Items), queryParam, listHandler)
_ = response.WriteAsJson(apiResult)
go func() {
err := h.requestSyncPipelineRun(client.ObjectKey{Namespace: pipeline.Namespace, Name: pipeline.Name})
if err != nil {
klog.Errorf("failed to request to synchronize PipelineRuns. Pipeline = %s", pipeline.Name)
return
}
}()
}
func (h *apiHandler) requestSyncPipelineRun(key client.ObjectKey) error {
// get latest Pipeline
pipelineToUpdate := &v1alpha3.Pipeline{}
err := h.client.Get(context.Background(), key, pipelineToUpdate)
if err != nil {
return err
}
// update pipeline annotations
if pipelineToUpdate.Annotations == nil {
pipelineToUpdate.Annotations = make(map[string]string)
}
pipelineToUpdate.Annotations[v1alpha3.PipelineRequestToSyncRunsAnnoKey] = "true"
// update the pipeline
if err := h.client.Update(context.Background(), pipelineToUpdate); err != nil && !apierrors.IsConflict(err) {
// we allow the conflict error here
return err
}
return nil
}
func (h *apiHandler) createPipelineRun(request *restful.Request, response *restful.Response) {
nsName := request.PathParameter("namespace")
pipName := request.PathParameter("pipeline")
branch := request.QueryParameter("branch")
payload := devops.RunPayload{}
if err := request.ReadEntity(&payload); err != nil && err != io.EOF {
api.HandleBadRequest(response, request, err)
return
}
// validate the Pipeline
var pipeline v1alpha3.Pipeline
if err := h.client.Get(context.Background(), client.ObjectKey{Namespace: nsName, Name: pipName}, &pipeline); err != nil {
api.HandleError(request, response, err)
return
}
var (
scm *v1alpha3.SCM
err error
)
if scm, err = CreateScm(&pipeline.Spec, branch); err != nil {
api.HandleBadRequest(response, request, err)
return
}
// create PipelineRun
pr := CreatePipelineRun(&pipeline, &payload, scm)
if err := h.client.Create(context.Background(), pr); err != nil {
api.HandleError(request, response, err)
return
}
_ = response.WriteEntity(pr)
}
func (h *apiHandler) getPipelineRun(request *restful.Request, response *restful.Response) {
nsName := request.PathParameter("namespace")
prName := request.PathParameter("pipelinerun")
// get pipelinerun
var pr v1alpha3.PipelineRun
if err := h.client.Get(context.Background(), client.ObjectKey{Namespace: nsName, Name: prName}, &pr); err != nil {
api.HandleError(request, response, err)
return
}
_ = response.WriteEntity(&pr)
}
func (h *apiHandler) getNodeDetails(request *restful.Request, response *restful.Response) {
namespaceName := request.PathParameter("namespace")
pipelineRunName := request.PathParameter("pipelinerun")
// get pipelinerun
pr := &v1alpha3.PipelineRun{}
if err := h.client.Get(context.Background(), client.ObjectKey{Namespace: namespaceName, Name: pipelineRunName}, pr); err != nil {
api.HandleError(request, response, err)
return
}
// get stage status
stagesJSON, ok := pr.Annotations[v1alpha3.JenkinsPipelineRunStagesStatusAnnoKey]
if !ok {
// If the stages status dose not exist, set it as an empty array
stagesJSON = "[]"
}
stages := []pipelinerun.NodeDetail{}
if err := json.Unmarshal([]byte(stagesJSON), &stages); err != nil {
api.HandleError(request, response, err)
return
}
// TODO(johnniang): Check current user Handle the approvable field of NodeDetail
// this is a temporary solution of approvable
for i := range stages {
for j := range stages[i].Steps {
stages[i].Steps[j].Approvable = true
}
}
_ = response.WriteEntity(&stages)
}