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