| 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 console | 5 package console |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "net/http" | 9 "net/http" |
| 10 "strings" | 10 "strings" |
| 11 | 11 |
| 12 "github.com/luci/luci-go/common/clock" | 12 "github.com/luci/luci-go/common/clock" |
| 13 » log "github.com/luci/luci-go/common/logging" | 13 » "github.com/luci/luci-go/common/logging" |
| 14 "github.com/luci/luci-go/milo/api/resp" | 14 "github.com/luci/luci-go/milo/api/resp" |
| 15 "github.com/luci/luci-go/milo/appengine/backend/git" | 15 "github.com/luci/luci-go/milo/appengine/backend/git" |
| 16 "github.com/luci/luci-go/milo/appengine/buildbot" | 16 "github.com/luci/luci-go/milo/appengine/buildbot" |
| 17 » "github.com/luci/luci-go/milo/appengine/settings" | 17 » "github.com/luci/luci-go/milo/appengine/common" |
| 18 "github.com/luci/luci-go/milo/common/config" | 18 "github.com/luci/luci-go/milo/common/config" |
| 19 "github.com/luci/luci-go/server/router" | 19 "github.com/luci/luci-go/server/router" |
| 20 "golang.org/x/net/context" | 20 "golang.org/x/net/context" |
| 21 ) | 21 ) |
| 22 | 22 |
| 23 // Returns results of build[commit_index][builder_index] | 23 // Returns results of build[commit_index][builder_index] |
| 24 func GetConsoleBuilds( | 24 func GetConsoleBuilds( |
| 25 c context.Context, module string, | 25 c context.Context, module string, |
| 26 builders []resp.BuilderRef, commits []string) ( | 26 builders []resp.BuilderRef, commits []string) ( |
| 27 [][]*resp.ConsoleBuild, error) { | 27 [][]*resp.ConsoleBuild, error) { |
| 28 | 28 |
| 29 switch module { | 29 switch module { |
| 30 case "buildbot": | 30 case "buildbot": |
| 31 return buildbot.GetConsoleBuilds(c, builders, commits) | 31 return buildbot.GetConsoleBuilds(c, builders, commits) |
| 32 // The case for buildbucket and dm goes here. | 32 // The case for buildbucket and dm goes here. |
| 33 default: | 33 default: |
| 34 panic(fmt.Errorf("Unrecognized module %s", module)) | 34 panic(fmt.Errorf("Unrecognized module %s", module)) |
| 35 } | 35 } |
| 36 } | 36 } |
| 37 | 37 |
| 38 // getConsoleDef finds the console definition as defined by any project. | 38 // getConsoleDef finds the console definition as defined by any project. |
| 39 // If the user is not a reader of the project, this will return a 404. | 39 // If the user is not a reader of the project, this will return a 404. |
| 40 // TODO(hinoka): If the user is not a reader of any of of the builders returned, | 40 // TODO(hinoka): If the user is not a reader of any of of the builders returned, |
| 41 // that builder will be removed from list of results. | 41 // that builder will be removed from list of results. |
| 42 func getConsoleDef(c context.Context, project, name string) (*config.Console, er
ror) { | 42 func getConsoleDef(c context.Context, project, name string) (*config.Console, er
ror) { |
| 43 » cs, err := settings.GetConsole(c, project, name) | 43 » cs, err := common.GetConsole(c, project, name) |
| 44 if err != nil { | 44 if err != nil { |
| 45 return nil, err | 45 return nil, err |
| 46 } | 46 } |
| 47 // TODO(hinoka): Remove builders that the user does not have access to. | 47 // TODO(hinoka): Remove builders that the user does not have access to. |
| 48 return cs, nil | 48 return cs, nil |
| 49 } | 49 } |
| 50 | 50 |
| 51 // Main is a redirect handler that redirects the user to the main console for a | 51 // Main is a redirect handler that redirects the user to the main console for a |
| 52 // particular project. | 52 // particular project. |
| 53 func Main(ctx *router.Context) { | 53 func Main(ctx *router.Context) { |
| 54 w, r, p := ctx.Writer, ctx.Request, ctx.Params | 54 w, r, p := ctx.Writer, ctx.Request, ctx.Params |
| 55 proj := p.ByName("project") | 55 proj := p.ByName("project") |
| 56 http.Redirect(w, r, fmt.Sprintf("/console/%s/main", proj), http.StatusMo
vedPermanently) | 56 http.Redirect(w, r, fmt.Sprintf("/console/%s/main", proj), http.StatusMo
vedPermanently) |
| 57 return | 57 return |
| 58 } | 58 } |
| 59 | 59 |
| 60 func console(c context.Context, project, name string) (*resp.Console, error) { | 60 func console(c context.Context, project, name string) (*resp.Console, error) { |
| 61 tStart := clock.Now(c) | 61 tStart := clock.Now(c) |
| 62 def, err := getConsoleDef(c, project, name) | 62 def, err := getConsoleDef(c, project, name) |
| 63 if err != nil { | 63 if err != nil { |
| 64 return nil, err | 64 return nil, err |
| 65 } | 65 } |
| 66 commits, err := git.GetCommits(c, def.RepoURL, def.Branch, 25) | 66 commits, err := git.GetCommits(c, def.RepoURL, def.Branch, 25) |
| 67 if err != nil { | 67 if err != nil { |
| 68 return nil, err | 68 return nil, err |
| 69 } | 69 } |
| 70 tGitiles := clock.Now(c) | 70 tGitiles := clock.Now(c) |
| 71 » log.Debugf(c, "Loading commits took %s.", tGitiles.Sub(tStart)) | 71 » logging.Debugf(c, "Loading commits took %s.", tGitiles.Sub(tStart)) |
| 72 commitNames := make([]string, len(commits)) | 72 commitNames := make([]string, len(commits)) |
| 73 for i, commit := range commits { | 73 for i, commit := range commits { |
| 74 commitNames[i] = commit.Revision | 74 commitNames[i] = commit.Revision |
| 75 } | 75 } |
| 76 | 76 |
| 77 // HACK(hinoka): This only supports buildbot.... | 77 // HACK(hinoka): This only supports buildbot.... |
| 78 builders := make([]resp.BuilderRef, len(def.Builders)) | 78 builders := make([]resp.BuilderRef, len(def.Builders)) |
| 79 for i, b := range def.Builders { | 79 for i, b := range def.Builders { |
| 80 builders[i] = resp.BuilderRef{ | 80 builders[i] = resp.BuilderRef{ |
| 81 b.Module, b.Name, strings.Split(b.Category, "|"), b.Shor
tName, | 81 b.Module, b.Name, strings.Split(b.Category, "|"), b.Shor
tName, |
| 82 } | 82 } |
| 83 } | 83 } |
| 84 cb, err := GetConsoleBuilds(c, "buildbot", builders, commitNames) | 84 cb, err := GetConsoleBuilds(c, "buildbot", builders, commitNames) |
| 85 tConsole := clock.Now(c) | 85 tConsole := clock.Now(c) |
| 86 » log.Debugf(c, "Loading the console took a total of %s.", tConsole.Sub(tG
itiles)) | 86 » logging.Debugf(c, "Loading the console took a total of %s.", tConsole.Su
b(tGitiles)) |
| 87 if err != nil { | 87 if err != nil { |
| 88 return nil, err | 88 return nil, err |
| 89 } | 89 } |
| 90 ccb := make([]resp.CommitBuild, len(commits)) | 90 ccb := make([]resp.CommitBuild, len(commits)) |
| 91 for i, commit := range commitNames { | 91 for i, commit := range commitNames { |
| 92 // TODO(hinoka): Not like this | 92 // TODO(hinoka): Not like this |
| 93 ccb[i].Commit = resp.Commit{Revision: commit} | 93 ccb[i].Commit = resp.Commit{Revision: commit} |
| 94 ccb[i].Build = cb[i] | 94 ccb[i].Build = cb[i] |
| 95 } | 95 } |
| 96 | 96 |
| 97 cs := &resp.Console{ | 97 cs := &resp.Console{ |
| 98 Name: def.Name, | 98 Name: def.Name, |
| 99 Commit: ccb, | 99 Commit: ccb, |
| 100 BuilderRef: builders, | 100 BuilderRef: builders, |
| 101 } | 101 } |
| 102 | 102 |
| 103 return cs, nil | 103 return cs, nil |
| 104 } | 104 } |
| OLD | NEW |