-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
119 lines (103 loc) · 2.62 KB
/
build.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package jenkins
import (
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"time"
)
type Build struct {
*Item
Number int
}
func NewBuild(url, class string, jenkins *Jenkins) *Build {
return &Build{Item: NewItem(url, class, jenkins), Number: parseId(url)}
}
func (b *Build) IsBuilding() (bool, error) {
var build struct {
Class string `json:"_class"`
Building bool `json:"building"`
}
err := b.ApiJson(&build, &ApiJsonOpts{Tree: "building"})
return build.Building, err
}
func (b *Build) GetResult() (string, error) {
status := make(map[string]string)
err := b.ApiJson(&status, &ApiJsonOpts{Tree: "result"})
return status["result"], err
}
func (b *Build) Delete() (*http.Response, error) {
return b.Request("POST", "doDelete", nil)
}
func (b *Build) Stop() (*http.Response, error) {
return b.Request("POST", "stop", nil)
}
func (b *Build) Kill() (*http.Response, error) {
return b.Request("POST", "kill", nil)
}
func (b *Build) Term() (*http.Response, error) {
return b.Request("POST", "term", nil)
}
var re = regexp.MustCompile(`\w+[/]?$`)
func (b *Build) GetJob() (*Job, error) {
jobName, _ := b.jenkins.URL2Name(re.ReplaceAllLiteralString(b.URL, ""))
return b.jenkins.GetJob(jobName)
}
func (b *Build) LoopLog(f func(line string) error) error {
resp, err := b.Request("GET", "consoleText", nil)
if err != nil {
return err
}
return scanResponse(resp, f)
}
func scanResponse(resp *http.Response, f func(line string) error) error {
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
return f(string(data))
}
func (b *Build) LoopProgressiveLog(kind string, f func(line string) error) error {
var entry string
switch kind {
case "html":
entry = "logText/progressiveHtml"
case "text":
entry = "logText/progressiveText"
default:
panic("kind must be html or text")
}
start := "0"
for {
resp, err := b.Request("GET", fmt.Sprintf("%s?start=%s", entry, start), nil)
time.Sleep(3 * time.Second)
if err != nil {
return err
}
if start == resp.Header.Get("X-Text-Size") {
continue
}
if err := scanResponse(resp, f); err != nil {
return err
}
if resp.Header.Get("X-More-Data") != "true" {
break
}
start = resp.Header.Get("X-Text-Size")
}
return nil
}
func (b *Build) GetDescription() (string, error) {
data := make(map[string]string)
if err := b.ApiJson(&data, &ApiJsonOpts{Tree: "description"}); err != nil {
return "", err
}
return data["description"], nil
}
func (b *Build) SetDescription(description string) (*http.Response, error) {
v := url.Values{}
v.Add("description", description)
return b.Request("POST", "submitDescription?"+v.Encode(), nil)
}