devops.go 25.9 KB
Newer Older
S
sunzhu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*

  Copyright 2019 The KubeSphere Authors.

  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

      http://www.apache.org/licenses/LICENSE-2.0

  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 已提交
17
*/
S
sunzhu 已提交
18 19 20
package devops

import (
S
soulseen 已提交
21
	"bytes"
S
sunzhu 已提交
22
	"compress/gzip"
Z
Zhuxiaoyang 已提交
23
	"encoding/json"
S
sunzhu 已提交
24
	"fmt"
Z
Zhuxiaoyang 已提交
25
	"github.com/PuerkitoBio/goquery"
26
	"github.com/emicklei/go-restful"
S
sunzhu 已提交
27 28
	"io"
	"io/ioutil"
29 30
	log "k8s.io/klog"
	cs "kubesphere.io/kubesphere/pkg/simple/client"
S
sunzhu 已提交
31 32
	"net/http"
	"net/url"
33
	"strings"
Z
Zhuxiaoyang 已提交
34
	"sync"
S
sunzhu 已提交
35 36 37
	"time"
)

38 39 40 41
const (
	channelMaxCapacity = 100
	cronJobLayout      = "Monday, January 2, 2006 15:04:05 PM"
)
Z
Zhuxiaoyang 已提交
42

Z
Zhuxiaoyang 已提交
43
func GetPipeline(projectName, pipelineName string, req *http.Request) ([]byte, error) {
44 45 46 47 48 49
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+GetPipelineUrl, projectName, pipelineName)
S
sunzhu 已提交
50

Z
Zhuxiaoyang 已提交
51
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
52
	if err != nil {
S
soulseen 已提交
53
		log.Error(err)
S
sunzhu 已提交
54 55 56
		return nil, err
	}

Z
Zhuxiaoyang 已提交
57 58 59
	return res, err
}

Z
Zhuxiaoyang 已提交
60
func SearchPipelines(req *http.Request) ([]byte, error) {
61 62 63 64 65 66
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := devops.Jenkins().Server + SearchPipelineUrl + req.URL.RawQuery
Z
Zhuxiaoyang 已提交
67

Z
Zhuxiaoyang 已提交
68
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
69 70 71 72 73 74 75 76
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
77
func SearchPipelineRuns(projectName, pipelineName string, req *http.Request) ([]byte, error) {
78 79 80 81
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}
S
soulseen 已提交
82

83
	baseUrl := fmt.Sprintf(devops.Jenkins().Server+SearchPipelineRunUrl, projectName, pipelineName)
Z
Zhuxiaoyang 已提交
84

S
soulseen 已提交
85
	res, err := sendJenkinsRequest(baseUrl+req.URL.RawQuery, req)
Z
Zhuxiaoyang 已提交
86 87 88 89 90 91 92 93
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
94
func GetBranchPipelineRun(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) {
95 96 97 98 99 100
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+GetPipeBranchRunUrl, projectName, pipelineName, branchName, runId)
S
sunzhu 已提交
101

Z
Zhuxiaoyang 已提交
102
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
103
	if err != nil {
S
soulseen 已提交
104
		log.Error(err)
S
sunzhu 已提交
105 106 107
		return nil, err
	}

Z
Zhuxiaoyang 已提交
108 109 110
	return res, err
}

Z
Zhuxiaoyang 已提交
111
func GetPipelineRunNodesbyBranch(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) {
112 113 114 115 116 117
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+GetBranchPipeRunNodesUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId)
S
soulseen 已提交
118
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
119

Z
Zhuxiaoyang 已提交
120
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
121 122 123 124 125 126 127 128
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

S
soulseen 已提交
129
func GetBranchStepLog(projectName, pipelineName, branchName, runId, nodeId, stepId string, req *http.Request) ([]byte, http.Header, error) {
130 131 132 133 134 135
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+GetBranchStepLogUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId, nodeId, stepId)
Z
Zhuxiaoyang 已提交
136

S
soulseen 已提交
137
	resBody, header, err := jenkinsClient(baseUrl, req)
Z
Zhuxiaoyang 已提交
138 139
	if err != nil {
		log.Error(err)
S
soulseen 已提交
140
		return nil, nil, err
Z
Zhuxiaoyang 已提交
141 142
	}

S
soulseen 已提交
143
	return resBody, header, err
Z
Zhuxiaoyang 已提交
144 145
}

S
soulseen 已提交
146
func GetStepLog(projectName, pipelineName, runId, nodeId, stepId string, req *http.Request) ([]byte, http.Header, error) {
147 148 149 150 151 152
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+GetStepLogUrl+req.URL.RawQuery, projectName, pipelineName, runId, nodeId, stepId)
Z
Zhuxiaoyang 已提交
153

S
soulseen 已提交
154
	resBody, header, err := jenkinsClient(baseUrl, req)
Z
Zhuxiaoyang 已提交
155 156
	if err != nil {
		log.Error(err)
S
soulseen 已提交
157
		return nil, nil, err
Z
Zhuxiaoyang 已提交
158 159
	}

S
soulseen 已提交
160
	return resBody, header, err
Z
Zhuxiaoyang 已提交
161 162
}

163
func GetSCMServers(scmId string, req *http.Request) ([]byte, error) {
164 165 166 167 168 169
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+GetSCMServersUrl, scmId)
170 171 172 173 174 175 176 177 178 179
	req.Method = http.MethodGet
	resBody, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	return resBody, err
}

func CreateSCMServers(scmId string, req *http.Request) ([]byte, error) {
180 181 182 183 184
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	requestBody, err := ioutil.ReadAll(req.Body)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	createReq := &CreateScmServerReq{}
	err = json.Unmarshal(requestBody, createReq)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	req.Body = nil
	byteServers, err := GetSCMServers(scmId, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}
202

203 204 205 206 207 208 209 210
	var servers []*SCMServer
	_ = json.Unmarshal(byteServers, &servers)
	for _, server := range servers {
		if server.ApiURL == createReq.ApiURL {
			return json.Marshal(server)
		}
	}
	req.Body = ioutil.NopCloser(bytes.NewReader(requestBody))
211 212 213

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+CreateSCMServersUrl, scmId)

214 215 216 217 218 219 220 221 222
	req.Method = http.MethodPost
	resBody, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	return resBody, err
}

Z
Zhuxiaoyang 已提交
223
func Validate(scmId string, req *http.Request) ([]byte, error) {
224 225 226 227 228 229
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+ValidateUrl, scmId)
S
sunzhu 已提交
230

231
	req.Method = http.MethodPut
Z
Zhuxiaoyang 已提交
232
	resBody, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
233
	if err != nil {
S
soulseen 已提交
234
		log.Error(err)
S
sunzhu 已提交
235 236 237
		return nil, err
	}

Z
Zhuxiaoyang 已提交
238 239 240
	return resBody, err
}

Z
Zhuxiaoyang 已提交
241
func GetSCMOrg(scmId string, req *http.Request) ([]byte, error) {
242 243 244 245 246 247
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+GetSCMOrgUrl+req.URL.RawQuery, scmId)
Z
Zhuxiaoyang 已提交
248

Z
Zhuxiaoyang 已提交
249
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
250 251 252 253 254 255 256 257
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
258
func GetOrgRepo(scmId, organizationId string, req *http.Request) ([]byte, error) {
259 260 261 262 263 264
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+GetOrgRepoUrl+req.URL.RawQuery, scmId, organizationId)
S
sunzhu 已提交
265

Z
Zhuxiaoyang 已提交
266
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
267
	if err != nil {
S
soulseen 已提交
268
		log.Error(err)
S
sunzhu 已提交
269 270 271
		return nil, err
	}

Z
Zhuxiaoyang 已提交
272 273 274
	return res, err
}

Z
Zhuxiaoyang 已提交
275
func StopBranchPipeline(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) {
276 277 278 279 280 281
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+StopBranchPipelineUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId)
Z
Zhuxiaoyang 已提交
282

283
	req.Method = http.MethodPut
Z
Zhuxiaoyang 已提交
284 285 286 287 288 289 290 291 292 293
	res, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

func StopPipeline(projectName, pipelineName, runId string, req *http.Request) ([]byte, error) {
294 295 296 297 298 299
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+StopPipelineUrl+req.URL.RawQuery, projectName, pipelineName, runId)
Z
Zhuxiaoyang 已提交
300

301
	req.Method = http.MethodPut
Z
Zhuxiaoyang 已提交
302 303 304 305 306 307 308 309 310 311
	res, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

func ReplayBranchPipeline(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) {
312 313 314 315 316 317
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+ReplayBranchPipelineUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId)
Z
Zhuxiaoyang 已提交
318 319 320 321 322 323 324 325 326 327 328

	res, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

func ReplayPipeline(projectName, pipelineName, runId string, req *http.Request) ([]byte, error) {
329 330 331 332 333 334
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+ReplayPipelineUrl+req.URL.RawQuery, projectName, pipelineName, runId)
Z
Zhuxiaoyang 已提交
335

Z
Zhuxiaoyang 已提交
336
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
337 338 339 340 341 342 343 344
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
345
func GetBranchRunLog(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) {
346 347 348 349 350 351
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+GetBranchRunLogUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId)
S
sunzhu 已提交
352

Z
Zhuxiaoyang 已提交
353
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
354
	if err != nil {
S
soulseen 已提交
355
		log.Error(err)
S
sunzhu 已提交
356 357 358
		return nil, err
	}

Z
Zhuxiaoyang 已提交
359 360 361
	return res, err
}

Z
Zhuxiaoyang 已提交
362
func GetRunLog(projectName, pipelineName, runId string, req *http.Request) ([]byte, error) {
363 364 365 366 367 368
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+GetRunLogUrl+req.URL.RawQuery, projectName, pipelineName, runId)
Z
Zhuxiaoyang 已提交
369

Z
Zhuxiaoyang 已提交
370
	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
371 372 373 374 375
	if err != nil {
		log.Error(err)
		return nil, err
	}

Z
Zhuxiaoyang 已提交
376
	return res, err
Z
Zhuxiaoyang 已提交
377 378
}

Z
Zhuxiaoyang 已提交
379
func GetBranchArtifacts(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) {
380 381 382 383 384 385
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+GetBranchArtifactsUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId)
Z
Zhuxiaoyang 已提交
386 387 388 389 390 391 392 393 394 395 396

	res, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

func GetArtifacts(projectName, pipelineName, runId string, req *http.Request) ([]byte, error) {
397 398 399 400 401 402
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+GetArtifactsUrl+req.URL.RawQuery, projectName, pipelineName, runId)
Z
Zhuxiaoyang 已提交
403

Z
Zhuxiaoyang 已提交
404
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
405 406 407 408 409 410 411 412
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
413
func GetPipeBranch(projectName, pipelineName string, req *http.Request) ([]byte, error) {
414 415 416 417 418 419
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+GetPipeBranchUrl, projectName, pipelineName)
Z
Zhuxiaoyang 已提交
420

S
soulseen 已提交
421
	res, err := sendJenkinsRequest(baseUrl+req.URL.RawQuery, req)
Z
Zhuxiaoyang 已提交
422 423 424 425 426 427 428 429
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

S
soulseen 已提交
430
func SubmitBranchInputStep(projectName, pipelineName, branchName, runId, nodeId, stepId string, req *http.Request) ([]byte, error) {
431 432 433 434 435 436
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+CheckBranchPipelineUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId, nodeId, stepId)
Z
Zhuxiaoyang 已提交
437

S
soulseen 已提交
438 439 440 441 442 443
	newBody, err := getInputReqBody(req.Body)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	req.Body = newBody
Z
Zhuxiaoyang 已提交
444 445 446 447 448 449 450 451 452
	resBody, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return resBody, err
}

S
soulseen 已提交
453
func SubmitInputStep(projectName, pipelineName, runId, nodeId, stepId string, req *http.Request) ([]byte, error) {
454 455 456 457 458 459
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+CheckPipelineUrl+req.URL.RawQuery, projectName, pipelineName, runId, nodeId, stepId)
S
sunzhu 已提交
460

S
soulseen 已提交
461 462 463 464 465 466
	newBody, err := getInputReqBody(req.Body)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	req.Body = newBody
Z
Zhuxiaoyang 已提交
467
	resBody, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
468
	if err != nil {
S
soulseen 已提交
469 470 471 472 473 474 475
		log.Error(err)
		return nil, err
	}

	return resBody, err
}

S
soulseen 已提交
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
func getInputReqBody(reqBody io.ReadCloser) (newReqBody io.ReadCloser, err error) {
	var checkBody CheckPlayload
	var jsonBody []byte
	var workRound struct {
		ID         string                    `json:"id,omitempty" description:"id"`
		Parameters []CheckPlayloadParameters `json:"parameters"`
		Abort      bool                      `json:"abort,omitempty" description:"abort or not"`
	}

	Body, err := ioutil.ReadAll(reqBody)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	err = json.Unmarshal(Body, &checkBody)

	if checkBody.Abort != true && checkBody.Parameters == nil {
		workRound.Parameters = []CheckPlayloadParameters{}
		workRound.ID = checkBody.ID
		jsonBody, _ = json.Marshal(workRound)
	} else {
		jsonBody, _ = json.Marshal(checkBody)
	}

	newReqBody = parseBody(bytes.NewBuffer(jsonBody))

	return newReqBody, nil

}

func parseBody(body io.Reader) (newReqBody io.ReadCloser) {
	rc, ok := body.(io.ReadCloser)
	if !ok && body != nil {
		rc = ioutil.NopCloser(body)
	}
	return rc
}

Z
Zhuxiaoyang 已提交
515
func GetConsoleLog(projectName, pipelineName string, req *http.Request) ([]byte, error) {
516 517 518 519 520 521
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+GetConsoleLogUrl+req.URL.RawQuery, projectName, pipelineName)
S
soulseen 已提交
522

Z
Zhuxiaoyang 已提交
523
	resBody, err := sendJenkinsRequest(baseUrl, req)
S
soulseen 已提交
524 525
	if err != nil {
		log.Error(err)
S
sunzhu 已提交
526 527 528 529 530 531
		return nil, err
	}

	return resBody, err
}

Z
Zhuxiaoyang 已提交
532
func ScanBranch(projectName, pipelineName string, req *http.Request) ([]byte, error) {
533 534 535 536 537 538
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+ScanBranchUrl+req.URL.RawQuery, projectName, pipelineName)
S
soulseen 已提交
539

Z
Zhuxiaoyang 已提交
540
	resBody, err := sendJenkinsRequest(baseUrl, req)
S
soulseen 已提交
541 542 543 544 545
	if err != nil {
		log.Error(err)
		return nil, err
	}

Z
Zhuxiaoyang 已提交
546 547 548
	return resBody, err
}

Z
Zhuxiaoyang 已提交
549
func RunBranchPipeline(projectName, pipelineName, branchName string, req *http.Request) ([]byte, error) {
550 551 552 553 554 555
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+RunBranchPipelineUrl+req.URL.RawQuery, projectName, pipelineName, branchName)
Z
Zhuxiaoyang 已提交
556 557 558 559 560 561 562 563 564 565 566

	res, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

func RunPipeline(projectName, pipelineName string, req *http.Request) ([]byte, error) {
567 568 569 570 571
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}
	baseUrl := fmt.Sprintf(devops.Jenkins().Server+RunPipelineUrl+req.URL.RawQuery, projectName, pipelineName)
Z
Zhuxiaoyang 已提交
572

Z
Zhuxiaoyang 已提交
573
	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
574 575 576 577 578 579 580 581
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
582
func GetCrumb(req *http.Request) ([]byte, error) {
583 584 585 586 587 588
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server + GetCrumbUrl)
Z
Zhuxiaoyang 已提交
589

Z
Zhuxiaoyang 已提交
590
	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
591 592 593 594 595 596 597 598
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

599
func CheckScriptCompile(projectName, pipelineName string, req *http.Request) ([]byte, error) {
600 601 602 603 604 605
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+CheckScriptCompileUrl, projectName, pipelineName)
Z
Zhuxiaoyang 已提交
606 607 608 609 610 611 612 613 614 615

	resBody, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return resBody, err
}

616
func CheckCron(projectName string, req *http.Request) (*CheckCronRes, error) {
617 618 619 620 621 622 623
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	jenkins := devops.Jenkins()

624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
	var res = new(CheckCronRes)
	var cron = new(CronData)
	var reader io.ReadCloser
	var baseUrl string

	reader = req.Body
	cronData, err := ioutil.ReadAll(reader)
	json.Unmarshal(cronData, cron)

	if cron.PipelineName != "" {
		baseUrl = fmt.Sprintf(jenkins.Server+CheckPipelienCronUrl, projectName, cron.PipelineName, cron.Cron)
	} else {
		baseUrl = fmt.Sprintf(jenkins.Server+CheckCronUrl, projectName, cron.Cron)
	}

639
	newUrl, err := url.Parse(baseUrl)
640 641 642 643
	if err != nil {
		log.Error(err)
		return nil, err
	}
644
	newUrl.RawQuery = newUrl.Query().Encode()
S
soulseen 已提交
645 646 647

	reqJenkins := &http.Request{
		Method: http.MethodGet,
648
		URL:    newUrl,
649
		Header: req.Header,
S
soulseen 已提交
650 651
	}

652
	client := &http.Client{Timeout: 30 * time.Second}
Z
Zhuxiaoyang 已提交
653

S
soulseen 已提交
654
	resp, err := client.Do(reqJenkins)
655 656

	if resp != nil && resp.StatusCode != http.StatusOK {
657 658 659 660 661 662
		resBody, _ := getRespBody(resp)
		return &CheckCronRes{
			Result:  "error",
			Message: string(resBody),
		}, err
	}
Z
Zhuxiaoyang 已提交
663 664
	if err != nil {
		log.Error(err)
665
		return nil, err
Z
Zhuxiaoyang 已提交
666 667 668 669 670 671
	}
	defer resp.Body.Close()

	doc, err := goquery.NewDocumentFromReader(resp.Body)
	if err != nil {
		log.Error(err)
672
		return nil, err
Z
Zhuxiaoyang 已提交
673 674 675 676 677
	}
	doc.Find("div").Each(func(i int, selection *goquery.Selection) {
		res.Message = selection.Text()
		res.Result, _ = selection.Attr("class")
	})
678 679 680 681 682 683 684 685
	if res.Result == "ok" {
		res.LastTime, res.NextTime, err = parseCronJobTime(res.Message)
		if err != nil {
			log.Error(err)
			return nil, err
		}
	}

Z
Zhuxiaoyang 已提交
686 687 688
	return res, err
}

689
func parseCronJobTime(msg string) (string, string, error) {
690

691 692 693 694 695 696 697
	times := strings.Split(msg, ";")

	lastTmp := strings.SplitN(times[0], " ", 2)
	lastTime := strings.SplitN(lastTmp[1], " UTC", 2)
	lastUinx, err := time.Parse(cronJobLayout, lastTime[0])
	if err != nil {
		log.Error(err)
H
huanggze 已提交
698
		return "", "", err
699 700 701 702 703 704 705 706
	}
	last := lastUinx.Format(time.RFC3339)

	nextTmp := strings.SplitN(times[1], " ", 3)
	nextTime := strings.SplitN(nextTmp[2], " UTC", 2)
	nextUinx, err := time.Parse(cronJobLayout, nextTime[0])
	if err != nil {
		log.Error(err)
H
huanggze 已提交
707
		return "", "", err
708 709 710 711 712 713
	}
	next := nextUinx.Format(time.RFC3339)

	return last, next, nil
}

Z
Zhuxiaoyang 已提交
714
func GetPipelineRun(projectName, pipelineName, runId string, req *http.Request) ([]byte, error) {
715 716 717 718 719 720
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+GetPipelineRunUrl, projectName, pipelineName, runId)
Z
Zhuxiaoyang 已提交
721 722

	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
723 724
	if err != nil {
		log.Error(err)
Z
Zhuxiaoyang 已提交
725
		return nil, err
Z
Zhuxiaoyang 已提交
726 727
	}

Z
Zhuxiaoyang 已提交
728 729 730
	return res, err
}

Z
Zhuxiaoyang 已提交
731
func GetBranchPipeline(projectName, pipelineName, branchName string, req *http.Request) ([]byte, error) {
732 733 734 735 736 737
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+GetBranchPipeUrl, projectName, pipelineName, branchName)
Z
Zhuxiaoyang 已提交
738 739

	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
740 741
	if err != nil {
		log.Error(err)
Z
Zhuxiaoyang 已提交
742
		return nil, err
Z
Zhuxiaoyang 已提交
743 744
	}

Z
Zhuxiaoyang 已提交
745 746 747
	return res, err
}

Z
Zhuxiaoyang 已提交
748
func GetPipelineRunNodes(projectName, pipelineName, runId string, req *http.Request) ([]byte, error) {
749 750 751 752 753 754
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+GetPipeRunNodesUrl+req.URL.RawQuery, projectName, pipelineName, runId)
Z
Zhuxiaoyang 已提交
755 756 757 758 759 760 761 762 763 764

	res, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
765
func GetBranchNodeSteps(projectName, pipelineName, branchName, runId, nodeId string, req *http.Request) ([]byte, error) {
766 767 768 769 770 771
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+GetBranchNodeStepsUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId, nodeId)
Z
Zhuxiaoyang 已提交
772 773 774 775 776 777 778 779 780 781 782

	res, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

func GetNodeSteps(projectName, pipelineName, runId, nodeId string, req *http.Request) ([]byte, error) {
783 784 785 786 787 788
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server+GetNodeStepsUrl+req.URL.RawQuery, projectName, pipelineName, runId, nodeId)
Z
Zhuxiaoyang 已提交
789 790 791 792 793 794 795 796 797 798 799

	res, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

func ToJenkinsfile(req *http.Request) ([]byte, error) {
800 801 802 803 804 805
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server + ToJenkinsfileUrl)
Z
Zhuxiaoyang 已提交
806 807 808 809 810 811 812 813 814 815 816

	res, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

func ToJson(req *http.Request) ([]byte, error) {
817 818 819 820 821 822
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprintf(devops.Jenkins().Server + ToJsonUrl)
Z
Zhuxiaoyang 已提交
823 824 825 826 827 828 829 830 831 832 833

	res, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

func GetNotifyCommit(req *http.Request) ([]byte, error) {
834 835 836 837 838 839
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprint(devops.Jenkins().Server, GetNotifyCommitUrl, req.URL.RawQuery)
Z
Zhuxiaoyang 已提交
840 841 842 843 844 845 846 847 848 849 850 851
	req.Method = "GET"

	res, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

func GithubWebhook(req *http.Request) ([]byte, error) {
852 853 854 855 856 857
	devops, err := cs.ClientSets().Devops()
	if err != nil {
		return nil, restful.NewError(http.StatusServiceUnavailable, err.Error())
	}

	baseUrl := fmt.Sprint(devops.Jenkins().Server, GithubWebhookUrl, req.URL.RawQuery)
Z
Zhuxiaoyang 已提交
858 859 860 861 862 863 864 865

	res, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
Z
Zhuxiaoyang 已提交
866 867
}

Z
Zhuxiaoyang 已提交
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
func GetBranchNodesDetail(projectName, pipelineName, branchName, runId string, req *http.Request) ([]NodesDetail, error) {
	var wg sync.WaitGroup
	var nodesDetails []NodesDetail
	stepChan := make(chan *NodesStepsIndex, channelMaxCapacity)

	respNodes, err := GetPipelineRunNodesbyBranch(projectName, pipelineName, branchName, runId, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	err = json.Unmarshal(respNodes, &nodesDetails)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	// get all steps in nodes.
	for i, v := range nodesDetails {
		wg.Add(1)
		go func(nodeId string, index int) {
			var steps []NodeSteps
			respSteps, err := GetBranchNodeSteps(projectName, pipelineName, branchName, runId, nodeId, req)
			if err != nil {
				log.Error(err)
				return
			}
			err = json.Unmarshal(respSteps, &steps)

			stepChan <- &NodesStepsIndex{index, steps}
			wg.Done()
		}(v.ID, i)
	}

	wg.Wait()
	close(stepChan)

	for oneNodeSteps := range stepChan {
		if oneNodeSteps != nil {
			nodesDetails[oneNodeSteps.Id].Steps = append(nodesDetails[oneNodeSteps.Id].Steps, oneNodeSteps.Steps...)
		}
	}

	return nodesDetails, err
}

func GetNodesDetail(projectName, pipelineName, runId string, req *http.Request) ([]NodesDetail, error) {
	var wg sync.WaitGroup
	var nodesDetails []NodesDetail
	stepChan := make(chan *NodesStepsIndex, channelMaxCapacity)

	respNodes, err := GetPipelineRunNodes(projectName, pipelineName, runId, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	err = json.Unmarshal(respNodes, &nodesDetails)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	// get all steps in nodes.
	for i, v := range nodesDetails {
		wg.Add(1)
		go func(nodeId string, index int) {
			var steps []NodeSteps
			respSteps, err := GetNodeSteps(projectName, pipelineName, runId, nodeId, req)
			if err != nil {
				log.Error(err)
				return
			}
			err = json.Unmarshal(respSteps, &steps)

			stepChan <- &NodesStepsIndex{index, steps}
			wg.Done()
		}(v.ID, i)
	}

	wg.Wait()
	close(stepChan)

	for oneNodeSteps := range stepChan {
		if oneNodeSteps != nil {
			nodesDetails[oneNodeSteps.Id].Steps = append(nodesDetails[oneNodeSteps.Id].Steps, oneNodeSteps.Steps...)
		}
	}

	return nodesDetails, err
}

Z
Zhuxiaoyang 已提交
958 959
// create jenkins request
func sendJenkinsRequest(baseUrl string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
960 961 962 963 964
	resBody, _, err := jenkinsClient(baseUrl, req)
	return resBody, err
}

func jenkinsClient(baseUrl string, req *http.Request) ([]byte, http.Header, error) {
S
sunzhu 已提交
965 966 967
	newReqUrl, err := url.Parse(baseUrl)
	if err != nil {
		log.Error(err)
S
soulseen 已提交
968
		return nil, nil, err
S
sunzhu 已提交
969 970 971 972 973
	}

	client := &http.Client{Timeout: 30 * time.Second}

	newRequest := &http.Request{
Z
Zhuxiaoyang 已提交
974 975 976 977 978 979
		Method:   req.Method,
		URL:      newReqUrl,
		Header:   req.Header,
		Body:     req.Body,
		Form:     req.Form,
		PostForm: req.PostForm,
S
sunzhu 已提交
980 981 982 983 984
	}

	resp, err := client.Do(newRequest)
	if err != nil {
		log.Error(err)
S
soulseen 已提交
985
		return nil, nil, err
S
sunzhu 已提交
986 987 988
	}

	resBody, _ := getRespBody(resp)
S
soulseen 已提交
989 990
	defer resp.Body.Close()

S
sunzhu 已提交
991
	if resp.StatusCode >= http.StatusBadRequest {
S
soulseen 已提交
992
		log.Errorf("%+v", string(resBody))
S
sunzhu 已提交
993
		jkerr := new(JkError)
Z
Zhuxiaoyang 已提交
994
		jkerr.Code = resp.StatusCode
995
		jkerr.Message = string(resBody)
S
soulseen 已提交
996
		return nil, nil, jkerr
S
sunzhu 已提交
997 998
	}

S
soulseen 已提交
999 1000
	return resBody, resp.Header, nil

S
sunzhu 已提交
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
}

// Decompress response.body of JenkinsAPIResponse
func getRespBody(resp *http.Response) ([]byte, error) {
	var reader io.ReadCloser
	if resp.Header.Get("Content-Encoding") == "gzip" {
		reader, _ = gzip.NewReader(resp.Body)
	} else {
		reader = resp.Body
	}
	resBody, err := ioutil.ReadAll(reader)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	return resBody, err

}