devops.go 20.2 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"
Z
Zhuxiaoyang 已提交
33
	"sync"
S
sunzhu 已提交
34 35 36
	"time"
)

Z
Zhuxiaoyang 已提交
37 38
const channelMaxCapacity = 100

S
soulseen 已提交
39
var jenkins *gojenkins.Jenkins
S
sunzhu 已提交
40

S
soulseen 已提交
41 42
func JenkinsInit() {
	jenkins = admin_jenkins.GetJenkins()
S
sunzhu 已提交
43 44
}

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

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

Z
Zhuxiaoyang 已提交
55 56 57
	return res, err
}

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

Z
Zhuxiaoyang 已提交
62
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
63 64 65 66 67 68 69 70
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

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

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

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

	return res, err
}

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

Z
Zhuxiaoyang 已提交
89
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
90
	if err != nil {
S
soulseen 已提交
91
		log.Error(err)
S
sunzhu 已提交
92 93 94
		return nil, err
	}

Z
Zhuxiaoyang 已提交
95 96 97
	return res, err
}

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

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

	return res, err
}

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

S
soulseen 已提交
115
	resBody, header, err := jenkinsClient(baseUrl, req)
Z
Zhuxiaoyang 已提交
116 117
	if err != nil {
		log.Error(err)
S
soulseen 已提交
118
		return nil, nil, err
Z
Zhuxiaoyang 已提交
119 120
	}

S
soulseen 已提交
121
	return resBody, header, err
Z
Zhuxiaoyang 已提交
122 123
}

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

S
soulseen 已提交
128
	resBody, header, err := jenkinsClient(baseUrl, req)
Z
Zhuxiaoyang 已提交
129 130
	if err != nil {
		log.Error(err)
S
soulseen 已提交
131
		return nil, nil, err
Z
Zhuxiaoyang 已提交
132 133
	}

S
soulseen 已提交
134 135
	return resBody, header, err

Z
Zhuxiaoyang 已提交
136 137
}

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
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) {
	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 已提交
162
func Validate(scmId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
163
	baseUrl := fmt.Sprintf(jenkins.Server+ValidateUrl, scmId)
S
soulseen 已提交
164
	log.Info("Jenkins-url: " + baseUrl)
S
sunzhu 已提交
165

166
	req.Method = http.MethodPut
Z
Zhuxiaoyang 已提交
167
	resBody, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
168
	if err != nil {
S
soulseen 已提交
169
		log.Error(err)
S
sunzhu 已提交
170 171 172
		return nil, err
	}

Z
Zhuxiaoyang 已提交
173 174 175
	return resBody, err
}

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

Z
Zhuxiaoyang 已提交
180
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
181 182 183 184 185 186 187 188
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

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

Z
Zhuxiaoyang 已提交
193
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
194
	if err != nil {
S
soulseen 已提交
195
		log.Error(err)
S
sunzhu 已提交
196 197 198
		return nil, err
	}

Z
Zhuxiaoyang 已提交
199 200 201
	return res, err
}

Z
Zhuxiaoyang 已提交
202 203
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 已提交
204
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
205

206
	req.Method = http.MethodPut
Z
Zhuxiaoyang 已提交
207 208 209 210 211 212 213 214 215 216 217
	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 已提交
218
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
219

220
	req.Method = http.MethodPut
Z
Zhuxiaoyang 已提交
221 222 223 224 225 226 227 228 229 230 231
	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 已提交
232
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
233 234 235 236 237 238 239 240 241 242 243 244

	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 已提交
245
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
246

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

	return res, err
}

Z
Zhuxiaoyang 已提交
256 257
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 已提交
258
	log.Info("Jenkins-url: " + baseUrl)
S
sunzhu 已提交
259

Z
Zhuxiaoyang 已提交
260
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
261
	if err != nil {
S
soulseen 已提交
262
		log.Error(err)
S
sunzhu 已提交
263 264 265
		return nil, err
	}

Z
Zhuxiaoyang 已提交
266 267 268
	return res, err
}

Z
Zhuxiaoyang 已提交
269 270
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 已提交
271
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
272

Z
Zhuxiaoyang 已提交
273
	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
274 275 276 277 278
	if err != nil {
		log.Error(err)
		return nil, err
	}

Z
Zhuxiaoyang 已提交
279
	return res, err
Z
Zhuxiaoyang 已提交
280 281
}

Z
Zhuxiaoyang 已提交
282 283
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 已提交
284
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
285 286 287 288 289 290 291 292 293 294 295 296

	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 已提交
297
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
298

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

	return res, err
}

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

S
soulseen 已提交
312
	res, err := sendJenkinsRequest(baseUrl+req.URL.RawQuery, req)
Z
Zhuxiaoyang 已提交
313 314 315 316 317 318 319 320
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

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

S
soulseen 已提交
325 326 327 328 329 330
	newBody, err := getInputReqBody(req.Body)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	req.Body = newBody
Z
Zhuxiaoyang 已提交
331 332 333 334 335 336 337 338 339
	resBody, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return resBody, err
}

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

S
soulseen 已提交
344 345 346 347 348 349
	newBody, err := getInputReqBody(req.Body)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	req.Body = newBody
Z
Zhuxiaoyang 已提交
350
	resBody, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
351
	if err != nil {
S
soulseen 已提交
352 353 354 355 356 357 358
		log.Error(err)
		return nil, err
	}

	return resBody, err
}

S
soulseen 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
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 已提交
398
func GetConsoleLog(projectName, pipelineName string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
399
	baseUrl := fmt.Sprintf(jenkins.Server+GetConsoleLogUrl+req.URL.RawQuery, projectName, pipelineName)
S
soulseen 已提交
400
	log.Info("Jenkins-url: " + baseUrl)
S
soulseen 已提交
401

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

	return resBody, err
}

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

Z
Zhuxiaoyang 已提交
415
	resBody, err := sendJenkinsRequest(baseUrl, req)
S
soulseen 已提交
416 417 418 419 420
	if err != nil {
		log.Error(err)
		return nil, err
	}

Z
Zhuxiaoyang 已提交
421 422 423
	return resBody, err
}

Z
Zhuxiaoyang 已提交
424 425
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 已提交
426
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
427 428 429 430 431 432 433 434 435 436 437 438

	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 已提交
439
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
440

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

	return res, err
}

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

Z
Zhuxiaoyang 已提交
454
	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
455 456 457 458 459 460 461 462
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
463
func CheckScriptCompile(req *http.Request) ([]byte, error) {
S
soulseen 已提交
464
	baseUrl := jenkins.Server + CheckScriptCompileUrl
S
soulseen 已提交
465
	log.Info("Jenkins-url: " + baseUrl)
S
soulseen 已提交
466
	req.SetBasicAuth(jenkins.Requester.BasicAuth.Username, jenkins.Requester.BasicAuth.Password)
Z
Zhuxiaoyang 已提交
467 468 469 470 471 472 473 474 475 476 477

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

	return resBody, err
}

func CheckCron(req *http.Request) (*CheckCronRes, error) {
S
soulseen 已提交
478 479 480 481 482 483 484
	newurl, err := url.Parse(jenkins.Server + CheckCronUrl + req.URL.RawQuery)

	reqJenkins := &http.Request{
		Method: http.MethodGet,
		URL:    newurl,
		Header: http.Header{},
	}
Z
Zhuxiaoyang 已提交
485
	var res = new(CheckCronRes)
S
soulseen 已提交
486 487 488
	client := &http.Client{Timeout: 30 * time.Second}

	reqJenkins.SetBasicAuth(jenkins.Requester.BasicAuth.Username, jenkins.Requester.BasicAuth.Password)
Z
Zhuxiaoyang 已提交
489

S
soulseen 已提交
490
	resp, err := client.Do(reqJenkins)
Z
Zhuxiaoyang 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
	if err != nil {
		log.Error(err)
		return res, err
	}
	defer resp.Body.Close()

	doc, err := goquery.NewDocumentFromReader(resp.Body)
	if err != nil {
		log.Error(err)
		return res, err
	}
	doc.Find("div").Each(func(i int, selection *goquery.Selection) {
		res.Message = selection.Text()
		res.Result, _ = selection.Attr("class")
	})
	return res, err
}

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

	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
514 515
	if err != nil {
		log.Error(err)
Z
Zhuxiaoyang 已提交
516
		return nil, err
Z
Zhuxiaoyang 已提交
517 518
	}

Z
Zhuxiaoyang 已提交
519 520 521
	return res, err
}

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

	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
527 528
	if err != nil {
		log.Error(err)
Z
Zhuxiaoyang 已提交
529
		return nil, err
Z
Zhuxiaoyang 已提交
530 531
	}

Z
Zhuxiaoyang 已提交
532 533 534
	return res, err
}

Z
Zhuxiaoyang 已提交
535
func GetPipelineRunNodes(projectName, pipelineName, runId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
536
	baseUrl := fmt.Sprintf(jenkins.Server+GetPipeRunNodesUrl+req.URL.RawQuery, projectName, pipelineName, runId)
S
soulseen 已提交
537
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
538 539 540 541 542 543 544 545 546 547

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

	return res, err
}

Z
Zhuxiaoyang 已提交
548 549
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 已提交
550
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
551 552 553 554 555 556 557 558 559 560 561 562

	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 已提交
563
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
564 565 566 567 568 569 570 571 572 573 574

	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 已提交
575
	baseUrl := fmt.Sprintf(jenkins.Server + ToJenkinsfileUrl)
S
soulseen 已提交
576
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
577 578 579 580 581 582 583 584 585 586 587

	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 已提交
588
	baseUrl := fmt.Sprintf(jenkins.Server + ToJsonUrl)
S
soulseen 已提交
589
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
590 591 592 593 594 595 596 597 598 599 600

	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 已提交
601 602
	baseUrl := fmt.Sprint(jenkins.Server, GetNotifyCommitUrl, req.URL.RawQuery)
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
603 604 605 606 607 608 609 610 611 612 613 614
	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 已提交
615 616
	baseUrl := fmt.Sprint(jenkins.Server, GithubWebhookUrl, req.URL.RawQuery)
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
617 618 619 620 621 622 623 624

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

	return res, err
Z
Zhuxiaoyang 已提交
625 626
}

Z
Zhuxiaoyang 已提交
627 628
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 已提交
629
	log.Info("getNodesUrl: " + getNodesUrl)
Z
Zhuxiaoyang 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
	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 已提交
676
	log.Info("getNodesUrl: " + getNodesUrl)
Z
Zhuxiaoyang 已提交
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
	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 已提交
721 722
// create jenkins request
func sendJenkinsRequest(baseUrl string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
723 724 725 726 727
	resBody, _, err := jenkinsClient(baseUrl, req)
	return resBody, err
}

func jenkinsClient(baseUrl string, req *http.Request) ([]byte, http.Header, error) {
S
sunzhu 已提交
728 729 730
	newReqUrl, err := url.Parse(baseUrl)
	if err != nil {
		log.Error(err)
S
soulseen 已提交
731
		return nil, nil, err
S
sunzhu 已提交
732 733 734 735 736
	}

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

	newRequest := &http.Request{
Z
Zhuxiaoyang 已提交
737 738 739 740 741 742
		Method:   req.Method,
		URL:      newReqUrl,
		Header:   req.Header,
		Body:     req.Body,
		Form:     req.Form,
		PostForm: req.PostForm,
S
sunzhu 已提交
743 744 745 746 747
	}

	resp, err := client.Do(newRequest)
	if err != nil {
		log.Error(err)
S
soulseen 已提交
748
		return nil, nil, err
S
sunzhu 已提交
749 750 751
	}

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

S
sunzhu 已提交
754
	if resp.StatusCode >= http.StatusBadRequest {
S
soulseen 已提交
755
		log.Errorf("%+v", string(resBody))
S
sunzhu 已提交
756
		jkerr := new(JkError)
Z
Zhuxiaoyang 已提交
757
		jkerr.Code = resp.StatusCode
758
		jkerr.Message = string(resBody)
S
soulseen 已提交
759
		return nil, nil, jkerr
S
sunzhu 已提交
760 761
	}

S
soulseen 已提交
762 763
	return resBody, resp.Header, nil

S
sunzhu 已提交
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
}

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

}