| 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 buildbot | 5 package buildbot |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "encoding/json" | 8 "encoding/json" |
| 9 "fmt" | 9 "fmt" |
| 10 "io/ioutil" | 10 "io/ioutil" |
| 11 "net/http" | 11 "net/http" |
| 12 "path/filepath" | 12 "path/filepath" |
| 13 "sort" | 13 "sort" |
| 14 "strconv" | |
| 15 "strings" | 14 "strings" |
| 16 "time" | 15 "time" |
| 17 | 16 |
| 18 ds "github.com/luci/gae/service/datastore" | 17 ds "github.com/luci/gae/service/datastore" |
| 19 "github.com/luci/luci-go/common/logging" | 18 "github.com/luci/luci-go/common/logging" |
| 20 "github.com/luci/luci-go/milo/api/resp" | 19 "github.com/luci/luci-go/milo/api/resp" |
| 21 "github.com/luci/luci-go/milo/appengine/settings" | 20 "github.com/luci/luci-go/milo/appengine/settings" |
| 22 "github.com/luci/luci-go/milo/common/miloerror" | 21 "github.com/luci/luci-go/milo/common/miloerror" |
| 23 "golang.org/x/net/context" | 22 "golang.org/x/net/context" |
| 24 ) | 23 ) |
| 25 | 24 |
| 26 var errBuildNotFound = miloerror.Error{ | 25 var errBuildNotFound = miloerror.Error{ |
| 27 Message: "Build not found", | 26 Message: "Build not found", |
| 28 Code: http.StatusNotFound, | 27 Code: http.StatusNotFound, |
| 29 } | 28 } |
| 30 | 29 |
| 31 // getBuild fetches a buildbot build from the datastore and checks ACLs. | 30 // getBuild fetches a buildbot build from the datastore and checks ACLs. |
| 32 func getBuild(c context.Context, master, builder, buildNum string) (*buildbotBui
ld, error) { | 31 func getBuild(c context.Context, master, builder string, buildNum int) (*buildbo
tBuild, error) { |
| 33 result := &buildbotBuild{ | 32 result := &buildbotBuild{ |
| 34 Master: master, | 33 Master: master, |
| 35 Buildername: builder, | 34 Buildername: builder, |
| 35 Number: buildNum, |
| 36 } | 36 } |
| 37 | 37 |
| 38 » num, err := strconv.Atoi(buildNum) | 38 » err := ds.Get(c, result) |
| 39 » if err != nil { | |
| 40 » » return nil, miloerror.Error{ | |
| 41 » » » Message: fmt.Sprintf("%s does not look like a number", b
uildNum), | |
| 42 » » » Code: http.StatusBadRequest, | |
| 43 » » } | |
| 44 » } | |
| 45 » result.Number = num | |
| 46 | |
| 47 » err = ds.Get(c, result) | |
| 48 switch { | 39 switch { |
| 49 case err == ds.ErrNoSuchEntity: | 40 case err == ds.ErrNoSuchEntity: |
| 50 return nil, errBuildNotFound | 41 return nil, errBuildNotFound |
| 51 case err != nil: | 42 case err != nil: |
| 52 return nil, err | 43 return nil, err |
| 53 } | 44 } |
| 54 if result.Internal { | 45 if result.Internal { |
| 55 allowed, err := settings.IsAllowedInternal(c) | 46 allowed, err := settings.IsAllowedInternal(c) |
| 56 if err != nil { | 47 if err != nil { |
| 57 return nil, err | 48 return nil, err |
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 Label: fmt.Sprintf("Issue %d", issue), | 392 Label: fmt.Sprintf("Issue %d", issue), |
| 402 URL: fmt.Sprintf("%s/%d", rietveld, issue), | 393 URL: fmt.Sprintf("%s/%d", rietveld, issue), |
| 403 } | 394 } |
| 404 } else { | 395 } else { |
| 405 logging.Warningf(c, "Found issue but not rietveld proper
ty.") | 396 logging.Warningf(c, "Found issue but not rietveld proper
ty.") |
| 406 } | 397 } |
| 407 } | 398 } |
| 408 return ss | 399 return ss |
| 409 } | 400 } |
| 410 | 401 |
| 411 func getDebugBuild(c context.Context, builder, buildNum string) (*buildbotBuild,
error) { | 402 func getDebugBuild(c context.Context, builder string, buildNum int) (*buildbotBu
ild, error) { |
| 412 » fname := fmt.Sprintf("%s.%s.json", builder, buildNum) | 403 » fname := fmt.Sprintf("%s.%d.json", builder, buildNum) |
| 413 // ../buildbot below assumes that | 404 // ../buildbot below assumes that |
| 414 // - this code is not executed by tests outside of this dir | 405 // - this code is not executed by tests outside of this dir |
| 415 // - this dir is a sibling of frontend dir | 406 // - this dir is a sibling of frontend dir |
| 416 path := filepath.Join("..", "buildbot", "testdata", fname) | 407 path := filepath.Join("..", "buildbot", "testdata", fname) |
| 417 raw, err := ioutil.ReadFile(path) | 408 raw, err := ioutil.ReadFile(path) |
| 418 if err != nil { | 409 if err != nil { |
| 419 return nil, err | 410 return nil, err |
| 420 } | 411 } |
| 421 b := &buildbotBuild{} | 412 b := &buildbotBuild{} |
| 422 return b, json.Unmarshal(raw, b) | 413 return b, json.Unmarshal(raw, b) |
| 423 } | 414 } |
| 424 | 415 |
| 425 // build fetches a buildbot build and translates it into a miloBuild. | 416 // build fetches a buildbot build and translates it into a miloBuild. |
| 426 func build(c context.Context, master, builder, buildNum string) (*resp.MiloBuild
, error) { | 417 func build(c context.Context, master, builder string, buildNum int) (*resp.MiloB
uild, error) { |
| 427 var b *buildbotBuild | 418 var b *buildbotBuild |
| 428 var err error | 419 var err error |
| 429 if master == "debug" { | 420 if master == "debug" { |
| 430 b, err = getDebugBuild(c, builder, buildNum) | 421 b, err = getDebugBuild(c, builder, buildNum) |
| 431 } else { | 422 } else { |
| 432 b, err = getBuild(c, master, builder, buildNum) | 423 b, err = getBuild(c, master, builder, buildNum) |
| 433 } | 424 } |
| 434 if err != nil { | 425 if err != nil { |
| 435 return nil, err | 426 return nil, err |
| 436 } | 427 } |
| 437 | 428 |
| 438 // TODO(hinoka): Do all fields concurrently. | 429 // TODO(hinoka): Do all fields concurrently. |
| 439 return &resp.MiloBuild{ | 430 return &resp.MiloBuild{ |
| 440 SourceStamp: sourcestamp(c, b), | 431 SourceStamp: sourcestamp(c, b), |
| 441 Summary: summary(c, b), | 432 Summary: summary(c, b), |
| 442 Components: components(b), | 433 Components: components(b), |
| 443 PropertyGroup: properties(b), | 434 PropertyGroup: properties(b), |
| 444 Blame: blame(b), | 435 Blame: blame(b), |
| 445 }, nil | 436 }, nil |
| 446 } | 437 } |
| OLD | NEW |