devops.go 19.6 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
}

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

142
	req.Method = http.MethodPut
Z
Zhuxiaoyang 已提交
143
	resBody, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
144
	if err != nil {
S
soulseen 已提交
145
		log.Error(err)
S
sunzhu 已提交
146 147 148
		return nil, err
	}

Z
Zhuxiaoyang 已提交
149 150 151
	return resBody, err
}

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

Z
Zhuxiaoyang 已提交
156
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
157 158 159 160 161 162 163 164
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

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

Z
Zhuxiaoyang 已提交
169
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
170
	if err != nil {
S
soulseen 已提交
171
		log.Error(err)
S
sunzhu 已提交
172 173 174
		return nil, err
	}

Z
Zhuxiaoyang 已提交
175 176 177
	return res, err
}

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

182
	req.Method = http.MethodPut
Z
Zhuxiaoyang 已提交
183 184 185 186 187 188 189 190 191 192 193
	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 已提交
194
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
195

196
	req.Method = http.MethodPut
Z
Zhuxiaoyang 已提交
197 198 199 200 201 202 203 204 205 206 207
	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 已提交
208
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
209 210 211 212 213 214 215 216 217 218 219 220

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

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

	return res, err
}

Z
Zhuxiaoyang 已提交
232 233
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 已提交
234
	log.Info("Jenkins-url: " + baseUrl)
S
sunzhu 已提交
235

Z
Zhuxiaoyang 已提交
236
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
237
	if err != nil {
S
soulseen 已提交
238
		log.Error(err)
S
sunzhu 已提交
239 240 241
		return nil, err
	}

Z
Zhuxiaoyang 已提交
242 243 244
	return res, err
}

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

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

Z
Zhuxiaoyang 已提交
255
	return res, err
Z
Zhuxiaoyang 已提交
256 257
}

Z
Zhuxiaoyang 已提交
258 259
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 已提交
260
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
261 262 263 264 265 266 267 268 269 270 271 272

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

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

	return res, err
}

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

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

	return res, err
}

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

S
soulseen 已提交
301 302 303 304 305 306
	newBody, err := getInputReqBody(req.Body)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	req.Body = newBody
Z
Zhuxiaoyang 已提交
307 308 309 310 311 312 313 314 315
	resBody, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return resBody, err
}

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

S
soulseen 已提交
320 321 322 323 324 325
	newBody, err := getInputReqBody(req.Body)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	req.Body = newBody
Z
Zhuxiaoyang 已提交
326
	resBody, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
327
	if err != nil {
S
soulseen 已提交
328 329 330 331 332 333 334
		log.Error(err)
		return nil, err
	}

	return resBody, err
}

S
soulseen 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
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 已提交
374
func GetConsoleLog(projectName, pipelineName string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
375
	baseUrl := fmt.Sprintf(jenkins.Server+GetConsoleLogUrl+req.URL.RawQuery, projectName, pipelineName)
S
soulseen 已提交
376
	log.Info("Jenkins-url: " + baseUrl)
S
soulseen 已提交
377

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

	return resBody, err
}

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

Z
Zhuxiaoyang 已提交
391
	resBody, err := sendJenkinsRequest(baseUrl, req)
S
soulseen 已提交
392 393 394 395 396
	if err != nil {
		log.Error(err)
		return nil, err
	}

Z
Zhuxiaoyang 已提交
397 398 399
	return resBody, err
}

Z
Zhuxiaoyang 已提交
400 401
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 已提交
402
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
403 404 405 406 407 408 409 410 411 412 413 414

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

Z
Zhuxiaoyang 已提交
417
	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
418 419 420 421 422 423 424 425
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

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

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

	return res, err
}

Z
Zhuxiaoyang 已提交
439
func CheckScriptCompile(req *http.Request) ([]byte, error) {
S
soulseen 已提交
440
	baseUrl := jenkins.Server + CheckScriptCompileUrl
S
soulseen 已提交
441
	log.Info("Jenkins-url: " + baseUrl)
S
soulseen 已提交
442
	req.SetBasicAuth(jenkins.Requester.BasicAuth.Username, jenkins.Requester.BasicAuth.Password)
Z
Zhuxiaoyang 已提交
443 444 445 446 447 448 449 450 451 452 453

	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 已提交
454 455 456 457 458 459 460
	newurl, err := url.Parse(jenkins.Server + CheckCronUrl + req.URL.RawQuery)

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

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

S
soulseen 已提交
466
	resp, err := client.Do(reqJenkins)
Z
Zhuxiaoyang 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
	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 已提交
486
	baseUrl := fmt.Sprintf(jenkins.Server+GetPipelineRunUrl, projectName, pipelineName, runId)
S
soulseen 已提交
487
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
488 489

	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
490 491
	if err != nil {
		log.Error(err)
Z
Zhuxiaoyang 已提交
492
		return nil, err
Z
Zhuxiaoyang 已提交
493 494
	}

Z
Zhuxiaoyang 已提交
495 496 497
	return res, err
}

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

	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
503 504
	if err != nil {
		log.Error(err)
Z
Zhuxiaoyang 已提交
505
		return nil, err
Z
Zhuxiaoyang 已提交
506 507
	}

Z
Zhuxiaoyang 已提交
508 509 510
	return res, err
}

Z
Zhuxiaoyang 已提交
511
func GetPipelineRunNodes(projectName, pipelineName, runId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
512
	baseUrl := fmt.Sprintf(jenkins.Server+GetPipeRunNodesUrl+req.URL.RawQuery, projectName, pipelineName, runId)
S
soulseen 已提交
513
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
514 515 516 517 518 519 520 521 522 523

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

	return res, err
}

Z
Zhuxiaoyang 已提交
524 525
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 已提交
526
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
527 528 529 530 531 532 533 534 535 536 537 538

	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 已提交
539
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
540 541 542 543 544 545 546 547 548 549 550

	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 已提交
551
	baseUrl := fmt.Sprintf(jenkins.Server + ToJenkinsfileUrl)
S
soulseen 已提交
552
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
553 554 555 556 557 558 559 560 561 562 563

	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 已提交
564
	baseUrl := fmt.Sprintf(jenkins.Server + ToJsonUrl)
S
soulseen 已提交
565
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
566 567 568 569 570 571 572 573 574 575 576

	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 已提交
577 578
	baseUrl := fmt.Sprint(jenkins.Server, GetNotifyCommitUrl, req.URL.RawQuery)
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
579 580 581 582 583 584 585 586 587 588 589 590
	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 已提交
591 592
	baseUrl := fmt.Sprint(jenkins.Server, GithubWebhookUrl, req.URL.RawQuery)
	log.Info("Jenkins-url: " + baseUrl)
Z
Zhuxiaoyang 已提交
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
Z
Zhuxiaoyang 已提交
601 602
}

Z
Zhuxiaoyang 已提交
603 604
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 已提交
605
	log.Info("getNodesUrl: " + getNodesUrl)
Z
Zhuxiaoyang 已提交
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
	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 已提交
652
	log.Info("getNodesUrl: " + getNodesUrl)
Z
Zhuxiaoyang 已提交
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
	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 已提交
697 698
// create jenkins request
func sendJenkinsRequest(baseUrl string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
699 700 701 702 703
	resBody, _, err := jenkinsClient(baseUrl, req)
	return resBody, err
}

func jenkinsClient(baseUrl string, req *http.Request) ([]byte, http.Header, error) {
S
sunzhu 已提交
704 705 706
	newReqUrl, err := url.Parse(baseUrl)
	if err != nil {
		log.Error(err)
S
soulseen 已提交
707
		return nil, nil, err
S
sunzhu 已提交
708 709 710 711 712
	}

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

	newRequest := &http.Request{
Z
Zhuxiaoyang 已提交
713 714 715 716 717 718
		Method:   req.Method,
		URL:      newReqUrl,
		Header:   req.Header,
		Body:     req.Body,
		Form:     req.Form,
		PostForm: req.PostForm,
S
sunzhu 已提交
719 720 721 722 723
	}

	resp, err := client.Do(newRequest)
	if err != nil {
		log.Error(err)
S
soulseen 已提交
724
		return nil, nil, err
S
sunzhu 已提交
725 726 727
	}

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

S
sunzhu 已提交
730
	if resp.StatusCode >= http.StatusBadRequest {
S
soulseen 已提交
731
		log.Errorf("%+v", string(resBody))
S
sunzhu 已提交
732
		jkerr := new(JkError)
Z
Zhuxiaoyang 已提交
733 734
		jkerr.Code = resp.StatusCode
		jkerr.Message = http.StatusText(resp.StatusCode)
S
soulseen 已提交
735
		return nil, nil, jkerr
S
sunzhu 已提交
736 737
	}

S
soulseen 已提交
738 739
	return resBody, resp.Header, nil

S
sunzhu 已提交
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
}

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

}