| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package buildbucket | 5 package buildbucket |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "encoding/json" | 8 "encoding/json" |
| 9 "fmt" | 9 "fmt" |
| 10 "net/url" | 10 "net/url" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 func(err error, wait time.Duration) { | 49 func(err error, wait time.Duration) { |
| 50 logging.WithError(err).Warningf(c, "buildbucket search r
equest failed transiently, will retry in %s", wait) | 50 logging.WithError(err).Warningf(c, "buildbucket search r
equest failed transiently, will retry in %s", wait) |
| 51 }) | 51 }) |
| 52 return res, err | 52 return res, err |
| 53 } | 53 } |
| 54 | 54 |
| 55 // fetchBuilds fetches builds given a criteria. | 55 // fetchBuilds fetches builds given a criteria. |
| 56 // The returned builds are sorted by build creation descending. | 56 // The returned builds are sorted by build creation descending. |
| 57 // count defines maximum number of builds to fetch; if <0, defaults to 100. | 57 // count defines maximum number of builds to fetch; if <0, defaults to 100. |
| 58 func fetchBuilds(c context.Context, client *buildbucket.Service, bucket, builder
, | 58 func fetchBuilds(c context.Context, client *buildbucket.Service, bucket, builder
, |
| 59 » status string, count int) ([]*buildbucket.ApiBuildMessage, error) { | 59 » status string, count int) ([]*buildbucket.ApiCommonBuildMessage, error)
{ |
| 60 | 60 |
| 61 req := client.Search() | 61 req := client.Search() |
| 62 req.Bucket(bucket) | 62 req.Bucket(bucket) |
| 63 req.Status(status) | 63 req.Status(status) |
| 64 req.Tag("builder:" + builder) | 64 req.Tag("builder:" + builder) |
| 65 | 65 |
| 66 if count < 0 { | 66 if count < 0 { |
| 67 count = 100 | 67 count = 100 |
| 68 } | 68 } |
| 69 | 69 |
| 70 » fetched := make([]*buildbucket.ApiBuildMessage, 0, count) | 70 » fetched := make([]*buildbucket.ApiCommonBuildMessage, 0, count) |
| 71 start := clock.Now(c) | 71 start := clock.Now(c) |
| 72 for len(fetched) < count { | 72 for len(fetched) < count { |
| 73 req.MaxBuilds(int64(count - len(fetched))) | 73 req.MaxBuilds(int64(count - len(fetched))) |
| 74 | 74 |
| 75 res, err := search(c, client, req) | 75 res, err := search(c, client, req) |
| 76 switch { | 76 switch { |
| 77 case err != nil: | 77 case err != nil: |
| 78 return fetched, err | 78 return fetched, err |
| 79 case res.Error != nil: | 79 case res.Error != nil: |
| 80 return fetched, fmt.Errorf(res.Error.Message) | 80 return fetched, fmt.Errorf(res.Error.Message) |
| 81 } | 81 } |
| 82 | 82 |
| 83 fetched = append(fetched, res.Builds...) | 83 fetched = append(fetched, res.Builds...) |
| 84 | 84 |
| 85 if len(res.Builds) == 0 || res.NextCursor == "" { | 85 if len(res.Builds) == 0 || res.NextCursor == "" { |
| 86 break | 86 break |
| 87 } | 87 } |
| 88 req.StartCursor(res.NextCursor) | 88 req.StartCursor(res.NextCursor) |
| 89 } | 89 } |
| 90 logging.Debugf(c, "Fetched %d %s builds in %s", len(fetched), status, cl
ock.Since(c, start)) | 90 logging.Debugf(c, "Fetched %d %s builds in %s", len(fetched), status, cl
ock.Since(c, start)) |
| 91 return fetched, nil | 91 return fetched, nil |
| 92 } | 92 } |
| 93 | 93 |
| 94 // toMiloBuild converts a buildbucket build to a milo build. | 94 // toMiloBuild converts a buildbucket build to a milo build. |
| 95 // In case of an error, returns a build with a description of the error | 95 // In case of an error, returns a build with a description of the error |
| 96 // and logs the error. | 96 // and logs the error. |
| 97 func toMiloBuild(c context.Context, build *buildbucket.ApiBuildMessage) *resp.Bu
ildSummary { | 97 func toMiloBuild(c context.Context, build *buildbucket.ApiCommonBuildMessage) *r
esp.BuildSummary { |
| 98 // Parsing of parameters and result details is best effort. | 98 // Parsing of parameters and result details is best effort. |
| 99 var params buildParameters | 99 var params buildParameters |
| 100 if err := json.NewDecoder(strings.NewReader(build.ParametersJson)).Decod
e(¶ms); err != nil { | 100 if err := json.NewDecoder(strings.NewReader(build.ParametersJson)).Decod
e(¶ms); err != nil { |
| 101 logging.Errorf(c, "Could not parse parameters of build %d: %s",
build.Id, err) | 101 logging.Errorf(c, "Could not parse parameters of build %d: %s",
build.Id, err) |
| 102 } | 102 } |
| 103 var resultDetails resultDetails | 103 var resultDetails resultDetails |
| 104 if err := json.NewDecoder(strings.NewReader(build.ResultDetailsJson)).De
code(&resultDetails); err != nil { | 104 if err := json.NewDecoder(strings.NewReader(build.ResultDetailsJson)).De
code(&resultDetails); err != nil { |
| 105 logging.Errorf(c, "Could not parse result details of build %d: %
s", build.Id, err) | 105 logging.Errorf(c, "Could not parse result details of build %d: %
s", build.Id, err) |
| 106 } | 106 } |
| 107 | 107 |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 return time.Unix(microseconds/1e6, microseconds%1e6*1000).UTC() | 289 return time.Unix(microseconds/1e6, microseconds%1e6*1000).UTC() |
| 290 } | 290 } |
| 291 | 291 |
| 292 type newBuildsFirst []*resp.BuildSummary | 292 type newBuildsFirst []*resp.BuildSummary |
| 293 | 293 |
| 294 func (a newBuildsFirst) Len() int { return len(a) } | 294 func (a newBuildsFirst) Len() int { return len(a) } |
| 295 func (a newBuildsFirst) Swap(i, j int) { a[i], a[j] = a[j], a[i] } | 295 func (a newBuildsFirst) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
| 296 func (a newBuildsFirst) Less(i, j int) bool { | 296 func (a newBuildsFirst) Less(i, j int) bool { |
| 297 return a[i].PendingTime.Started.After(a[j].PendingTime.Started) | 297 return a[i].PendingTime.Started.After(a[j].PendingTime.Started) |
| 298 } | 298 } |
| OLD | NEW |