| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | |
| 3 // that can be found in the LICENSE file. | |
| 4 | |
| 5 package console | |
| 6 | |
| 7 import ( | |
| 8 "fmt" | |
| 9 "net/http" | |
| 10 "strings" | |
| 11 | |
| 12 "golang.org/x/net/context" | |
| 13 | |
| 14 "github.com/luci/luci-go/common/clock" | |
| 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" | |
| 19 "github.com/luci/luci-go/milo/api/resp" | |
| 20 "github.com/luci/luci-go/milo/appengine/common" | |
| 21 "github.com/luci/luci-go/milo/appengine/common/gitiles" | |
| 22 "github.com/luci/luci-go/milo/appengine/job_source/buildbot" | |
| 23 ) | |
| 24 | |
| 25 // Returns results of build[commit_index][builder_index] | |
| 26 func GetConsoleBuilds( | |
| 27 c context.Context, module string, | |
| 28 builders []resp.BuilderRef, commits []string) ( | |
| 29 [][]*resp.ConsoleBuild, error) { | |
| 30 | |
| 31 switch module { | |
| 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 } | |
| 39 | |
| 40 // 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. | |
| 42 // 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. | |
| 44 func getConsoleDef(c context.Context, project, name string) (*config.Console, er
ror) { | |
| 45 cs, err := common.GetConsole(c, project, name) | |
| 46 if err != nil { | |
| 47 return nil, err | |
| 48 } | |
| 49 // TODO(hinoka): Remove builders that the user does not have access to. | |
| 50 return cs, nil | |
| 51 } | |
| 52 | |
| 53 // Main is a redirect handler that redirects the user to the main console for a | |
| 54 // particular project. | |
| 55 func Main(ctx *router.Context) { | |
| 56 w, r, p := ctx.Writer, ctx.Request, ctx.Params | |
| 57 proj := p.ByName("project") | |
| 58 http.Redirect(w, r, fmt.Sprintf("/console/%s/main", proj), http.StatusMo
vedPermanently) | |
| 59 return | |
| 60 } | |
| 61 | |
| 62 func console(c context.Context, project, name string) (*resp.Console, error) { | |
| 63 tStart := clock.Now(c) | |
| 64 def, err := getConsoleDef(c, project, name) | |
| 65 if err != nil { | |
| 66 return nil, err | |
| 67 } | |
| 68 commits, err := gitiles.GetCommits(c, def.RepoURL, def.Branch, 25) | |
| 69 if err != nil { | |
| 70 return nil, err | |
| 71 } | |
| 72 tGitiles := clock.Now(c) | |
| 73 logging.Debugf(c, "Loading commits took %s.", tGitiles.Sub(tStart)) | |
| 74 commitNames := make([]string, len(commits)) | |
| 75 commitLinks := make([]*resp.Link, len(commits)) | |
| 76 for i, commit := range commits { | |
| 77 commitNames[i] = commit.Revision.Label | |
| 78 commitLinks[i] = commit.Revision | |
| 79 } | |
| 80 | |
| 81 // HACK(hinoka): This only supports buildbot.... | |
| 82 builders := make([]resp.BuilderRef, len(def.Builders)) | |
| 83 for i, b := range def.Builders { | |
| 84 builders[i] = resp.BuilderRef{ | |
| 85 b.Module, b.Name, strings.Split(b.Category, "|"), b.Shor
tName, | |
| 86 } | |
| 87 } | |
| 88 cb, err := GetConsoleBuilds(c, "buildbot", builders, commitNames) | |
| 89 tConsole := clock.Now(c) | |
| 90 logging.Debugf(c, "Loading the console took a total of %s.", tConsole.Su
b(tGitiles)) | |
| 91 if err != nil { | |
| 92 return nil, err | |
| 93 } | |
| 94 ccb := make([]resp.CommitBuild, len(commits)) | |
| 95 for i, commit := range commitLinks { | |
| 96 // TODO(hinoka): Not like this | |
| 97 ccb[i].Commit = resp.Commit{Revision: commit} | |
| 98 ccb[i].Build = cb[i] | |
| 99 } | |
| 100 | |
| 101 cs := &resp.Console{ | |
| 102 Name: def.Name, | |
| 103 Commit: ccb, | |
| 104 BuilderRef: builders, | |
| 105 } | |
| 106 | |
| 107 return cs, nil | |
| 108 } | |
| OLD | NEW |