-
Notifications
You must be signed in to change notification settings - Fork 3
/
builds.go
216 lines (190 loc) · 4.44 KB
/
builds.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package teamcity
import (
"encoding/json"
"fmt"
"log"
"net/http"
"sync"
)
func (c *Client) GetBuildDetails(id BuildID) (BuildDetails, error) {
buildDetails := BuildDetails{}
chData := DataFlow{
Response: make(chan *http.Response, 1),
}
url := fmt.Sprint(c.URL, "/app/rest/builds/id:", id, "/resulting-properties")
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return buildDetails, err
}
chData.Request = req
c.Flow <- chData
for res := range chData.Response {
body, err := processResponse(res)
if err != nil {
return buildDetails, err
}
if err := json.Unmarshal(body, &buildDetails); err != nil {
return buildDetails, err
}
}
return buildDetails, nil
}
func (c *Client) GetBuildsByParams(bl BuildLocator) (Builds, error) {
builds := Builds{}
url := fmt.Sprint(c.URL, "/app/rest/builds/?locator=", convertLocatorToString(bl))
for {
buildsIter := Builds{}
chData := DataFlow{
Response: make(chan *http.Response, 1),
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return builds, err
}
chData.Request = req
c.Flow <- chData
for res := range chData.Response {
body, err := processResponse(res)
if err != nil {
return builds, err
}
if err := json.Unmarshal(body, &buildsIter); err != nil {
return builds, err
}
}
for i := range buildsIter.Builds {
if buildsIter.Builds[i].BranchName == "" {
buildsIter.Builds[i].BranchName = "<default>"
}
builds.Builds = append(builds.Builds, buildsIter.Builds[i])
}
if bl.Count == 0 && buildsIter.NextHref != "" {
url = c.URL + buildsIter.NextHref
} else {
break
}
}
return builds, nil
}
func (c *Client) GetBuildStat(id BuildID) (BuildStatistics, error) {
stat := BuildStatistics{}
chData := DataFlow{
Response: make(chan *http.Response, 1),
}
url := fmt.Sprint(c.URL, "/app/rest/builds/id:", id, "/statistics")
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return stat, err
}
chData.Request = req
c.Flow <- chData
for res := range chData.Response {
body, err := processResponse(res)
if err != nil {
return stat, err
}
if err := json.Unmarshal(body, &stat); err != nil {
return stat, err
}
}
return stat, nil
}
func (c *Client) getBuildsByParamsPipelined(in <-chan BuildLocator, out chan<- Build) {
wg := new(sync.WaitGroup)
for filter := range in {
wg.Add(1)
go func(f BuildLocator) {
defer wg.Done()
build, err := c.GetBuildsByParams(f)
if err != nil {
log.Println(err)
return
}
if len(build.Builds) > 0 {
out <- build.Builds[0]
} else {
log.Printf("No builds found for build configuration '%s', branch '%s'", f.BuildType, f.Branch)
return
}
}(filter)
}
wg.Wait()
close(out)
}
func (c *Client) GetLatestBuild(bl BuildLocator) (Builds, error) {
chFilters := make(chan BuildLocator)
chBuilds := make(chan Build)
builds := Builds{}
go c.getBuildsByParamsPipelined(chFilters, chBuilds)
wg1 := new(sync.WaitGroup)
wg1.Add(1)
go func() {
defer wg1.Done()
for build := range chBuilds {
builds.Builds = append(builds.Builds, build)
}
}()
// get build types
buildTypes := []BuildTypeID{}
if bl.BuildType == "" {
bt, err := c.GetAllBuildConfigurations()
if err != nil {
log.Fatal(err)
}
for i := range bt.BuildTypes {
buildTypes = append(buildTypes, bt.BuildTypes[i].ID)
}
} else {
buildTypes = append(buildTypes, bl.BuildType)
}
// get branches and combine filters
wg2 := new(sync.WaitGroup)
if bl.Branch == "" {
for _, buildType := range buildTypes {
wg2.Add(1)
go func(bt BuildTypeID) {
defer wg2.Done()
branches, err := c.GetAllBranches(bt)
if err != nil {
log.Fatal(err)
}
if len(branches.Branches) == 1 {
branches.Branches[0].Name = ""
}
for _, branch := range branches.Branches {
f := BuildLocator{
BuildType: bt,
Branch: branch.Name,
Status: bl.Status,
Running: bl.Running,
Canceled: bl.Canceled,
Count: 1,
}
chFilters <- f
}
}(buildType)
}
wg2.Wait()
close(chFilters)
} else {
for _, buildType := range buildTypes {
wg2.Add(1)
go func(bt BuildTypeID) {
defer wg2.Done()
f := BuildLocator{
BuildType: bt,
Branch: bl.Branch,
Status: bl.Status,
Running: bl.Running,
Canceled: bl.Canceled,
Count: 1,
}
chFilters <- f
}(buildType)
}
wg2.Wait()
close(chFilters)
}
wg1.Wait()
return builds, nil
}