devops.go 22.1 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"
S
sunzhu 已提交
26 27 28
	log "github.com/golang/glog"
	"io"
	"io/ioutil"
S
soulseen 已提交
29
	"kubesphere.io/kubesphere/pkg/gojenkins"
30
	"kubesphere.io/kubesphere/pkg/simple/client/admin_jenkins"
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

S
soulseen 已提交
43
var jenkins *gojenkins.Jenkins
S
sunzhu 已提交
44

S
soulseen 已提交
45 46
func JenkinsInit() {
	jenkins = admin_jenkins.GetJenkins()
S
sunzhu 已提交
47 48
}

Z
Zhuxiaoyang 已提交
49
func GetPipeline(projectName, pipelineName string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
50
	baseUrl := fmt.Sprintf(jenkins.Server+GetPipelineUrl, projectName, pipelineName)
S
sunzhu 已提交
51 52
	log.Infof("Jenkins-url: " + baseUrl)

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

Z
Zhuxiaoyang 已提交
59 60 61
	return res, err
}

Z
Zhuxiaoyang 已提交
62
func SearchPipelines(req *http.Request) ([]byte, error) {
S
soulseen 已提交
63
	baseUrl := jenkins.Server + SearchPipelineUrl + req.URL.RawQuery
Z
Zhuxiaoyang 已提交
64 65
	log.Infof("Jenkins-url: " + baseUrl)

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

	return res, err
}

Z
Zhuxiaoyang 已提交
75
func SearchPipelineRuns(projectName, pipelineName string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
76 77
	baseUrl := fmt.Sprintf(jenkins.Server+SearchPipelineRunUrl, projectName, pipelineName)

S
soulseen 已提交
78
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
79

S
soulseen 已提交
80
	res, err := sendJenkinsRequest(baseUrl+req.URL.RawQuery, req)
Z
Zhuxiaoyang 已提交
81 82 83 84 85 86 87 88
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
89
func GetBranchPipelineRun(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
90
	baseUrl := fmt.Sprintf(jenkins.Server+GetPipeBranchRunUrl, projectName, pipelineName, branchName, runId)
S
soulseen 已提交
91
	log.Info("Jenkins-url: " + baseUrl)
S
sunzhu 已提交
92

Z
Zhuxiaoyang 已提交
93
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
94
	if err != nil {
S
soulseen 已提交
95
		log.Error(err)
S
sunzhu 已提交
96 97 98
		return nil, err
	}

Z
Zhuxiaoyang 已提交
99 100 101
	return res, err
}

Z
Zhuxiaoyang 已提交
102
func GetPipelineRunNodesbyBranch(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
103
	baseUrl := fmt.Sprintf(jenkins.Server+GetBranchPipeRunNodesUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId)
S
soulseen 已提交
104
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
105

Z
Zhuxiaoyang 已提交
106
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
107 108 109 110 111 112 113 114
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

S
soulseen 已提交
115
func GetBranchStepLog(projectName, pipelineName, branchName, runId, nodeId, stepId string, req *http.Request) ([]byte, http.Header, error) {
Z
Zhuxiaoyang 已提交
116
	baseUrl := fmt.Sprintf(jenkins.Server+GetBranchStepLogUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId, nodeId, stepId)
S
soulseen 已提交
117
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
118

S
soulseen 已提交
119
	resBody, header, err := jenkinsClient(baseUrl, req)
Z
Zhuxiaoyang 已提交
120 121
	if err != nil {
		log.Error(err)
S
soulseen 已提交
122
		return nil, nil, err
Z
Zhuxiaoyang 已提交
123 124
	}

S
soulseen 已提交
125
	return resBody, header, err
Z
Zhuxiaoyang 已提交
126 127
}

S
soulseen 已提交
128
func GetStepLog(projectName, pipelineName, runId, nodeId, stepId string, req *http.Request) ([]byte, http.Header, error) {
Z
Zhuxiaoyang 已提交
129
	baseUrl := fmt.Sprintf(jenkins.Server+GetStepLogUrl+req.URL.RawQuery, projectName, pipelineName, runId, nodeId, stepId)
S
soulseen 已提交
130
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
131

S
soulseen 已提交
132
	resBody, header, err := jenkinsClient(baseUrl, req)
Z
Zhuxiaoyang 已提交
133 134
	if err != nil {
		log.Error(err)
S
soulseen 已提交
135
		return nil, nil, err
Z
Zhuxiaoyang 已提交
136 137
	}

S
soulseen 已提交
138 139
	return resBody, header, err

Z
Zhuxiaoyang 已提交
140 141
}

142 143 144 145 146 147 148 149 150 151 152 153 154
func GetSCMServers(scmId string, req *http.Request) ([]byte, error) {
	baseUrl := fmt.Sprintf(jenkins.Server+GetSCMServersUrl, scmId)
	log.Info("Jenkins-url: " + baseUrl)
	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) {
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
	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
	}
	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))
180 181 182 183 184 185 186 187 188 189 190
	baseUrl := fmt.Sprintf(jenkins.Server+CreateSCMServersUrl, scmId)
	log.Info("Jenkins-url: " + baseUrl)
	req.Method = http.MethodPost
	resBody, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	return resBody, err
}

Z
Zhuxiaoyang 已提交
191
func Validate(scmId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
192
	baseUrl := fmt.Sprintf(jenkins.Server+ValidateUrl, scmId)
S
soulseen 已提交
193
	log.Info("Jenkins-url: " + baseUrl)
S
sunzhu 已提交
194

195
	req.Method = http.MethodPut
Z
Zhuxiaoyang 已提交
196
	resBody, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
197
	if err != nil {
S
soulseen 已提交
198
		log.Error(err)
S
sunzhu 已提交
199 200 201
		return nil, err
	}

Z
Zhuxiaoyang 已提交
202 203 204
	return resBody, err
}

Z
Zhuxiaoyang 已提交
205
func GetSCMOrg(scmId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
206
	baseUrl := fmt.Sprintf(jenkins.Server+GetSCMOrgUrl+req.URL.RawQuery, scmId)
S
soulseen 已提交
207
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
208

Z
Zhuxiaoyang 已提交
209
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
210 211 212 213 214 215 216 217
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
218
func GetOrgRepo(scmId, organizationId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
219
	baseUrl := fmt.Sprintf(jenkins.Server+GetOrgRepoUrl+req.URL.RawQuery, scmId, organizationId)
S
soulseen 已提交
220
	log.Info("Jenkins-url: " + baseUrl)
S
sunzhu 已提交
221

Z
Zhuxiaoyang 已提交
222
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
223
	if err != nil {
S
soulseen 已提交
224
		log.Error(err)
S
sunzhu 已提交
225 226 227
		return nil, err
	}

Z
Zhuxiaoyang 已提交
228 229 230
	return res, err
}

Z
Zhuxiaoyang 已提交
231 232
func StopBranchPipeline(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) {
	baseUrl := fmt.Sprintf(jenkins.Server+StopBranchPipelineUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId)
S
soulseen 已提交
233
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
234

235
	req.Method = http.MethodPut
Z
Zhuxiaoyang 已提交
236 237 238 239 240 241 242 243 244 245 246
	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) {
	baseUrl := fmt.Sprintf(jenkins.Server+StopPipelineUrl+req.URL.RawQuery, projectName, pipelineName, runId)
S
soulseen 已提交
247
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
248

249
	req.Method = http.MethodPut
Z
Zhuxiaoyang 已提交
250 251 252 253 254 255 256 257 258 259 260
	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) {
	baseUrl := fmt.Sprintf(jenkins.Server+ReplayBranchPipelineUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId)
S
soulseen 已提交
261
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
262 263 264 265 266 267 268 269 270 271 272 273

	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) {
	baseUrl := fmt.Sprintf(jenkins.Server+ReplayPipelineUrl+req.URL.RawQuery, projectName, pipelineName, runId)
S
soulseen 已提交
274
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
275

Z
Zhuxiaoyang 已提交
276
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
277 278 279 280 281 282 283 284
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
285 286
func GetBranchRunLog(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) {
	baseUrl := fmt.Sprintf(jenkins.Server+GetBranchRunLogUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId)
S
soulseen 已提交
287
	log.Info("Jenkins-url: " + baseUrl)
S
sunzhu 已提交
288

Z
Zhuxiaoyang 已提交
289
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
290
	if err != nil {
S
soulseen 已提交
291
		log.Error(err)
S
sunzhu 已提交
292 293 294
		return nil, err
	}

Z
Zhuxiaoyang 已提交
295 296 297
	return res, err
}

Z
Zhuxiaoyang 已提交
298 299
func GetRunLog(projectName, pipelineName, runId string, req *http.Request) ([]byte, error) {
	baseUrl := fmt.Sprintf(jenkins.Server+GetRunLogUrl+req.URL.RawQuery, projectName, pipelineName, runId)
S
soulseen 已提交
300
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
301

Z
Zhuxiaoyang 已提交
302
	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
303 304 305 306 307
	if err != nil {
		log.Error(err)
		return nil, err
	}

Z
Zhuxiaoyang 已提交
308
	return res, err
Z
Zhuxiaoyang 已提交
309 310
}

Z
Zhuxiaoyang 已提交
311 312
func GetBranchArtifacts(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) {
	baseUrl := fmt.Sprintf(jenkins.Server+GetBranchArtifactsUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId)
S
soulseen 已提交
313
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
314 315 316 317 318 319 320 321 322 323 324 325

	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) {
	baseUrl := fmt.Sprintf(jenkins.Server+GetArtifactsUrl+req.URL.RawQuery, projectName, pipelineName, runId)
S
soulseen 已提交
326
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
327

Z
Zhuxiaoyang 已提交
328
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
329 330 331 332 333 334 335 336
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
337
func GetPipeBranch(projectName, pipelineName string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
338
	baseUrl := fmt.Sprintf(jenkins.Server+GetPipeBranchUrl, projectName, pipelineName)
S
soulseen 已提交
339
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
340

S
soulseen 已提交
341
	res, err := sendJenkinsRequest(baseUrl+req.URL.RawQuery, req)
Z
Zhuxiaoyang 已提交
342 343 344 345 346 347 348 349
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

S
soulseen 已提交
350
func SubmitBranchInputStep(projectName, pipelineName, branchName, runId, nodeId, stepId string, req *http.Request) ([]byte, error) {
Z
Zhuxiaoyang 已提交
351
	baseUrl := fmt.Sprintf(jenkins.Server+CheckBranchPipelineUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId, nodeId, stepId)
S
soulseen 已提交
352
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
353

S
soulseen 已提交
354 355 356 357 358 359
	newBody, err := getInputReqBody(req.Body)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	req.Body = newBody
Z
Zhuxiaoyang 已提交
360 361 362 363 364 365 366 367 368
	resBody, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return resBody, err
}

S
soulseen 已提交
369
func SubmitInputStep(projectName, pipelineName, runId, nodeId, stepId string, req *http.Request) ([]byte, error) {
Z
Zhuxiaoyang 已提交
370
	baseUrl := fmt.Sprintf(jenkins.Server+CheckPipelineUrl+req.URL.RawQuery, projectName, pipelineName, runId, nodeId, stepId)
S
soulseen 已提交
371
	log.Info("Jenkins-url: " + baseUrl)
S
sunzhu 已提交
372

S
soulseen 已提交
373 374 375 376 377 378
	newBody, err := getInputReqBody(req.Body)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	req.Body = newBody
Z
Zhuxiaoyang 已提交
379
	resBody, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
380
	if err != nil {
S
soulseen 已提交
381 382 383 384 385 386 387
		log.Error(err)
		return nil, err
	}

	return resBody, err
}

S
soulseen 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
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 已提交
427
func GetConsoleLog(projectName, pipelineName string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
428
	baseUrl := fmt.Sprintf(jenkins.Server+GetConsoleLogUrl+req.URL.RawQuery, projectName, pipelineName)
S
soulseen 已提交
429
	log.Info("Jenkins-url: " + baseUrl)
S
soulseen 已提交
430

Z
Zhuxiaoyang 已提交
431
	resBody, err := sendJenkinsRequest(baseUrl, req)
S
soulseen 已提交
432 433
	if err != nil {
		log.Error(err)
S
sunzhu 已提交
434 435 436 437 438 439
		return nil, err
	}

	return resBody, err
}

Z
Zhuxiaoyang 已提交
440
func ScanBranch(projectName, pipelineName string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
441
	baseUrl := fmt.Sprintf(jenkins.Server+ScanBranchUrl+req.URL.RawQuery, projectName, pipelineName)
S
soulseen 已提交
442
	log.Info("Jenkins-url: " + baseUrl)
S
soulseen 已提交
443

Z
Zhuxiaoyang 已提交
444
	resBody, err := sendJenkinsRequest(baseUrl, req)
S
soulseen 已提交
445 446 447 448 449
	if err != nil {
		log.Error(err)
		return nil, err
	}

Z
Zhuxiaoyang 已提交
450 451 452
	return resBody, err
}

Z
Zhuxiaoyang 已提交
453 454
func RunBranchPipeline(projectName, pipelineName, branchName string, req *http.Request) ([]byte, error) {
	baseUrl := fmt.Sprintf(jenkins.Server+RunBranchPipelineUrl+req.URL.RawQuery, projectName, pipelineName, branchName)
S
soulseen 已提交
455
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
456 457 458 459 460 461 462 463 464 465 466 467

	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) {
	baseUrl := fmt.Sprintf(jenkins.Server+RunPipelineUrl+req.URL.RawQuery, projectName, pipelineName)
S
soulseen 已提交
468
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
469

Z
Zhuxiaoyang 已提交
470
	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
471 472 473 474 475 476 477 478
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
479
func GetCrumb(req *http.Request) ([]byte, error) {
S
soulseen 已提交
480
	baseUrl := fmt.Sprintf(jenkins.Server + GetCrumbUrl)
S
soulseen 已提交
481
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
482

Z
Zhuxiaoyang 已提交
483
	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
484 485 486 487 488 489 490 491
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

492 493
func CheckScriptCompile(projectName, pipelineName string, req *http.Request) ([]byte, error) {
	baseUrl := fmt.Sprintf(jenkins.Server+CheckScriptCompileUrl, projectName, pipelineName)
S
soulseen 已提交
494
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
495 496 497 498 499 500 501 502 503 504

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

	return resBody, err
}

505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
func CheckCron(projectName string, req *http.Request) (*CheckCronRes, error) {
	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)
	}

	log.Info("Jenkins-url: " + baseUrl)
	newurl, err := url.Parse(baseUrl)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	newurl.RawQuery = newurl.Query().Encode()
S
soulseen 已提交
528 529 530 531

	reqJenkins := &http.Request{
		Method: http.MethodGet,
		URL:    newurl,
532
		Header: req.Header,
S
soulseen 已提交
533 534
	}

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

S
soulseen 已提交
537
	resp, err := client.Do(reqJenkins)
538 539 540 541 542 543 544
	if resp.StatusCode != http.StatusOK {
		resBody, _ := getRespBody(resp)
		return &CheckCronRes{
			Result:  "error",
			Message: string(resBody),
		}, err
	}
Z
Zhuxiaoyang 已提交
545 546
	if err != nil {
		log.Error(err)
547
		return nil, err
Z
Zhuxiaoyang 已提交
548 549 550 551 552 553
	}
	defer resp.Body.Close()

	doc, err := goquery.NewDocumentFromReader(resp.Body)
	if err != nil {
		log.Error(err)
554
		return nil, err
Z
Zhuxiaoyang 已提交
555 556 557 558 559
	}
	doc.Find("div").Each(func(i int, selection *goquery.Selection) {
		res.Message = selection.Text()
		res.Result, _ = selection.Attr("class")
	})
560 561 562 563 564 565 566 567
	if res.Result == "ok" {
		res.LastTime, res.NextTime, err = parseCronJobTime(res.Message)
		if err != nil {
			log.Error(err)
			return nil, err
		}
	}

Z
Zhuxiaoyang 已提交
568 569 570
	return res, err
}

571 572 573 574 575 576 577 578
func parseCronJobTime(msg string) (string, string, error) {
	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 已提交
579
		return "", "", err
580 581 582 583 584 585 586 587
	}
	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 已提交
588
		return "", "", err
589 590 591 592 593 594
	}
	next := nextUinx.Format(time.RFC3339)

	return last, next, nil
}

Z
Zhuxiaoyang 已提交
595
func GetPipelineRun(projectName, pipelineName, runId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
596
	baseUrl := fmt.Sprintf(jenkins.Server+GetPipelineRunUrl, projectName, pipelineName, runId)
S
soulseen 已提交
597
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
598 599

	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
600 601
	if err != nil {
		log.Error(err)
Z
Zhuxiaoyang 已提交
602
		return nil, err
Z
Zhuxiaoyang 已提交
603 604
	}

Z
Zhuxiaoyang 已提交
605 606 607
	return res, err
}

Z
Zhuxiaoyang 已提交
608
func GetBranchPipeline(projectName, pipelineName, branchName string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
609
	baseUrl := fmt.Sprintf(jenkins.Server+GetBranchPipeUrl, projectName, pipelineName, branchName)
S
soulseen 已提交
610
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
611 612

	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
613 614
	if err != nil {
		log.Error(err)
Z
Zhuxiaoyang 已提交
615
		return nil, err
Z
Zhuxiaoyang 已提交
616 617
	}

Z
Zhuxiaoyang 已提交
618 619 620
	return res, err
}

Z
Zhuxiaoyang 已提交
621
func GetPipelineRunNodes(projectName, pipelineName, runId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
622
	baseUrl := fmt.Sprintf(jenkins.Server+GetPipeRunNodesUrl+req.URL.RawQuery, projectName, pipelineName, runId)
S
soulseen 已提交
623
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
624 625 626 627 628 629 630 631 632 633

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

	return res, err
}

Z
Zhuxiaoyang 已提交
634 635
func GetBranchNodeSteps(projectName, pipelineName, branchName, runId, nodeId string, req *http.Request) ([]byte, error) {
	baseUrl := fmt.Sprintf(jenkins.Server+GetBranchNodeStepsUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId, nodeId)
S
soulseen 已提交
636
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
637 638 639 640 641 642 643 644 645 646 647 648

	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) {
	baseUrl := fmt.Sprintf(jenkins.Server+GetNodeStepsUrl+req.URL.RawQuery, projectName, pipelineName, runId, nodeId)
S
soulseen 已提交
649
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
650 651 652 653 654 655 656 657 658 659 660

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

	return res, err
}

func ToJenkinsfile(req *http.Request) ([]byte, error) {
S
soulseen 已提交
661
	baseUrl := fmt.Sprintf(jenkins.Server + ToJenkinsfileUrl)
S
soulseen 已提交
662
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
663 664 665 666 667 668 669 670 671 672 673

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

	return res, err
}

func ToJson(req *http.Request) ([]byte, error) {
S
soulseen 已提交
674
	baseUrl := fmt.Sprintf(jenkins.Server + ToJsonUrl)
S
soulseen 已提交
675
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
676 677 678 679 680 681 682 683 684 685 686

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

	return res, err
}

func GetNotifyCommit(req *http.Request) ([]byte, error) {
S
soulseen 已提交
687 688
	baseUrl := fmt.Sprint(jenkins.Server, GetNotifyCommitUrl, req.URL.RawQuery)
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
689 690 691 692 693 694 695 696 697 698 699 700
	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) {
S
soulseen 已提交
701 702
	baseUrl := fmt.Sprint(jenkins.Server, GithubWebhookUrl, req.URL.RawQuery)
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
703 704 705 706 707 708 709 710

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

	return res, err
Z
Zhuxiaoyang 已提交
711 712
}

Z
Zhuxiaoyang 已提交
713 714
func GetBranchNodesDetail(projectName, pipelineName, branchName, runId string, req *http.Request) ([]NodesDetail, error) {
	getNodesUrl := fmt.Sprintf(jenkins.Server+GetBranchPipeRunNodesUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId)
S
soulseen 已提交
715
	log.Info("getNodesUrl: " + getNodesUrl)
Z
Zhuxiaoyang 已提交
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
	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) {
	getNodesUrl := fmt.Sprintf(jenkins.Server+GetPipeRunNodesUrl+req.URL.RawQuery, projectName, pipelineName, runId)
S
soulseen 已提交
762
	log.Info("getNodesUrl: " + getNodesUrl)
Z
Zhuxiaoyang 已提交
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
	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 已提交
807 808
// create jenkins request
func sendJenkinsRequest(baseUrl string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
809 810 811 812 813
	resBody, _, err := jenkinsClient(baseUrl, req)
	return resBody, err
}

func jenkinsClient(baseUrl string, req *http.Request) ([]byte, http.Header, error) {
S
sunzhu 已提交
814 815 816
	newReqUrl, err := url.Parse(baseUrl)
	if err != nil {
		log.Error(err)
S
soulseen 已提交
817
		return nil, nil, err
S
sunzhu 已提交
818 819 820 821 822
	}

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

	newRequest := &http.Request{
Z
Zhuxiaoyang 已提交
823 824 825 826 827 828
		Method:   req.Method,
		URL:      newReqUrl,
		Header:   req.Header,
		Body:     req.Body,
		Form:     req.Form,
		PostForm: req.PostForm,
S
sunzhu 已提交
829 830 831 832 833
	}

	resp, err := client.Do(newRequest)
	if err != nil {
		log.Error(err)
S
soulseen 已提交
834
		return nil, nil, err
S
sunzhu 已提交
835 836 837
	}

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

S
sunzhu 已提交
840
	if resp.StatusCode >= http.StatusBadRequest {
S
soulseen 已提交
841
		log.Errorf("%+v", string(resBody))
S
sunzhu 已提交
842
		jkerr := new(JkError)
Z
Zhuxiaoyang 已提交
843
		jkerr.Code = resp.StatusCode
844
		jkerr.Message = string(resBody)
S
soulseen 已提交
845
		return nil, nil, jkerr
S
sunzhu 已提交
846 847
	}

S
soulseen 已提交
848 849
	return resBody, resp.Header, nil

S
sunzhu 已提交
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
}

// 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

}