| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The LUCI Authors. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 | |
| 15 package console | |
| 16 | |
| 17 import ( | |
| 18 "fmt" | |
| 19 "net/http" | |
| 20 "strings" | |
| 21 | |
| 22 "golang.org/x/net/context" | |
| 23 | |
| 24 "github.com/luci/luci-go/common/api/gitiles" | |
| 25 "github.com/luci/luci-go/common/clock" | |
| 26 "github.com/luci/luci-go/common/logging" | |
| 27 "github.com/luci/luci-go/milo/api/config" | |
| 28 "github.com/luci/luci-go/milo/api/resp" | |
| 29 "github.com/luci/luci-go/milo/common" | |
| 30 "github.com/luci/luci-go/milo/common/model" | |
| 31 "github.com/luci/luci-go/server/router" | |
| 32 ) | |
| 33 | |
| 34 // Returns results of build[commit_index][builder_index] | |
| 35 func GetConsoleBuilds( | |
| 36 c context.Context, builders []resp.BuilderRef, commits []string) ( | |
| 37 [][]*model.BuildSummary, error) { | |
| 38 | |
| 39 panic("Nothing to see here, check back later.") | |
| 40 } | |
| 41 | |
| 42 // getConsoleDef finds the console definition as defined by any project. | |
| 43 // If the user is not a reader of the project, this will return a 404. | |
| 44 // TODO(hinoka): If the user is not a reader of any of of the builders returned, | |
| 45 // that builder will be removed from list of results. | |
| 46 func getConsoleDef(c context.Context, project, name string) (*config.Console, er
ror) { | |
| 47 cs, err := common.GetConsole(c, project, name) | |
| 48 if err != nil { | |
| 49 return nil, err | |
| 50 } | |
| 51 // TODO(hinoka): Remove builders that the user does not have access to. | |
| 52 return cs, nil | |
| 53 } | |
| 54 | |
| 55 // Main is a redirect handler that redirects the user to the main console for a | |
| 56 // particular project. | |
| 57 func Main(ctx *router.Context) { | |
| 58 w, r, p := ctx.Writer, ctx.Request, ctx.Params | |
| 59 proj := p.ByName("project") | |
| 60 http.Redirect(w, r, fmt.Sprintf("/console/%s/main", proj), http.StatusMo
vedPermanently) | |
| 61 return | |
| 62 } | |
| 63 | |
| 64 func summaryToConsole(bs []*model.BuildSummary) []*resp.ConsoleBuild { | |
| 65 cb := make([]*resp.ConsoleBuild, 0, len(bs)) | |
| 66 for _, b := range bs { | |
| 67 cb = append(cb, &resp.ConsoleBuild{ | |
| 68 // TODO(hinoka): This should link to the actual build. | |
| 69 Link: resp.NewLink(b.BuildKey.String(), "#"), | |
| 70 Status: b.Summary.Status, | |
| 71 }) | |
| 72 } | |
| 73 return cb | |
| 74 } | |
| 75 | |
| 76 func console(c context.Context, project, name string) (*resp.Console, error) { | |
| 77 tStart := clock.Now(c) | |
| 78 def, err := getConsoleDef(c, project, name) | |
| 79 if err != nil { | |
| 80 return nil, err | |
| 81 } | |
| 82 commits, err := getCommits(c, def.RepoURL, def.Branch, 25) | |
| 83 if err != nil { | |
| 84 return nil, err | |
| 85 } | |
| 86 tGitiles := clock.Now(c) | |
| 87 logging.Debugf(c, "Loading commits took %s.", tGitiles.Sub(tStart)) | |
| 88 commitNames := make([]string, len(commits)) | |
| 89 commitLinks := make([]*resp.Link, len(commits)) | |
| 90 for i, commit := range commits { | |
| 91 commitNames[i] = commit.Revision.Label | |
| 92 commitLinks[i] = commit.Revision | |
| 93 } | |
| 94 | |
| 95 // HACK(hinoka): This only supports buildbot.... | |
| 96 builders := make([]resp.BuilderRef, len(def.Builders)) | |
| 97 for i, b := range def.Builders { | |
| 98 builders[i] = resp.BuilderRef{ | |
| 99 b.Name, strings.Split(b.Category, "|"), b.ShortName, | |
| 100 } | |
| 101 } | |
| 102 cb, err := GetConsoleBuilds(c, builders, commitNames) | |
| 103 tConsole := clock.Now(c) | |
| 104 logging.Debugf(c, "Loading the console took a total of %s.", tConsole.Su
b(tGitiles)) | |
| 105 if err != nil { | |
| 106 return nil, err | |
| 107 } | |
| 108 ccb := make([]resp.CommitBuild, len(commits)) | |
| 109 for i, commit := range commitLinks { | |
| 110 // TODO(hinoka): Not like this | |
| 111 ccb[i].Commit = resp.Commit{Revision: commit} | |
| 112 ccb[i].Build = summaryToConsole(cb[i]) | |
| 113 } | |
| 114 | |
| 115 cs := &resp.Console{ | |
| 116 Name: def.Name, | |
| 117 Commit: ccb, | |
| 118 BuilderRef: builders, | |
| 119 } | |
| 120 | |
| 121 return cs, nil | |
| 122 } | |
| 123 | |
| 124 func getCommits(c context.Context, repoURL, treeish string, limit int) ([]resp.C
ommit, error) { | |
| 125 commits, err := gitiles.Log(c, repoURL, treeish, limit) | |
| 126 if err != nil { | |
| 127 return nil, err | |
| 128 } | |
| 129 result := make([]resp.Commit, len(commits)) | |
| 130 for i, log := range commits { | |
| 131 result[i] = resp.Commit{ | |
| 132 AuthorName: log.Author.Name, | |
| 133 AuthorEmail: log.Author.Email, | |
| 134 Repo: repoURL, | |
| 135 Revision: resp.NewLink(log.Commit, repoURL+"/+/"+log.
Commit), | |
| 136 Description: log.Message, | |
| 137 Title: strings.SplitN(log.Message, "\n", 2)[0], | |
| 138 // TODO(hinoka): Fill in the rest of resp.Commit and add
those details | |
| 139 // in the html. | |
| 140 } | |
| 141 } | |
| 142 return result, nil | |
| 143 } | |
| OLD | NEW |