devops.go 22.5 KB
Newer Older
S
sunzhu 已提交
1
/*
S
shaowenchen 已提交
2
Copyright 2020 The KubeSphere Authors.
S
sunzhu 已提交
3

S
shaowenchen 已提交
4 5 6
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
S
sunzhu 已提交
7

S
shaowenchen 已提交
8
    http://www.apache.org/licenses/LICENSE-2.0
S
sunzhu 已提交
9

S
shaowenchen 已提交
10 11 12 13 14
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
R
update  
runzexia 已提交
15
*/
S
shaowenchen 已提交
16

R
runzexia 已提交
17
package v1alpha2
S
sunzhu 已提交
18 19

import (
20 21 22
	"encoding/json"
	"errors"
	"fmt"
S
sunzhu 已提交
23
	"github.com/emicklei/go-restful"
24
	"k8s.io/apiserver/pkg/authentication/user"
J
Jeff 已提交
25
	log "k8s.io/klog"
R
runzexia 已提交
26
	"kubesphere.io/kubesphere/pkg/api"
27 28
	iamv1alpha2 "kubesphere.io/kubesphere/pkg/apis/iam/v1alpha2"
	"kubesphere.io/kubesphere/pkg/apiserver/request"
29
	"kubesphere.io/kubesphere/pkg/constants"
S
sunzhu 已提交
30
	"kubesphere.io/kubesphere/pkg/models/devops"
31
	clientDevOps "kubesphere.io/kubesphere/pkg/simple/client/devops"
S
sunzhu 已提交
32
	"net/http"
S
soulseen 已提交
33
	"strings"
S
sunzhu 已提交
34 35
)

S
soulseen 已提交
36
const jenkinsHeaderPre = "X-"
S
soulseen 已提交
37

R
runzexia 已提交
38
func (h *ProjectPipelineHandler) GetPipeline(req *restful.Request, resp *restful.Response) {
R
runzexia 已提交
39
	projectName := req.PathParameter("devops")
40
	pipelineName := req.PathParameter("pipeline")
S
sunzhu 已提交
41

R
runzexia 已提交
42
	res, err := h.devopsOperator.GetPipeline(projectName, pipelineName, req.Request)
S
sunzhu 已提交
43 44 45 46 47
	if err != nil {
		parseErr(err, resp)
		return
	}

48
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
R
runzexia 已提交
49
	resp.WriteAsJson(res)
S
sunzhu 已提交
50 51
}

R
runzexia 已提交
52 53
func (h *ProjectPipelineHandler) ListPipelines(req *restful.Request, resp *restful.Response) {
	res, err := h.devopsOperator.ListPipelines(req.Request)
S
sunzhu 已提交
54 55 56 57 58
	if err != nil {
		parseErr(err, resp)
		return
	}

Z
Zhuxiaoyang 已提交
59
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
R
runzexia 已提交
60
	resp.WriteAsJson(res)
S
sunzhu 已提交
61 62
}

R
runzexia 已提交
63
func (h *ProjectPipelineHandler) GetPipelineRun(req *restful.Request, resp *restful.Response) {
64 65
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
R
runzexia 已提交
66
	runId := req.PathParameter("run")
S
sunzhu 已提交
67

R
runzexia 已提交
68
	res, err := h.devopsOperator.GetPipelineRun(projectName, pipelineName, runId, req.Request)
S
sunzhu 已提交
69 70 71 72 73
	if err != nil {
		parseErr(err, resp)
		return
	}

Z
Zhuxiaoyang 已提交
74
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
R
runzexia 已提交
75
	resp.WriteAsJson(res)
S
sunzhu 已提交
76 77
}

R
runzexia 已提交
78
func (h *ProjectPipelineHandler) ListPipelineRuns(req *restful.Request, resp *restful.Response) {
79 80
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
S
sunzhu 已提交
81

R
runzexia 已提交
82
	res, err := h.devopsOperator.ListPipelineRuns(projectName, pipelineName, req.Request)
S
sunzhu 已提交
83 84 85 86 87
	if err != nil {
		parseErr(err, resp)
		return
	}

Z
Zhuxiaoyang 已提交
88
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
R
runzexia 已提交
89
	resp.WriteAsJson(res)
S
sunzhu 已提交
90 91
}

R
runzexia 已提交
92
func (h *ProjectPipelineHandler) StopPipeline(req *restful.Request, resp *restful.Response) {
93 94 95
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
	runId := req.PathParameter("run")
S
sunzhu 已提交
96

R
runzexia 已提交
97
	res, err := h.devopsOperator.StopPipeline(projectName, pipelineName, runId, req.Request)
S
sunzhu 已提交
98 99 100 101 102
	if err != nil {
		parseErr(err, resp)
		return
	}

Z
Zhuxiaoyang 已提交
103
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
R
runzexia 已提交
104
	resp.WriteAsJson(res)
S
sunzhu 已提交
105 106
}

R
runzexia 已提交
107
func (h *ProjectPipelineHandler) ReplayPipeline(req *restful.Request, resp *restful.Response) {
108 109 110
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
	runId := req.PathParameter("run")
S
soulseen 已提交
111

R
runzexia 已提交
112
	res, err := h.devopsOperator.ReplayPipeline(projectName, pipelineName, runId, req.Request)
Z
Zhuxiaoyang 已提交
113 114 115 116
	if err != nil {
		parseErr(err, resp)
		return
	}
R
runzexia 已提交
117 118 119

	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
	resp.WriteAsJson(res)
Z
Zhuxiaoyang 已提交
120 121
}

R
runzexia 已提交
122
func (h *ProjectPipelineHandler) RunPipeline(req *restful.Request, resp *restful.Response) {
123 124
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
Z
Zhuxiaoyang 已提交
125

R
runzexia 已提交
126
	res, err := h.devopsOperator.RunPipeline(projectName, pipelineName, req.Request)
S
sunzhu 已提交
127 128 129 130
	if err != nil {
		parseErr(err, resp)
		return
	}
R
runzexia 已提交
131 132 133

	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
	resp.WriteAsJson(res)
S
soulseen 已提交
134 135
}

R
runzexia 已提交
136 137 138 139
func (h *ProjectPipelineHandler) GetArtifacts(req *restful.Request, resp *restful.Response) {
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
	runId := req.PathParameter("run")
140

R
runzexia 已提交
141
	res, err := h.devopsOperator.GetArtifacts(projectName, pipelineName, runId, req.Request)
142 143 144 145 146
	if err != nil {
		parseErr(err, resp)
		return
	}
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
R
runzexia 已提交
147
	resp.WriteAsJson(res)
148 149
}

R
runzexia 已提交
150 151 152 153
func (h *ProjectPipelineHandler) GetRunLog(req *restful.Request, resp *restful.Response) {
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
	runId := req.PathParameter("run")
154

R
runzexia 已提交
155
	res, err := h.devopsOperator.GetRunLog(projectName, pipelineName, runId, req.Request)
156 157 158 159 160 161 162 163
	if err != nil {
		parseErr(err, resp)
		return
	}

	resp.Write(res)
}

R
runzexia 已提交
164 165 166 167 168 169
func (h *ProjectPipelineHandler) GetStepLog(req *restful.Request, resp *restful.Response) {
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
	runId := req.PathParameter("run")
	nodeId := req.PathParameter("node")
	stepId := req.PathParameter("step")
S
soulseen 已提交
170

R
runzexia 已提交
171
	res, header, err := h.devopsOperator.GetStepLog(projectName, pipelineName, runId, nodeId, stepId, req.Request)
S
soulseen 已提交
172
	if err != nil {
R
runzexia 已提交
173
		parseErr(err, resp)
S
soulseen 已提交
174 175
		return
	}
R
runzexia 已提交
176 177 178 179 180
	for k, v := range header {
		if strings.HasPrefix(k, jenkinsHeaderPre) {
			resp.AddHeader(k, v[0])
		}
	}
Z
Zhuxiaoyang 已提交
181
	resp.Write(res)
S
soulseen 已提交
182 183
}

R
runzexia 已提交
184 185 186 187 188
func (h *ProjectPipelineHandler) GetNodeSteps(req *restful.Request, resp *restful.Response) {
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
	runId := req.PathParameter("run")
	nodeId := req.PathParameter("node")
S
soulseen 已提交
189

R
runzexia 已提交
190
	res, err := h.devopsOperator.GetNodeSteps(projectName, pipelineName, runId, nodeId, req.Request)
Z
Zhuxiaoyang 已提交
191 192 193 194
	if err != nil {
		parseErr(err, resp)
		return
	}
Z
Zhuxiaoyang 已提交
195
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
R
runzexia 已提交
196
	resp.WriteAsJson(res)
Z
Zhuxiaoyang 已提交
197 198
}

R
runzexia 已提交
199 200 201 202
func (h *ProjectPipelineHandler) GetPipelineRunNodes(req *restful.Request, resp *restful.Response) {
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
	runId := req.PathParameter("run")
Z
Zhuxiaoyang 已提交
203

R
runzexia 已提交
204
	res, err := h.devopsOperator.GetPipelineRunNodes(projectName, pipelineName, runId, req.Request)
Z
Zhuxiaoyang 已提交
205 206 207 208
	if err != nil {
		parseErr(err, resp)
		return
	}
Z
Zhuxiaoyang 已提交
209
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
R
runzexia 已提交
210
	resp.WriteAsJson(res)
Z
Zhuxiaoyang 已提交
211 212
}

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
func (h *ProjectPipelineHandler) approvableCheck(nodes []clientDevOps.NodesDetail, req *restful.Request) {
	currentUserName, roleName := h.getCurrentUser(req)
	// check if current user belong to the admin group, grant it if it's true
	isAdmin := roleName == iamv1alpha2.PlatformAdmin

	for i, node := range nodes {
		if node.State != clientDevOps.StatePaused {
			continue
		}

		for j, step := range node.Steps {
			if step.State != clientDevOps.StatePaused || step.Input == nil {
				continue
			}

			nodes[i].Steps[j].Approvable = isAdmin || step.Input.Approvable(currentUserName)
		}
	}
}

233 234 235 236 237 238 239 240 241 242 243
func (h *ProjectPipelineHandler) createdBy(projectName string, pipelineName string, currentUserName string) bool {
	if pipeline, err := h.devopsOperator.GetPipelineObj(projectName, pipelineName); err == nil {
		if creator, ok := pipeline.Annotations[constants.CreatorAnnotationKey]; ok {
			return creator == currentUserName
		}
	} else {
		log.Error(fmt.Sprintf("cannot get pipeline %s/%s, error %#v", projectName, pipelineName, err))
	}
	return false
}

244
func (h *ProjectPipelineHandler) getCurrentUser(req *restful.Request) (username, roleName string) {
245 246
	var userInfo user.Info
	var ok bool
247 248
	var err error

249 250 251
	ctx := req.Request.Context()
	if userInfo, ok = request.UserFrom(ctx); ok {
		var role *iamv1alpha2.GlobalRole
252
		username = userInfo.GetName()
253
		if role, err = h.amInterface.GetGlobalRoleOfUser(username); err == nil {
254
			roleName = role.Name
255 256
		}
	}
257 258 259 260 261
	return
}

func (h *ProjectPipelineHandler) hasSubmitPermission(req *restful.Request) (hasPermit bool, err error) {
	currentUserName, roleName := h.getCurrentUser(req)
262 263 264 265
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
	// check if current user belong to the admin group or he's the owner, grant it if it's true
	if roleName == iamv1alpha2.PlatformAdmin || h.createdBy(projectName, pipelineName, currentUserName) {
266 267 268
		hasPermit = true
		return
	}
269 270 271 272 273 274 275 276 277

	// step 2, check if current user if was addressed
	httpReq := &http.Request{
		URL:      req.Request.URL,
		Header:   req.Request.Header,
		Form:     req.Request.Form,
		PostForm: req.Request.PostForm,
	}

278
	runId := req.PathParameter("run")
R
runzexia 已提交
279 280
	nodeId := req.PathParameter("node")
	stepId := req.PathParameter("step")
Z
Zhuxiaoyang 已提交
281

282
	// check if current user can approve this input
283 284 285 286 287 288 289 290 291 292 293 294
	var res []clientDevOps.NodesDetail
	if res, err = h.devopsOperator.GetNodesDetail(projectName, pipelineName, runId, httpReq); err == nil {
		for _, node := range res {
			if node.ID != nodeId {
				continue
			}

			for _, step := range node.Steps {
				if step.ID != stepId || step.Input == nil {
					continue
				}

295
				hasPermit = step.Input.Approvable(currentUserName)
296 297 298 299 300 301 302
				break
			}
			break
		}
	} else {
		log.Errorf("cannot get nodes detail, error: %v", err)
		err = errors.New("cannot get the submitters of current pipeline run")
Z
Zhuxiaoyang 已提交
303 304
		return
	}
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	return
}

func (h *ProjectPipelineHandler) SubmitInputStep(req *restful.Request, resp *restful.Response) {
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
	runId := req.PathParameter("run")
	nodeId := req.PathParameter("node")
	stepId := req.PathParameter("step")

	var (
		response []byte
		err      error
		ok       bool
	)

	if ok, err = h.hasSubmitPermission(req); !ok || err != nil {
		msg := map[string]string{
			"allow":   "false",
			"message": fmt.Sprintf("%v", err),
		}

		response, _ = json.Marshal(msg)
	} else {
		response, err = h.devopsOperator.SubmitInputStep(projectName, pipelineName, runId, nodeId, stepId, req.Request)
		if err != nil {
			parseErr(err, resp)
			return
		}
	}
	resp.Write(response)
Z
Zhuxiaoyang 已提交
336 337
}

R
runzexia 已提交
338
func (h *ProjectPipelineHandler) GetNodesDetail(req *restful.Request, resp *restful.Response) {
339 340 341
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
	runId := req.PathParameter("run")
Z
Zhuxiaoyang 已提交
342

R
runzexia 已提交
343
	res, err := h.devopsOperator.GetNodesDetail(projectName, pipelineName, runId, req.Request)
Z
Zhuxiaoyang 已提交
344 345 346 347
	if err != nil {
		parseErr(err, resp)
		return
	}
348 349
	h.approvableCheck(res, req)

R
runzexia 已提交
350
	resp.WriteAsJson(res)
Z
Zhuxiaoyang 已提交
351 352
}

R
runzexia 已提交
353
func (h *ProjectPipelineHandler) GetBranchPipeline(req *restful.Request, resp *restful.Response) {
354 355 356
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
	branchName := req.PathParameter("branch")
Z
Zhuxiaoyang 已提交
357

R
runzexia 已提交
358
	res, err := h.devopsOperator.GetBranchPipeline(projectName, pipelineName, branchName, req.Request)
Z
Zhuxiaoyang 已提交
359 360 361 362 363
	if err != nil {
		parseErr(err, resp)
		return
	}

Z
Zhuxiaoyang 已提交
364
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
R
runzexia 已提交
365
	resp.WriteAsJson(res)
Z
Zhuxiaoyang 已提交
366 367
}

R
runzexia 已提交
368
func (h *ProjectPipelineHandler) GetBranchPipelineRun(req *restful.Request, resp *restful.Response) {
369 370
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
R
runzexia 已提交
371
	branchName := req.PathParameter("branch")
372
	runId := req.PathParameter("run")
Z
Zhuxiaoyang 已提交
373

R
runzexia 已提交
374
	res, err := h.devopsOperator.GetBranchPipelineRun(projectName, pipelineName, branchName, runId, req.Request)
Z
Zhuxiaoyang 已提交
375 376 377 378 379
	if err != nil {
		parseErr(err, resp)
		return
	}

Z
Zhuxiaoyang 已提交
380
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
R
runzexia 已提交
381
	resp.WriteAsJson(res)
Z
Zhuxiaoyang 已提交
382 383
}

R
runzexia 已提交
384
func (h *ProjectPipelineHandler) StopBranchPipeline(req *restful.Request, resp *restful.Response) {
385 386 387 388
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
	branchName := req.PathParameter("branch")
	runId := req.PathParameter("run")
Z
Zhuxiaoyang 已提交
389

R
runzexia 已提交
390
	res, err := h.devopsOperator.StopBranchPipeline(projectName, pipelineName, branchName, runId, req.Request)
Z
Zhuxiaoyang 已提交
391 392 393 394 395
	if err != nil {
		parseErr(err, resp)
		return
	}

R
runzexia 已提交
396 397
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
	resp.WriteAsJson(res)
Z
Zhuxiaoyang 已提交
398 399
}

R
runzexia 已提交
400
func (h *ProjectPipelineHandler) ReplayBranchPipeline(req *restful.Request, resp *restful.Response) {
401 402
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
R
runzexia 已提交
403
	branchName := req.PathParameter("branch")
404
	runId := req.PathParameter("run")
Z
Zhuxiaoyang 已提交
405

R
runzexia 已提交
406
	res, err := h.devopsOperator.ReplayBranchPipeline(projectName, pipelineName, branchName, runId, req.Request)
Z
Zhuxiaoyang 已提交
407 408 409 410 411
	if err != nil {
		parseErr(err, resp)
		return
	}

R
runzexia 已提交
412 413
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
	resp.WriteAsJson(res)
Z
Zhuxiaoyang 已提交
414 415
}

R
runzexia 已提交
416
func (h *ProjectPipelineHandler) RunBranchPipeline(req *restful.Request, resp *restful.Response) {
417 418 419
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
	branchName := req.PathParameter("branch")
Z
Zhuxiaoyang 已提交
420

R
runzexia 已提交
421
	res, err := h.devopsOperator.RunBranchPipeline(projectName, pipelineName, branchName, req.Request)
Z
Zhuxiaoyang 已提交
422 423 424 425
	if err != nil {
		parseErr(err, resp)
		return
	}
R
runzexia 已提交
426

Z
Zhuxiaoyang 已提交
427
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
R
runzexia 已提交
428
	resp.WriteAsJson(res)
Z
Zhuxiaoyang 已提交
429 430
}

R
runzexia 已提交
431
func (h *ProjectPipelineHandler) GetBranchArtifacts(req *restful.Request, resp *restful.Response) {
432 433
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
R
runzexia 已提交
434
	branchName := req.PathParameter("branch")
435
	runId := req.PathParameter("run")
Z
Zhuxiaoyang 已提交
436

R
runzexia 已提交
437
	res, err := h.devopsOperator.GetBranchArtifacts(projectName, pipelineName, branchName, runId, req.Request)
Z
Zhuxiaoyang 已提交
438 439 440 441
	if err != nil {
		parseErr(err, resp)
		return
	}
Z
Zhuxiaoyang 已提交
442
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
R
runzexia 已提交
443
	resp.WriteAsJson(res)
Z
Zhuxiaoyang 已提交
444 445
}

R
runzexia 已提交
446
func (h *ProjectPipelineHandler) GetBranchRunLog(req *restful.Request, resp *restful.Response) {
447 448
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
R
runzexia 已提交
449 450
	branchName := req.PathParameter("branch")
	runId := req.PathParameter("run")
Z
Zhuxiaoyang 已提交
451

R
runzexia 已提交
452
	res, err := h.devopsOperator.GetBranchRunLog(projectName, pipelineName, branchName, runId, req.Request)
Z
Zhuxiaoyang 已提交
453 454 455 456
	if err != nil {
		parseErr(err, resp)
		return
	}
R
runzexia 已提交
457

Z
Zhuxiaoyang 已提交
458
	resp.Write(res)
Z
Zhuxiaoyang 已提交
459 460
}

R
runzexia 已提交
461
func (h *ProjectPipelineHandler) GetBranchStepLog(req *restful.Request, resp *restful.Response) {
462 463 464 465 466 467
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
	branchName := req.PathParameter("branch")
	runId := req.PathParameter("run")
	nodeId := req.PathParameter("node")
	stepId := req.PathParameter("step")
Z
Zhuxiaoyang 已提交
468

R
runzexia 已提交
469 470
	res, header, err := h.devopsOperator.GetBranchStepLog(projectName, pipelineName, branchName, runId, nodeId, stepId, req.Request)

Z
Zhuxiaoyang 已提交
471 472 473 474
	if err != nil {
		parseErr(err, resp)
		return
	}
R
runzexia 已提交
475 476 477 478 479
	for k, v := range header {
		if strings.HasPrefix(k, jenkinsHeaderPre) {
			resp.AddHeader(k, v[0])
		}
	}
Z
Zhuxiaoyang 已提交
480 481 482
	resp.Write(res)
}

R
runzexia 已提交
483
func (h *ProjectPipelineHandler) GetBranchNodeSteps(req *restful.Request, resp *restful.Response) {
484 485
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
R
runzexia 已提交
486
	branchName := req.PathParameter("branch")
487 488
	runId := req.PathParameter("run")
	nodeId := req.PathParameter("node")
Z
Zhuxiaoyang 已提交
489

R
runzexia 已提交
490
	res, err := h.devopsOperator.GetBranchNodeSteps(projectName, pipelineName, branchName, runId, nodeId, req.Request)
Z
Zhuxiaoyang 已提交
491 492 493 494
	if err != nil {
		parseErr(err, resp)
		return
	}
R
runzexia 已提交
495 496
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
	resp.WriteAsJson(res)
Z
Zhuxiaoyang 已提交
497 498
}

R
runzexia 已提交
499
func (h *ProjectPipelineHandler) GetBranchPipelineRunNodes(req *restful.Request, resp *restful.Response) {
500 501
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
R
runzexia 已提交
502 503
	branchName := req.PathParameter("branch")
	runId := req.PathParameter("run")
Z
Zhuxiaoyang 已提交
504

R
runzexia 已提交
505
	res, err := h.devopsOperator.GetBranchPipelineRunNodes(projectName, pipelineName, branchName, runId, req.Request)
Z
Zhuxiaoyang 已提交
506 507 508 509 510
	if err != nil {
		parseErr(err, resp)
		return
	}

R
runzexia 已提交
511 512
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
	resp.WriteAsJson(res)
Z
Zhuxiaoyang 已提交
513 514
}

R
runzexia 已提交
515
func (h *ProjectPipelineHandler) SubmitBranchInputStep(req *restful.Request, resp *restful.Response) {
516 517
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
R
runzexia 已提交
518 519 520 521
	branchName := req.PathParameter("branch")
	runId := req.PathParameter("run")
	nodeId := req.PathParameter("node")
	stepId := req.PathParameter("step")
Z
Zhuxiaoyang 已提交
522

523 524 525 526 527
	var (
		response []byte
		err      error
		ok       bool
	)
528

529 530 531 532
	if ok, err = h.hasSubmitPermission(req); !ok || err != nil {
		msg := map[string]string{
			"allow":   "false",
			"message": fmt.Sprintf("%v", err),
533 534
		}

535 536 537 538 539 540 541
		response, _ = json.Marshal(msg)
	} else {
		response, err = h.devopsOperator.SubmitBranchInputStep(projectName, pipelineName, branchName, runId, nodeId, stepId, req.Request)
		if err != nil {
			parseErr(err, resp)
			return
		}
Z
Zhuxiaoyang 已提交
542 543
	}

544
	resp.Write(response)
Z
Zhuxiaoyang 已提交
545 546
}

R
runzexia 已提交
547
func (h *ProjectPipelineHandler) GetBranchNodesDetail(req *restful.Request, resp *restful.Response) {
548 549 550
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
	branchName := req.PathParameter("branch")
R
runzexia 已提交
551
	runId := req.PathParameter("run")
Z
Zhuxiaoyang 已提交
552

R
runzexia 已提交
553
	res, err := h.devopsOperator.GetBranchNodesDetail(projectName, pipelineName, branchName, runId, req.Request)
Z
Zhuxiaoyang 已提交
554 555 556 557
	if err != nil {
		parseErr(err, resp)
		return
	}
558
	h.approvableCheck(res, req)
R
runzexia 已提交
559
	resp.WriteAsJson(res)
Z
Zhuxiaoyang 已提交
560 561
}

R
runzexia 已提交
562
func (h *ProjectPipelineHandler) GetPipelineBranch(req *restful.Request, resp *restful.Response) {
563 564
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")
Z
Zhuxiaoyang 已提交
565

R
runzexia 已提交
566
	res, err := h.devopsOperator.GetPipelineBranch(projectName, pipelineName, req.Request)
Z
Zhuxiaoyang 已提交
567 568 569 570
	if err != nil {
		parseErr(err, resp)
		return
	}
Z
Zhuxiaoyang 已提交
571
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
R
runzexia 已提交
572
	resp.WriteAsJson(res)
Z
Zhuxiaoyang 已提交
573 574
}

R
runzexia 已提交
575 576 577 578 579
func (h *ProjectPipelineHandler) ScanBranch(req *restful.Request, resp *restful.Response) {
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")

	res, err := h.devopsOperator.ScanBranch(projectName, pipelineName, req.Request)
S
soulseen 已提交
580 581 582 583 584
	if err != nil {
		parseErr(err, resp)
		return
	}

Z
Zhuxiaoyang 已提交
585 586 587
	resp.Write(res)
}

R
runzexia 已提交
588
func (h *ProjectPipelineHandler) GetConsoleLog(req *restful.Request, resp *restful.Response) {
589 590 591
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")

R
runzexia 已提交
592
	res, err := h.devopsOperator.GetConsoleLog(projectName, pipelineName, req.Request)
Z
Zhuxiaoyang 已提交
593 594 595 596 597
	if err != nil {
		parseErr(err, resp)
		return
	}

R
runzexia 已提交
598
	resp.Write(res)
Z
Zhuxiaoyang 已提交
599 600
}

R
runzexia 已提交
601 602
func (h *ProjectPipelineHandler) GetCrumb(req *restful.Request, resp *restful.Response) {
	res, err := h.devopsOperator.GetCrumb(req.Request)
Z
Zhuxiaoyang 已提交
603 604 605 606 607
	if err != nil {
		parseErr(err, resp)
		return
	}

Z
Zhuxiaoyang 已提交
608
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
Z
Zhuxiaoyang 已提交
609 610 611
	resp.WriteAsJson(res)
}

R
runzexia 已提交
612 613
func (h *ProjectPipelineHandler) GetSCMServers(req *restful.Request, resp *restful.Response) {
	scmId := req.PathParameter("scm")
Z
Zhuxiaoyang 已提交
614

R
runzexia 已提交
615
	res, err := h.devopsOperator.GetSCMServers(scmId, req.Request)
Z
Zhuxiaoyang 已提交
616 617 618 619 620
	if err != nil {
		parseErr(err, resp)
		return
	}

Z
Zhuxiaoyang 已提交
621
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
R
runzexia 已提交
622
	resp.WriteAsJson(res)
Z
Zhuxiaoyang 已提交
623 624
}

R
runzexia 已提交
625 626
func (h *ProjectPipelineHandler) GetSCMOrg(req *restful.Request, resp *restful.Response) {
	scmId := req.PathParameter("scm")
Z
Zhuxiaoyang 已提交
627

R
runzexia 已提交
628
	res, err := h.devopsOperator.GetSCMOrg(scmId, req.Request)
Z
Zhuxiaoyang 已提交
629 630 631 632 633
	if err != nil {
		parseErr(err, resp)
		return
	}

Z
Zhuxiaoyang 已提交
634
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
R
runzexia 已提交
635
	resp.WriteAsJson(res)
Z
Zhuxiaoyang 已提交
636 637
}

R
runzexia 已提交
638 639 640
func (h *ProjectPipelineHandler) GetOrgRepo(req *restful.Request, resp *restful.Response) {
	scmId := req.PathParameter("scm")
	organizationId := req.PathParameter("organization")
Z
Zhuxiaoyang 已提交
641

R
runzexia 已提交
642
	res, err := h.devopsOperator.GetOrgRepo(scmId, organizationId, req.Request)
Z
Zhuxiaoyang 已提交
643 644 645 646
	if err != nil {
		parseErr(err, resp)
		return
	}
R
runzexia 已提交
647

Z
Zhuxiaoyang 已提交
648
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
R
runzexia 已提交
649
	resp.WriteAsJson(res)
Z
Zhuxiaoyang 已提交
650 651
}

R
runzexia 已提交
652 653
func (h *ProjectPipelineHandler) CreateSCMServers(req *restful.Request, resp *restful.Response) {
	scmId := req.PathParameter("scm")
Z
Zhuxiaoyang 已提交
654

R
runzexia 已提交
655
	res, err := h.devopsOperator.CreateSCMServers(scmId, req.Request)
Z
Zhuxiaoyang 已提交
656 657 658 659
	if err != nil {
		parseErr(err, resp)
		return
	}
R
runzexia 已提交
660

Z
Zhuxiaoyang 已提交
661
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
R
runzexia 已提交
662
	resp.WriteAsJson(res)
Z
Zhuxiaoyang 已提交
663 664
}

R
runzexia 已提交
665 666
func (h *ProjectPipelineHandler) Validate(req *restful.Request, resp *restful.Response) {
	scmId := req.PathParameter("scm")
Z
Zhuxiaoyang 已提交
667

R
runzexia 已提交
668
	res, err := h.devopsOperator.Validate(scmId, req.Request)
Z
Zhuxiaoyang 已提交
669
	if err != nil {
R
runzexia 已提交
670 671 672 673 674 675 676 677 678 679
		log.Error(err)
		if jErr, ok := err.(*devops.JkError); ok {
			if jErr.Code != http.StatusUnauthorized {
				resp.WriteError(jErr.Code, err)
			} else {
				resp.WriteHeader(http.StatusPreconditionRequired)
			}
		} else {
			resp.WriteError(http.StatusInternalServerError, err)
		}
Z
Zhuxiaoyang 已提交
680 681
		return
	}
R
runzexia 已提交
682

Z
Zhuxiaoyang 已提交
683
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
R
runzexia 已提交
684
	resp.WriteAsJson(res)
Z
Zhuxiaoyang 已提交
685 686
}

R
runzexia 已提交
687 688
func (h *ProjectPipelineHandler) GetNotifyCommit(req *restful.Request, resp *restful.Response) {
	res, err := h.devopsOperator.GetNotifyCommit(req.Request)
Z
Zhuxiaoyang 已提交
689 690 691 692 693 694 695
	if err != nil {
		parseErr(err, resp)
		return
	}
	resp.Write(res)
}

R
runzexia 已提交
696 697
func (h *ProjectPipelineHandler) PostNotifyCommit(req *restful.Request, resp *restful.Response) {
	res, err := h.devopsOperator.GetNotifyCommit(req.Request)
Z
Zhuxiaoyang 已提交
698 699 700 701
	if err != nil {
		parseErr(err, resp)
		return
	}
Z
Zhuxiaoyang 已提交
702 703 704
	resp.Write(res)
}

R
runzexia 已提交
705 706
func (h *ProjectPipelineHandler) GithubWebhook(req *restful.Request, resp *restful.Response) {
	res, err := h.devopsOperator.GithubWebhook(req.Request)
Z
Zhuxiaoyang 已提交
707 708 709 710 711 712 713
	if err != nil {
		parseErr(err, resp)
		return
	}
	resp.Write(res)
}

R
runzexia 已提交
714 715 716 717 718
func (h *ProjectPipelineHandler) CheckScriptCompile(req *restful.Request, resp *restful.Response) {
	projectName := req.PathParameter("devops")
	pipelineName := req.PathParameter("pipeline")

	resBody, err := h.devopsOperator.CheckScriptCompile(projectName, pipelineName, req.Request)
R
runzexia 已提交
719 720 721 722
	if err != nil {
		parseErr(err, resp)
		return
	}
R
runzexia 已提交
723 724

	resp.WriteAsJson(resBody)
R
runzexia 已提交
725
}
R
runzexia 已提交
726 727 728 729 730

func (h *ProjectPipelineHandler) CheckCron(req *restful.Request, resp *restful.Response) {
	projectName := req.PathParameter("devops")

	res, err := h.devopsOperator.CheckCron(projectName, req.Request)
Z
Zhuxiaoyang 已提交
731 732 733 734
	if err != nil {
		parseErr(err, resp)
		return
	}
S
sunzhu 已提交
735

R
runzexia 已提交
736 737 738
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
	resp.WriteAsJson(res)
}
Z
Zhuxiaoyang 已提交
739

R
runzexia 已提交
740 741
func (h *ProjectPipelineHandler) ToJenkinsfile(req *restful.Request, resp *restful.Response) {
	res, err := h.devopsOperator.ToJenkinsfile(req.Request)
Z
Zhuxiaoyang 已提交
742 743 744 745
	if err != nil {
		parseErr(err, resp)
		return
	}
R
runzexia 已提交
746
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
Z
Zhuxiaoyang 已提交
747 748 749
	resp.WriteAsJson(res)
}

R
runzexia 已提交
750 751
func (h *ProjectPipelineHandler) ToJson(req *restful.Request, resp *restful.Response) {
	res, err := h.devopsOperator.ToJson(req.Request)
Z
Zhuxiaoyang 已提交
752 753 754 755
	if err != nil {
		parseErr(err, resp)
		return
	}
R
runzexia 已提交
756
	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
Z
Zhuxiaoyang 已提交
757 758 759
	resp.WriteAsJson(res)
}

R
runzexia 已提交
760 761 762 763 764 765 766 767 768 769 770 771 772 773
func (h *ProjectPipelineHandler) GetProjectCredentialUsage(req *restful.Request, resp *restful.Response) {
	projectId := req.PathParameter("devops")
	credentialId := req.PathParameter("credential")
	response, err := h.projectCredentialGetter.GetProjectCredentialUsage(projectId, credentialId)
	if err != nil {
		log.Errorf("%+v", err)
		api.HandleInternalError(resp, nil, err)
		return
	}
	resp.WriteAsJson(response)
	return

}

S
sunzhu 已提交
774
func parseErr(err error, resp *restful.Response) {
S
soulseen 已提交
775
	log.Error(err)
S
sunzhu 已提交
776
	if jErr, ok := err.(*devops.JkError); ok {
Z
Zhuxiaoyang 已提交
777
		resp.WriteError(jErr.Code, err)
S
sunzhu 已提交
778
	} else {
Z
Zhuxiaoyang 已提交
779
		resp.WriteError(http.StatusInternalServerError, err)
S
sunzhu 已提交
780 781 782
	}
	return
}