1
0
mirror of https://github.com/golang/go synced 2024-11-18 19:34:41 -07:00

x/tools/dashboard/app: use ?branch=foo to show foo branch commits

LGTM=rsc
R=bradfitz, rsc
CC=golang-codereviews
https://golang.org/cl/167660043
This commit is contained in:
Andrew Gerrand 2014-11-13 21:53:13 +11:00
parent c2b324b9b4
commit c05aea77a9
2 changed files with 65 additions and 26 deletions

View File

@ -12,6 +12,7 @@ package build
import ( import (
"bytes" "bytes"
"errors" "errors"
"fmt"
"html/template" "html/template"
"net/http" "net/http"
"regexp" "regexp"
@ -36,22 +37,28 @@ func uiHandler(w http.ResponseWriter, r *http.Request) {
d := dashboardForRequest(r) d := dashboardForRequest(r)
c := d.Context(appengine.NewContext(r)) c := d.Context(appengine.NewContext(r))
now := cache.Now(c) now := cache.Now(c)
const key = "build-ui" key := "build-ui"
page, _ := strconv.Atoi(r.FormValue("page")) page, _ := strconv.Atoi(r.FormValue("page"))
if page < 0 { if page < 0 {
page = 0 page = 0
} }
repo := r.FormValue("repo") key += fmt.Sprintf("-page%v", page)
useCache := page == 0 && repo == ""
// Used cached version of front page, if available. branch := r.FormValue("branch")
if useCache { if branch != "" {
var b []byte key += "-branch-" + branch
if cache.Get(r, now, key, &b) { }
w.Write(b)
return repo := r.FormValue("repo")
} if repo != "" {
key += "-repo-" + repo
}
var b []byte
if cache.Get(r, now, key, &b) {
w.Write(b)
return
} }
pkg := &Package{} // empty package is the main repository pkg := &Package{} // empty package is the main repository
@ -63,7 +70,7 @@ func uiHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
} }
commits, err := dashCommits(c, pkg, page) commits, err := dashCommits(c, pkg, page, branch)
if err != nil { if err != nil {
logErr(w, r, err) logErr(w, r, err)
return return
@ -71,7 +78,7 @@ func uiHandler(w http.ResponseWriter, r *http.Request) {
builders := commitBuilders(commits) builders := commitBuilders(commits)
var tipState *TagState var tipState *TagState
if pkg.Kind == "" && page == 0 && commits != nil { if pkg.Kind == "" && page == 0 && (branch == "" || branch == "default") {
// only show sub-repo state on first page of normal repo view // only show sub-repo state on first page of normal repo view
tipState, err = TagStateByName(c, "tip") tipState, err = TagStateByName(c, "tip")
if err != nil { if err != nil {
@ -88,7 +95,7 @@ func uiHandler(w http.ResponseWriter, r *http.Request) {
p.Prev = page - 1 p.Prev = page - 1
p.HasPrev = true p.HasPrev = true
} }
data := &uiTemplateData{d, pkg, commits, builders, tipState, p} data := &uiTemplateData{d, pkg, commits, builders, tipState, p, branch}
var buf bytes.Buffer var buf bytes.Buffer
if err := uiTemplate.Execute(&buf, data); err != nil { if err := uiTemplate.Execute(&buf, data); err != nil {
@ -96,10 +103,7 @@ func uiHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
// Cache the front page. cache.Set(r, now, key, buf.Bytes())
if useCache {
cache.Set(r, now, key, buf.Bytes())
}
buf.WriteTo(w) buf.WriteTo(w)
} }
@ -111,15 +115,49 @@ type Pagination struct {
// dashCommits gets a slice of the latest Commits to the current dashboard. // dashCommits gets a slice of the latest Commits to the current dashboard.
// If page > 0 it paginates by commitsPerPage. // If page > 0 it paginates by commitsPerPage.
func dashCommits(c appengine.Context, pkg *Package, page int) ([]*Commit, error) { func dashCommits(c appengine.Context, pkg *Package, page int, branch string) ([]*Commit, error) {
offset := page * commitsPerPage
q := datastore.NewQuery("Commit"). q := datastore.NewQuery("Commit").
Ancestor(pkg.Key(c)). Ancestor(pkg.Key(c)).
Order("-Num"). Order("-Num")
Limit(commitsPerPage).
Offset(page * commitsPerPage)
var commits []*Commit var commits []*Commit
_, err := q.GetAll(c, &commits) if branch == "" {
return commits, err _, err := q.Limit(commitsPerPage).Offset(offset).
GetAll(c, &commits)
return commits, err
}
// Look for commits on a specific branch.
for t, n := q.Run(c), 0; len(commits) < commitsPerPage && n < 1000; {
var c Commit
_, err := t.Next(&c)
if err == datastore.Done {
break
}
if err != nil {
return nil, err
}
if !isBranchCommit(&c, branch) {
continue
}
if n >= offset {
commits = append(commits, &c)
}
n++
}
return commits, nil
}
// isBranchCommit reports whether the given commit is on the specified branch.
// It does so by examining the commit description, so there will be some bad
// matches where the branch commits do not begin with the "[branch]" prefix.
func isBranchCommit(c *Commit, b string) bool {
d := strings.TrimSpace(c.Desc)
if b == "default" {
return !strings.HasPrefix(d, "[")
}
return strings.HasPrefix(d, "["+b+"]")
} }
// commitBuilders returns the names of the builders that provided // commitBuilders returns the names of the builders that provided
@ -251,6 +289,7 @@ type uiTemplateData struct {
Builders []string Builders []string
TipState *TagState TipState *TagState
Pagination *Pagination Pagination *Pagination
Branch string
} }
var uiTemplate = template.Must( var uiTemplate = template.Must(

View File

@ -123,9 +123,9 @@
{{with $.Pagination}} {{with $.Pagination}}
<div class="paginate"> <div class="paginate">
<nav> <nav>
<a {{if .HasPrev}}href="?{{with $.Package.Path}}repo={{.}}&{{end}}page={{.Prev}}"{{else}}class="inactive"{{end}}>newer</a> <a {{if .HasPrev}}href="?{{with $.Package.Path}}repo={{.}}&{{end}}page={{.Prev}}{{with $.Branch}}&branch={{.}}{{end}}"{{else}}class="inactive"{{end}}>newer</a>
<a {{if .Next}}href="?{{with $.Package.Path}}repo={{.}}&{{end}}page={{.Next}}"{{else}}class="inactive"{{end}}>older</a> <a {{if .Next}}href="?{{with $.Package.Path}}repo={{.}}&{{end}}page={{.Next}}{{with $.Branch}}&branch={{.}}{{end}}"{{else}}class="inactive"{{end}}>older</a>
<a {{if .HasPrev}}href="."{{else}}class="inactive"{{end}}>latest</a> <a {{if .HasPrev}}href=".{{with $.Branch}}?branch={{.}}{{end}}"{{else}}class="inactive"{{end}}>latest</a>
</nav> </nav>
</div> </div>
{{end}} {{end}}