Skip to content
This repository was archived by the owner on Apr 17, 2019. It is now read-only.

Commit 21010e3

Browse files
committed
adding build numbers listing
1 parent b72fa50 commit 21010e3

File tree

2 files changed

+92
-18
lines changed

2 files changed

+92
-18
lines changed

test-utils/utils/bucket.go

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,28 @@ func (b *Bucket) ExpandPathURL(pathElements ...interface{}) *url.URL {
9494
// enclosed by the provided path. Note that there's currently no way to get a
9595
// path that ends in '/'.
9696
func (b *Bucket) ExpandListURL(pathElements ...interface{}) *url.URL {
97+
return buildURL(map[string]string{
98+
// GCS api doesn't like preceding '/', so remove it.
99+
"prefix": strings.TrimPrefix(joinStringsAndInts(pathElements...), "/"),
100+
})
101+
}
102+
103+
// ExpandListDirURL produces the URL for a list API query which will list directories
104+
// enclosed by the provided path. Note that there's currently no way to get a
105+
// path that ends in '/'.
106+
func (b *Bucket) ExpandListDirURL(pathElements ...interface{}) *url.URL {
107+
return buildURL(map[string]string{
108+
// GCS api doesn't like preceding '/', so remove it.
109+
"prefix": strings.TrimPrefix(joinStringsAndInts(pathElements...)+"/", "/"),
110+
"delimiter": "/",
111+
})
112+
}
113+
114+
func (b *Bucket) buildUrl(parameters map[string]string) *url.URL {
97115
q := url.Values{}
98-
// GCS api doesn't like preceding '/', so remove it.
99-
q.Set("prefix", strings.TrimPrefix(joinStringsAndInts(pathElements...), "/"))
116+
for key, value := range parameters {
117+
q.Set(key, value)
118+
}
100119
return &url.URL{
101120
Scheme: b.scheme,
102121
Host: b.listHost,
@@ -109,31 +128,58 @@ func (b *Bucket) ExpandListURL(pathElements ...interface{}) *url.URL {
109128
// The returned file name included the complete path from bucket root
110129
func (b *Bucket) List(pathElements ...interface{}) ([]string, error) {
111130
listURL := b.ExpandListURL(pathElements...)
112-
res, err := getResponseWithRetry(listURL.String())
131+
data, err := queryURL(listURL.String())
132+
if err != nil {
133+
return nil, err
134+
}
135+
var ret []string
136+
if _, ok := data["items"]; !ok {
137+
glog.Warningf("No matching files were found (from: %v)", listURL.String())
138+
return ret, nil
139+
}
140+
for _, item := range data["items"].([]interface{}) {
141+
ret = append(ret, (item.(map[string]interface{})["name"]).(string))
142+
}
143+
return ret, nil
144+
}
145+
146+
// ListDirs returns a list of all directories inside the given path.
147+
// The returned direcotry name included the complete path from bucket root
148+
func (b *Bucket) ListDirs(pathElements ...interface{}) ([]string, error) {
149+
listURL := b.ExpandListDirURL(pathElements...)
150+
data, err := queryURL(listURL.String())
151+
if err != nil {
152+
return nil, err
153+
}
154+
var ret []string
155+
if _, ok := data["prefixes"]; !ok {
156+
glog.Warningf("No matching dirs were found (from: %v)", listURL.String())
157+
return ret, nil
158+
}
159+
for _, item := range data["prefixes"].([]interface{}) {
160+
ret = append(ret, item.(string))
161+
}
162+
return ret, nil
163+
}
164+
165+
func queryURL(url string) (map[string]interface{}, error) {
166+
res, err := getResponseWithRetry(url)
113167
if err != nil {
114-
return nil, fmt.Errorf("Failed to GET %v: %v", listURL, err)
168+
return nil, fmt.Errorf("Failed to GET %v: %v", url, err)
115169
}
116170
if res.StatusCode != http.StatusOK {
117-
return nil, fmt.Errorf("Got a non-success response %v while listing %v", res.StatusCode, listURL.String())
171+
return nil, fmt.Errorf("Got a non-success response %v while listing %v", res.StatusCode, url)
118172
}
119173
body, err := ioutil.ReadAll(res.Body)
120174
if err != nil {
121-
return nil, fmt.Errorf("Failed to read the response for %v: %v", listURL.String(), err)
175+
return nil, fmt.Errorf("Failed to read the response for %v: %v", url, err)
122176
}
123177
var data map[string]interface{}
124178
err = json.Unmarshal(body, &data)
125179
if err != nil {
126180
return nil, fmt.Errorf("Failed to unmarshal %v: %v", string(body), err)
127181
}
128-
var ret []string
129-
if _, ok := data["items"]; !ok {
130-
glog.Warningf("No matching files were found (from: %v)", listURL.String())
131-
return ret, nil
132-
}
133-
for _, item := range data["items"].([]interface{}) {
134-
ret = append(ret, (item.(map[string]interface{})["name"]).(string))
135-
}
136-
return ret, nil
182+
return data, nil
137183
}
138184

139185
func joinStringsAndInts(pathElements ...interface{}) string {

test-utils/utils/utils.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"io/ioutil"
2424
"net/http"
25+
"strconv"
2526
"strings"
2627
"sync"
2728

@@ -199,15 +200,43 @@ func (u *Utils) GetLastestBuildNumberFromJenkinsGoogleBucket(job string) (int, e
199200
return lastBuildNo, nil
200201
}
201202

203+
// GetBuildNumbersFromJenkinsGoogleBucket returns list of available build numbers
204+
// of the given job from the Google project's GCS bucket.
205+
func (u *Utils) GetBuildNumbersFromJenkinsGoogleBucket(job string) ([]int, error) {
206+
var results []string
207+
var err error
208+
if u.needsDeref(job) {
209+
results, err = u.bucket.ListDirs(u.pullDirectory, lookUpDirectory, job)
210+
} else {
211+
results, err = u.bucket.ListDirs(u.directory, job)
212+
}
213+
if err != nil {
214+
return nil, err
215+
}
216+
217+
jobPrefix := fmt.Sprintf("%s/%s/", u.directory, job)
218+
builds := make([]int, 0)
219+
for _, r := range results {
220+
build := strings.TrimPrefix(r, jobPrefix)
221+
build = strings.TrimSuffix(build, "/")
222+
buildNo, err := strconv.Atoi(build)
223+
if err != nil {
224+
fmt.Printf("jobPrefix: %s", jobPrefix)
225+
return nil, fmt.Errorf("unknown build name convention: %s", build)
226+
}
227+
builds = append(builds, buildNo)
228+
}
229+
return builds, nil
230+
}
231+
202232
// StartedFile is a type in which we store test starting information in GCS as started.json
203233
type StartedFile struct {
204234
Version string `json:"version"`
205235
Timestamp uint64 `json:"timestamp"`
206236
JenkinsNode string `json:"jenkins-node"`
207237
}
208238

209-
// CheckStartedStatus reads the started.json file for a given job and build number.
210-
// It returns true if the result stored there is success, and false otherwise.
239+
// CheckStartedStatus returns the started.json file for a given job and build number.
211240
func (u *Utils) CheckStartedStatus(job string, buildNumber int) (*StartedFile, error) {
212241
response, err := u.GetFileFromJenkinsGoogleBucket(job, buildNumber, "started.json")
213242
if err != nil {
@@ -273,7 +302,6 @@ func (u *Utils) ListFilesInBuild(job string, buildNumber int, prefix string) ([]
273302
}
274303
return u.bucket.List(dir, prefix)
275304
}
276-
277305
return u.bucket.List(u.directory, job, buildNumber, prefix)
278306
}
279307

0 commit comments

Comments
 (0)