2011-11-22 14:13:05 -07:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
2011-11-24 18:53:05 -07:00
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
|
|
|
"crypto/sha1"
|
2012-02-05 15:26:32 -07:00
|
|
|
"errors"
|
2011-11-24 18:53:05 -07:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2011-12-20 19:16:47 -07:00
|
|
|
"io/ioutil"
|
2011-11-24 18:53:05 -07:00
|
|
|
"strings"
|
2012-02-05 15:26:32 -07:00
|
|
|
"time"
|
2011-12-22 20:44:56 -07:00
|
|
|
|
|
|
|
"appengine"
|
|
|
|
"appengine/datastore"
|
2011-11-22 14:13:05 -07:00
|
|
|
)
|
|
|
|
|
2011-12-20 20:07:32 -07:00
|
|
|
const maxDatastoreStringLen = 500
|
2011-11-24 18:53:05 -07:00
|
|
|
|
2011-11-22 14:13:05 -07:00
|
|
|
// A Package describes a package that is listed on the dashboard.
|
|
|
|
type Package struct {
|
2012-01-29 17:59:06 -07:00
|
|
|
Kind string // "subrepo", "external", or empty for the main Go tree
|
2011-11-24 18:53:05 -07:00
|
|
|
Name string
|
|
|
|
Path string // (empty for the main Go tree)
|
|
|
|
NextNum int // Num of the next head Commit
|
2011-11-22 14:13:05 -07:00
|
|
|
}
|
|
|
|
|
2011-12-15 16:48:06 -07:00
|
|
|
func (p *Package) String() string {
|
|
|
|
return fmt.Sprintf("%s: %q", p.Path, p.Name)
|
|
|
|
}
|
|
|
|
|
2011-11-22 14:13:05 -07:00
|
|
|
func (p *Package) Key(c appengine.Context) *datastore.Key {
|
|
|
|
key := p.Path
|
|
|
|
if key == "" {
|
|
|
|
key = "go"
|
|
|
|
}
|
|
|
|
return datastore.NewKey(c, "Package", key, 0, nil)
|
|
|
|
}
|
|
|
|
|
2011-12-15 16:48:06 -07:00
|
|
|
// LastCommit returns the most recent Commit for this Package.
|
2012-02-05 15:26:32 -07:00
|
|
|
func (p *Package) LastCommit(c appengine.Context) (*Commit, error) {
|
2011-12-15 16:48:06 -07:00
|
|
|
var commits []*Commit
|
|
|
|
_, err := datastore.NewQuery("Commit").
|
|
|
|
Ancestor(p.Key(c)).
|
|
|
|
Order("-Time").
|
|
|
|
Limit(1).
|
|
|
|
GetAll(c, &commits)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(commits) != 1 {
|
|
|
|
return nil, datastore.ErrNoSuchEntity
|
|
|
|
}
|
|
|
|
return commits[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPackage fetches a Package by path from the datastore.
|
2012-02-05 15:26:32 -07:00
|
|
|
func GetPackage(c appengine.Context, path string) (*Package, error) {
|
2011-11-24 18:53:05 -07:00
|
|
|
p := &Package{Path: path}
|
|
|
|
err := datastore.Get(c, p.Key(c), p)
|
|
|
|
if err == datastore.ErrNoSuchEntity {
|
|
|
|
return nil, fmt.Errorf("package %q not found", path)
|
|
|
|
}
|
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
2011-11-22 14:13:05 -07:00
|
|
|
// A Commit describes an individual commit in a package.
|
|
|
|
//
|
|
|
|
// Each Commit entity is a descendant of its associated Package entity.
|
|
|
|
// In other words, all Commits with the same PackagePath belong to the same
|
|
|
|
// datastore entity group.
|
|
|
|
type Commit struct {
|
|
|
|
PackagePath string // (empty for Go commits)
|
|
|
|
Hash string
|
|
|
|
ParentHash string
|
2011-11-24 18:53:05 -07:00
|
|
|
Num int // Internal monotonic counter unique to this package.
|
2011-11-22 14:13:05 -07:00
|
|
|
|
|
|
|
User string
|
|
|
|
Desc string `datastore:",noindex"`
|
2012-02-05 15:26:32 -07:00
|
|
|
Time time.Time
|
2011-11-22 14:13:05 -07:00
|
|
|
|
2011-12-15 16:48:06 -07:00
|
|
|
// ResultData is the Data string of each build Result for this Commit.
|
2011-11-22 14:13:05 -07:00
|
|
|
// For non-Go commits, only the Results for the current Go tip, weekly,
|
|
|
|
// and release Tags are stored here. This is purely de-normalized data.
|
|
|
|
// The complete data set is stored in Result entities.
|
2011-12-15 16:48:06 -07:00
|
|
|
ResultData []string `datastore:",noindex"`
|
2011-12-20 19:16:47 -07:00
|
|
|
|
|
|
|
FailNotificationSent bool
|
2011-11-22 14:13:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (com *Commit) Key(c appengine.Context) *datastore.Key {
|
2011-11-24 18:53:05 -07:00
|
|
|
if com.Hash == "" {
|
|
|
|
panic("tried Key on Commit with empty Hash")
|
|
|
|
}
|
|
|
|
p := Package{Path: com.PackagePath}
|
|
|
|
key := com.PackagePath + "|" + com.Hash
|
|
|
|
return datastore.NewKey(c, "Commit", key, 0, p.Key(c))
|
|
|
|
}
|
|
|
|
|
2012-02-05 15:26:32 -07:00
|
|
|
func (c *Commit) Valid() error {
|
2011-11-24 18:53:05 -07:00
|
|
|
if !validHash(c.Hash) {
|
2012-02-05 15:26:32 -07:00
|
|
|
return errors.New("invalid Hash")
|
2011-11-24 18:53:05 -07:00
|
|
|
}
|
2011-12-04 22:22:14 -07:00
|
|
|
if c.ParentHash != "" && !validHash(c.ParentHash) { // empty is OK
|
2012-02-05 15:26:32 -07:00
|
|
|
return errors.New("invalid ParentHash")
|
2011-11-24 18:53:05 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddResult adds the denormalized Reuslt data to the Commit's Result field.
|
|
|
|
// It must be called from inside a datastore transaction.
|
2012-02-05 15:26:32 -07:00
|
|
|
func (com *Commit) AddResult(c appengine.Context, r *Result) error {
|
2011-11-24 18:53:05 -07:00
|
|
|
if err := datastore.Get(c, com.Key(c), com); err != nil {
|
2011-12-20 17:08:47 -07:00
|
|
|
return fmt.Errorf("getting Commit: %v", err)
|
2011-11-24 18:53:05 -07:00
|
|
|
}
|
2011-12-15 16:48:06 -07:00
|
|
|
com.ResultData = append(com.ResultData, r.Data())
|
2011-12-20 17:08:47 -07:00
|
|
|
if _, err := datastore.Put(c, com.Key(c), com); err != nil {
|
|
|
|
return fmt.Errorf("putting Commit: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
2011-11-24 18:53:05 -07:00
|
|
|
}
|
|
|
|
|
2011-12-15 16:48:06 -07:00
|
|
|
// Result returns the build Result for this Commit for the given builder/goHash.
|
|
|
|
func (c *Commit) Result(builder, goHash string) *Result {
|
|
|
|
for _, r := range c.ResultData {
|
|
|
|
p := strings.SplitN(r, "|", 4)
|
|
|
|
if len(p) != 4 || p[0] != builder || p[3] != goHash {
|
|
|
|
continue
|
2011-11-24 18:53:05 -07:00
|
|
|
}
|
2011-12-15 16:48:06 -07:00
|
|
|
return partsToHash(c, p)
|
2011-11-24 18:53:05 -07:00
|
|
|
}
|
2011-12-15 16:48:06 -07:00
|
|
|
return nil
|
2011-11-22 14:13:05 -07:00
|
|
|
}
|
|
|
|
|
2011-12-15 16:48:06 -07:00
|
|
|
// Results returns the build Results for this Commit for the given goHash.
|
|
|
|
func (c *Commit) Results(goHash string) (results []*Result) {
|
|
|
|
for _, r := range c.ResultData {
|
2011-11-29 01:24:57 -07:00
|
|
|
p := strings.SplitN(r, "|", 4)
|
2011-12-15 16:48:06 -07:00
|
|
|
if len(p) != 4 || p[3] != goHash {
|
|
|
|
continue
|
2011-11-29 01:24:57 -07:00
|
|
|
}
|
2011-12-15 16:48:06 -07:00
|
|
|
results = append(results, partsToHash(c, p))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// partsToHash converts a Commit and ResultData substrings to a Result.
|
|
|
|
func partsToHash(c *Commit, p []string) *Result {
|
|
|
|
return &Result{
|
|
|
|
Builder: p[0],
|
|
|
|
Hash: c.Hash,
|
|
|
|
PackagePath: c.PackagePath,
|
|
|
|
GoHash: p[3],
|
|
|
|
OK: p[1] == "true",
|
|
|
|
LogHash: p[2],
|
2011-11-29 01:24:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 19:16:47 -07:00
|
|
|
// OK returns the Commit's build state for a specific builder and goHash.
|
|
|
|
func (c *Commit) OK(builder, goHash string) (ok, present bool) {
|
|
|
|
r := c.Result(builder, goHash)
|
|
|
|
if r == nil {
|
|
|
|
return false, false
|
|
|
|
}
|
|
|
|
return r.OK, true
|
|
|
|
}
|
|
|
|
|
2011-11-22 14:13:05 -07:00
|
|
|
// A Result describes a build result for a Commit on an OS/architecture.
|
|
|
|
//
|
|
|
|
// Each Result entity is a descendant of its associated Commit entity.
|
|
|
|
type Result struct {
|
|
|
|
Builder string // "arch-os[-note]"
|
|
|
|
Hash string
|
|
|
|
PackagePath string // (empty for Go commits)
|
|
|
|
|
|
|
|
// The Go Commit this was built against (empty for Go commits).
|
|
|
|
GoHash string
|
|
|
|
|
|
|
|
OK bool
|
2011-12-20 18:13:27 -07:00
|
|
|
Log string `datastore:"-"` // for JSON unmarshaling only
|
2011-11-22 14:13:05 -07:00
|
|
|
LogHash string `datastore:",noindex"` // Key to the Log record.
|
2011-12-20 21:43:12 -07:00
|
|
|
|
|
|
|
RunTime int64 // time to build+test in nanoseconds
|
2011-11-22 14:13:05 -07:00
|
|
|
}
|
|
|
|
|
2011-11-24 18:53:05 -07:00
|
|
|
func (r *Result) Key(c appengine.Context) *datastore.Key {
|
|
|
|
p := Package{Path: r.PackagePath}
|
|
|
|
key := r.Builder + "|" + r.PackagePath + "|" + r.Hash + "|" + r.GoHash
|
|
|
|
return datastore.NewKey(c, "Result", key, 0, p.Key(c))
|
|
|
|
}
|
|
|
|
|
2012-02-05 15:26:32 -07:00
|
|
|
func (r *Result) Valid() error {
|
2011-11-24 18:53:05 -07:00
|
|
|
if !validHash(r.Hash) {
|
2012-02-05 15:26:32 -07:00
|
|
|
return errors.New("invalid Hash")
|
2011-11-24 18:53:05 -07:00
|
|
|
}
|
|
|
|
if r.PackagePath != "" && !validHash(r.GoHash) {
|
2012-02-05 15:26:32 -07:00
|
|
|
return errors.New("invalid GoHash")
|
2011-11-24 18:53:05 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-12-15 16:48:06 -07:00
|
|
|
// Data returns the Result in string format
|
|
|
|
// to be stored in Commit's ResultData field.
|
|
|
|
func (r *Result) Data() string {
|
|
|
|
return fmt.Sprintf("%v|%v|%v|%v", r.Builder, r.OK, r.LogHash, r.GoHash)
|
|
|
|
}
|
|
|
|
|
2011-11-22 14:13:05 -07:00
|
|
|
// A Log is a gzip-compressed log file stored under the SHA1 hash of the
|
|
|
|
// uncompressed log text.
|
|
|
|
type Log struct {
|
|
|
|
CompressedLog []byte
|
|
|
|
}
|
|
|
|
|
2012-02-05 15:26:32 -07:00
|
|
|
func (l *Log) Text() ([]byte, error) {
|
2011-12-20 19:16:47 -07:00
|
|
|
d, err := gzip.NewReader(bytes.NewBuffer(l.CompressedLog))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("reading log data: %v", err)
|
|
|
|
}
|
|
|
|
b, err := ioutil.ReadAll(d)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("reading log data: %v", err)
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2012-02-05 15:26:32 -07:00
|
|
|
func PutLog(c appengine.Context, text string) (hash string, err error) {
|
2011-11-24 18:53:05 -07:00
|
|
|
h := sha1.New()
|
2011-12-20 18:13:27 -07:00
|
|
|
io.WriteString(h, text)
|
2011-11-24 18:53:05 -07:00
|
|
|
b := new(bytes.Buffer)
|
|
|
|
z, _ := gzip.NewWriterLevel(b, gzip.BestCompression)
|
2011-12-20 18:13:27 -07:00
|
|
|
io.WriteString(z, text)
|
2011-11-24 18:53:05 -07:00
|
|
|
z.Close()
|
2012-02-05 15:26:32 -07:00
|
|
|
hash = fmt.Sprintf("%x", h.Sum(nil))
|
2011-11-24 18:53:05 -07:00
|
|
|
key := datastore.NewKey(c, "Log", hash, 0, nil)
|
|
|
|
_, err = datastore.Put(c, key, &Log{b.Bytes()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// A Tag is used to keep track of the most recent Go weekly and release tags.
|
2011-11-22 14:13:05 -07:00
|
|
|
// Typically there will be one Tag entity for each kind of hg tag.
|
|
|
|
type Tag struct {
|
|
|
|
Kind string // "weekly", "release", or "tip"
|
|
|
|
Name string // the tag itself (for example: "release.r60")
|
|
|
|
Hash string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tag) Key(c appengine.Context) *datastore.Key {
|
2011-12-15 16:48:06 -07:00
|
|
|
p := &Package{}
|
2011-11-24 18:53:05 -07:00
|
|
|
return datastore.NewKey(c, "Tag", t.Kind, 0, p.Key(c))
|
|
|
|
}
|
|
|
|
|
2012-02-05 15:26:32 -07:00
|
|
|
func (t *Tag) Valid() error {
|
2011-12-15 16:48:06 -07:00
|
|
|
if t.Kind != "weekly" && t.Kind != "release" && t.Kind != "tip" {
|
2012-02-05 15:26:32 -07:00
|
|
|
return errors.New("invalid Kind")
|
2011-11-24 18:53:05 -07:00
|
|
|
}
|
|
|
|
if !validHash(t.Hash) {
|
2012-02-05 15:26:32 -07:00
|
|
|
return errors.New("invalid Hash")
|
2011-11-24 18:53:05 -07:00
|
|
|
}
|
|
|
|
return nil
|
2011-11-22 14:13:05 -07:00
|
|
|
}
|
|
|
|
|
2012-01-29 20:50:36 -07:00
|
|
|
// Commit returns the Commit that corresponds with this Tag.
|
2012-02-05 15:26:32 -07:00
|
|
|
func (t *Tag) Commit(c appengine.Context) (*Commit, error) {
|
2012-01-29 20:50:36 -07:00
|
|
|
com := &Commit{Hash: t.Hash}
|
|
|
|
err := datastore.Get(c, com.Key(c), com)
|
|
|
|
return com, err
|
|
|
|
}
|
|
|
|
|
2011-12-15 16:48:06 -07:00
|
|
|
// GetTag fetches a Tag by name from the datastore.
|
2012-02-05 15:26:32 -07:00
|
|
|
func GetTag(c appengine.Context, tag string) (*Tag, error) {
|
2011-12-15 16:48:06 -07:00
|
|
|
t := &Tag{Kind: tag}
|
|
|
|
if err := datastore.Get(c, t.Key(c), t); err != nil {
|
|
|
|
if err == datastore.ErrNoSuchEntity {
|
2012-02-05 15:26:32 -07:00
|
|
|
return nil, errors.New("tag not found: " + tag)
|
2011-12-15 16:48:06 -07:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := t.Valid(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2012-01-29 17:59:06 -07:00
|
|
|
// Packages returns packages of the specified kind.
|
|
|
|
// Kind must be one of "external" or "subrepo".
|
2012-02-05 15:26:32 -07:00
|
|
|
func Packages(c appengine.Context, kind string) ([]*Package, error) {
|
2012-01-29 17:59:06 -07:00
|
|
|
switch kind {
|
|
|
|
case "external", "subrepo":
|
|
|
|
default:
|
2012-02-05 15:26:32 -07:00
|
|
|
return nil, errors.New(`kind must be one of "external" or "subrepo"`)
|
2012-01-29 17:59:06 -07:00
|
|
|
}
|
2011-11-29 01:24:57 -07:00
|
|
|
var pkgs []*Package
|
2012-01-29 17:59:06 -07:00
|
|
|
q := datastore.NewQuery("Package").Filter("Kind=", kind)
|
|
|
|
for t := q.Run(c); ; {
|
2011-11-29 01:24:57 -07:00
|
|
|
pkg := new(Package)
|
|
|
|
if _, err := t.Next(pkg); err == datastore.Done {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
2011-12-01 22:05:12 -07:00
|
|
|
return nil, err
|
2011-11-29 01:24:57 -07:00
|
|
|
}
|
|
|
|
if pkg.Path != "" {
|
|
|
|
pkgs = append(pkgs, pkg)
|
|
|
|
}
|
|
|
|
}
|
2011-12-01 22:05:12 -07:00
|
|
|
return pkgs, nil
|
2011-11-29 01:24:57 -07:00
|
|
|
}
|