This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 398
/
jenkins.go
81 lines (72 loc) · 2.06 KB
/
jenkins.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package components
import (
"github.com/astaxie/beego"
"github.com/bndr/gojenkins"
"net/url"
"regexp"
"strings"
)
type BasJenkins struct {
baseComponents BaseComponents
}
func (c *BasJenkins) SetBaseComponents(b BaseComponents) {
c.baseComponents = b
}
/**
* 获取提交历史
*
*/
type JenkinData struct {
Build string `json:"build"`
TarUrl string `json:"tar_url"`
MD5 interface{} `json:"md5"`
}
func (c *BasJenkins) GetCommitList(count int) ([]JenkinData, error) {
//获取url 和job
var list []JenkinData
u, err := url.Parse(c.baseComponents.project.RepoUrl)
jenkinsUrl := u.Scheme + "://" + u.Host
jobs := strings.Split(u.Path, "/job/")
job := strings.Trim(jobs[1], "/")
jenkins := gojenkins.CreateJenkins(nil, jenkinsUrl, "")
if beego.AppConfig.String("JenkinsUserName") != "" {
jenkins = gojenkins.CreateJenkins(nil, jenkinsUrl, beego.AppConfig.String("JenkinsUserName"), beego.AppConfig.String("JenkinsPwd"))
}
_, err = jenkins.Init()
if err != nil {
beego.Error(err, "Jenkins Initialization failed")
return list, err
}
builds, _ := jenkins.GetAllBuildIds(job)
for _, b := range builds {
build, _ := jenkins.GetBuild(job, b.Number)
if len(build.Raw.Artifacts) == 0 {
the_base := strings.Split(build.Base, "/")
the_base_id := the_base[len(the_base)-1]
var de_map JenkinData
de_map.Build = the_base_id + "/null"
de_map.TarUrl = "null"
de_map.MD5 = ""
list = append(list, de_map)
continue
}
//取ID号
path := build.Raw.Artifacts[0].RelativePath
the_base := strings.Split(build.Base, "/")
the_base_id := the_base[len(the_base)-1]
reg := regexp.MustCompile("target/|-assembly.tar.gz|tar.gz")
new_path := reg.ReplaceAllString(path, "")
//new_path := strings.Replace(path, reg, "", -1)
//拼接url
url := "null"
//var md5 interface{}
url = jenkinsUrl + build.Base + "/artifact/" + path
//md5 = build.Raw.MavenArtifacts
var build_map JenkinData
build_map.Build = the_base_id + "/" + new_path
build_map.TarUrl = url
//build_map.MD5 = md5
list = append(list, build_map)
}
return list, nil
}