removed builds table since not being used in the near term

This commit is contained in:
Brad 2014-06-07 12:57:20 -07:00
parent 90df70d109
commit 1fff27a28c
19 changed files with 122 additions and 646 deletions

View file

@ -80,21 +80,12 @@ var stmts = []string{`
,commit_updated INTEGER
,UNIQUE(commit_sha, commit_branch, repo_id)
);`, `
CREATE TABLE IF NOT EXISTS builds (
build_id INTEGER PRIMARY KEY AUTOINCREMENT
CREATE TABLE IF NOT EXISTS output (
output_id INTEGER PRIMARY KEY AUTOINCREMENT
,commit_id INTEGER
,build_number INTEGER
,build_matrix VARCHAR(255)
,build_status VARCHAR(255)
,build_console BLOB
,build_started INTEGER
,build_finished INTEGER
,build_duration INTEGER
,build_created INTEGER
,build_updated INTEGER
,UNIQUE(commit_id, build_number)
,output_raw BLOB
,UNIQUE(commit_id)
);`,
`CREATE INDEX IF NOT EXISTS builds_commit_id ON builds (commit_id)`,
}
func Load(db *sql.DB) {

View file

@ -31,12 +31,9 @@ var stmts = []string{
"insert into commits values (4, 1, 'Success', 1398065345, 1398069999, 854, 'd12c9e5a11982f71796ad909c93551b16fba053e', 'dev', '', 'drcooper@caltech.edu', 'ab23a88a3ed77ecdfeb894c0eaf2817a', 'Wed Apr 23 02:00:00 2014 -0700', 'a commit message', '', 1398065343, 1398065344);",
"insert into commits values (5, 1, 'Started', 1398065345, 0, 0, '85f8c029b902ed9400bc600bac301a0aadb144ac', 'master', '', 'drcooper@caltech.edu', 'ab23a88a3ed77ecdfeb894c0eaf2817a', 'Wed Apr 23 03:00:00 2014 -0700', 'a commit message', '', 1398065343, 1398065344);",
// insert build entries
"insert into builds values (1, 2, 1, '', 'Success', 'some output', 1398065345, 1398069999, 854, 1398065343, 1398065344);",
"insert into builds values (2, 2, 2, '', 'Success', 'some output', 1398065345, 1398069999, 854, 1398065343, 1398065344);",
"insert into builds values (3, 2, 3, '', 'Success', 'some output', 1398065345, 1398069999, 854, 1398065343, 1398065344);",
"insert into builds values (4, 1, 1, '', 'Success', 'some output', 1398065345, 1398069999, 854, 1398065343, 1398065344);",
"insert into builds values (5, 3, 1, '', 'Started', 'some output', 1398065345, 0, 0, 1398065343, 1398065344);",
// insert commit console output
"insert into output values (1, 1, 'sample console output');",
"insert into output values (2, 2, 'sample console output.....');",
}
// Load will populate the database with a fixed dataset for

View file

@ -4,7 +4,6 @@ import (
"net/http"
"time"
"github.com/drone/drone/server/resource/build"
"github.com/drone/drone/server/resource/commit"
"github.com/drone/drone/server/resource/repo"
"github.com/gorilla/pat"
@ -59,24 +58,24 @@ func (h *BadgeHandler) GetStatus(w http.ResponseWriter, r *http.Request) error {
}
// get the latest commit
commit, _ := h.commits.FindLatest(arepo.ID, branch)
c, _ := h.commits.FindLatest(arepo.ID, branch)
// if no commit was found then display
// the 'none' badge
if commit == nil {
if c == nil {
w.Write(badgeNone)
return nil
}
// determine which badge to load
switch commit.Status {
case build.StatusSuccess:
switch c.Status {
case commit.StatusSuccess:
w.Write(badgeSuccess)
case build.StatusFailure:
case commit.StatusFailure:
w.Write(badgeFailure)
case build.StatusError:
case commit.StatusError:
w.Write(badgeError)
case build.StatusEnqueue, build.StatusStarted:
case commit.StatusEnqueue, commit.StatusStarted:
w.Write(badgeStarted)
default:
w.Write(badgeNone)

View file

@ -1,145 +0,0 @@
package handler
import (
"encoding/json"
"net/http"
"strconv"
"github.com/drone/drone/server/resource/build"
"github.com/drone/drone/server/resource/commit"
"github.com/drone/drone/server/resource/perm"
"github.com/drone/drone/server/resource/repo"
"github.com/drone/drone/server/session"
"github.com/gorilla/pat"
)
type BuildHandler struct {
builds build.BuildManager
commits commit.CommitManager
repos repo.RepoManager
perms perm.PermManager
sess session.Session
}
func NewBuildHandler(repos repo.RepoManager, commits commit.CommitManager, builds build.BuildManager,
perms perm.PermManager, sess session.Session) *BuildHandler {
return &BuildHandler{builds, commits, repos, perms, sess}
}
// GetCommit gets the commit for the repository, branch and sha.
// GET /v1/repos/{host}/{owner}/{name}/branches/{branch}/commits/{commit}/builds/{build}
func (h *BuildHandler) GetBuild(w http.ResponseWriter, r *http.Request) error {
var host, owner, name = parseRepo(r)
var branch = r.FormValue(":branch")
var sha = r.FormValue(":commit")
var num, _ = strconv.Atoi(r.FormValue(":build"))
// get the user form the session.
user := h.sess.User(r)
// get the repository from the database.
repo, err := h.repos.FindName(host, owner, name)
if err != nil {
return notFound{err}
}
// user must have read access to the repository.
if ok, _ := h.perms.Read(user, repo); !ok {
return notFound{err}
}
// get the commit information for the specified hash
commit, err := h.commits.FindSha(repo.ID, branch, sha)
if err != nil {
return notFound{err}
}
// get the builds for the hash
build, err := h.builds.FindNumber(commit.ID, int64(num))
if err != nil {
return notFound{err}
}
return json.NewEncoder(w).Encode(build)
}
// GetBuilds gets the list of builds for a commit.
// GET /v1/repos/{host}/{owner}/{name}/branches/{branch}/commits/{commit}/builds
func (h *BuildHandler) GetBuilds(w http.ResponseWriter, r *http.Request) error {
var host, owner, name = parseRepo(r)
var branch = r.FormValue(":branch")
var sha = r.FormValue(":commit")
// get the user form the session.
user := h.sess.User(r)
// get the repository from the database.
repo, err := h.repos.FindName(host, owner, name)
if err != nil {
return notFound{err}
}
// user must have read access to the repository.
if ok, _ := h.perms.Read(user, repo); !ok {
return notFound{err}
}
// get the commit information for the specified hash
commit, err := h.commits.FindSha(repo.ID, branch, sha)
if err != nil {
return notFound{err}
}
// get the builds for the hash
builds, err := h.builds.List(commit.ID)
if err != nil {
return notFound{err}
}
return json.NewEncoder(w).Encode(builds)
}
// GetOut gets the console output for a build. If the build is in-progress it
// returns a link to the websocket (I think ...)
// GET /v1/repos/{host}/{owner}/{name}/branches/{branch}/commits/{commit}/builds/{build}/out
func (h *BuildHandler) GetOut(w http.ResponseWriter, r *http.Request) error {
var host, owner, name = parseRepo(r)
var branch = r.FormValue(":branch")
var sha = r.FormValue(":commit")
var num, _ = strconv.Atoi(r.FormValue(":build"))
// get the user form the session.
user := h.sess.User(r)
// get the repository from the database.
repo, err := h.repos.FindName(host, owner, name)
if err != nil {
return notFound{err}
}
// user must have read access to the repository.
if ok, _ := h.perms.Read(user, repo); !ok {
return notFound{err}
}
// get the commit information for the specified hash
commit, err := h.commits.FindSha(repo.ID, branch, sha)
if err != nil {
return notFound{err}
}
// get the builds for the hash
out, err := h.builds.FindOutput(commit.ID, int64(num))
if err != nil {
return notFound{err}
}
w.Write(out)
return nil
}
func (h *BuildHandler) Register(r *pat.Router) {
r.Get("/v1/repos/{host}/{owner}/{name}/branches/{branch}/commits/{commit}/builds/{build}/console", errorHandler(h.GetOut))
r.Get("/v1/repos/{host}/{owner}/{name}/branches/{branch}/commits/{commit}/builds/{build}", errorHandler(h.GetBuild))
r.Get("/v1/repos/{host}/{owner}/{name}/branches/{branch}/commits/{commit}/builds", errorHandler(h.GetBuilds))
}

View file

@ -79,6 +79,41 @@ func (h *CommitHandler) GetCommit(w http.ResponseWriter, r *http.Request) error
return json.NewEncoder(w).Encode(commit)
}
// GetCommitOutput gets the commit's stdout.
// GET /v1/repos/{host}/{owner}/{name}/branches/{branch}/commits/{commit}/console
func (h *CommitHandler) GetCommitOutput(w http.ResponseWriter, r *http.Request) error {
var host, owner, name = parseRepo(r)
var branch = r.FormValue(":branch")
var sha = r.FormValue(":commit")
// get the user form the session.
user := h.sess.User(r)
// get the repository from the database.
repo, err := h.repos.FindName(host, owner, name)
if err != nil {
return notFound{err}
}
// user must have read access to the repository.
if ok, _ := h.perms.Read(user, repo); !ok {
return notFound{err}
}
commit, err := h.commits.FindSha(repo.ID, branch, sha)
if err != nil {
return notFound{err}
}
output, err := h.commits.FindOutput(commit.ID)
if err != nil {
return notFound{err}
}
w.Write(output)
return nil
}
// PostCommit gets the commit for the repository and schedules to re-build.
// GET /v1/repos/{host}/{owner}/{name}/branches/{branch}/commits/{commit}
func (h *CommitHandler) PostCommit(w http.ResponseWriter, r *http.Request) error {
@ -86,6 +121,7 @@ func (h *CommitHandler) PostCommit(w http.ResponseWriter, r *http.Request) error
}
func (h *CommitHandler) Register(r *pat.Router) {
r.Get("/v1/repos/{host}/{owner}/{name}/branches/{branch}/commits/{commit}/console", errorHandler(h.GetCommitOutput))
r.Get("/v1/repos/{host}/{owner}/{name}/branches/{branch}/commits/{commit}", errorHandler(h.GetCommit))
r.Post("/v1/repos/{host}/{owner}/{name}/branches/{branch}/commits/{commit}", errorHandler(h.PostCommit)).Queries("action", "rebuild")
r.Get("/v1/repos/{host}/{owner}/{name}/branches/{branch}/commits", errorHandler(h.GetFeed))

View file

@ -4,7 +4,6 @@ import (
"fmt"
"net/http"
"github.com/drone/drone/server/resource/build"
"github.com/drone/drone/server/resource/commit"
"github.com/drone/drone/server/resource/config"
"github.com/drone/drone/server/resource/repo"
@ -16,13 +15,11 @@ type HookHandler struct {
users user.UserManager
repos repo.RepoManager
commits commit.CommitManager
builds build.BuildManager
conf *config.Config
}
func NewHookHandler(users user.UserManager, repos repo.RepoManager, commits commit.CommitManager,
builds build.BuildManager, conf *config.Config) *HookHandler {
return &HookHandler{users, repos, commits, builds, conf}
func NewHookHandler(users user.UserManager, repos repo.RepoManager, commits commit.CommitManager, conf *config.Config) *HookHandler {
return &HookHandler{users, repos, commits, conf}
}
// PostHook receives a post-commit hook from GitHub, Bitbucket, etc
@ -90,16 +87,6 @@ func (h *HookHandler) PostHook(w http.ResponseWriter, r *http.Request) error {
return badRequest{err}
}
b := build.Build{
CommitID: c.ID,
Status: c.Status,
Number: 1,
}
// inser the build entry into the database
if err := h.builds.Insert(&b); err != nil {
return internalServerError{err}
}
fmt.Printf("%#v", hook)
fmt.Printf("%s", script)

View file

@ -4,7 +4,6 @@ import (
"net/http"
"github.com/drone/drone/server/render"
"github.com/drone/drone/server/resource/build"
"github.com/drone/drone/server/resource/commit"
"github.com/drone/drone/server/resource/perm"
"github.com/drone/drone/server/resource/repo"
@ -18,16 +17,13 @@ type SiteHandler struct {
users user.UserManager
repos repo.RepoManager
commits commit.CommitManager
builds build.BuildManager
perms perm.PermManager
sess session.Session
render render.Render
}
func NewSiteHandler(users user.UserManager,
repos repo.RepoManager, commits commit.CommitManager, builds build.BuildManager,
perms perm.PermManager, sess session.Session, render render.Render) *SiteHandler {
return &SiteHandler{users, repos, commits, builds, perms, sess, render}
func NewSiteHandler(users user.UserManager, repos repo.RepoManager, commits commit.CommitManager, perms perm.PermManager, sess session.Session, render render.Render) *SiteHandler {
return &SiteHandler{users, repos, commits, perms, sess, render}
}
// GetIndex serves the root domain request. This is forwarded to the dashboard
@ -102,8 +98,6 @@ func (s *SiteHandler) GetRepo(w http.ResponseWriter, r *http.Request) error {
Branches []*commit.Commit
Commits []*commit.Commit
Commit *commit.Commit
Builds []*build.Build
Build *build.Build
}{User: usr, Repo: arepo}
// if commit details are provided we should retrieve the build details
@ -113,10 +107,6 @@ func (s *SiteHandler) GetRepo(w http.ResponseWriter, r *http.Request) error {
if err != nil {
return s.render(w, "404.html", nil)
}
data.Build, err = s.builds.FindNumber(data.Commit.ID, 1)
if err != nil {
return s.render(w, "404.html", nil)
}
return s.render(w, "repo_commit.html", &data)
}

View file

@ -9,7 +9,6 @@ import (
"github.com/drone/drone/server/database"
"github.com/drone/drone/server/handler"
"github.com/drone/drone/server/render"
"github.com/drone/drone/server/resource/build"
"github.com/drone/drone/server/resource/commit"
"github.com/drone/drone/server/resource/config"
"github.com/drone/drone/server/resource/perm"
@ -72,7 +71,6 @@ func main() {
repos := repo.NewManager(db)
users := user.NewManager(db)
perms := perm.NewManager(db)
builds := build.NewManager(db)
commits := commit.NewManager(db)
// setup the session managers
@ -82,15 +80,14 @@ func main() {
router := pat.New()
handler.NewUsersHandler(users, sess).Register(router)
handler.NewUserHandler(users, repos, commits, sess).Register(router)
handler.NewHookHandler(users, repos, commits, builds, &conf).Register(router)
handler.NewHookHandler(users, repos, commits, &conf).Register(router)
handler.NewLoginHandler(users, repos, perms, sess, &conf).Register(router)
handler.NewBuildHandler(repos, commits, builds, perms, sess).Register(router)
handler.NewCommitHandler(repos, commits, perms, sess).Register(router)
handler.NewBranchHandler(repos, commits, perms, sess).Register(router)
handler.NewRepoHandler(repos, commits, perms, sess, &conf).Register(router)
handler.NewBadgeHandler(repos, commits).Register(router)
handler.NewConfigHandler(conf, sess).Register(router)
handler.NewSiteHandler(users, repos, commits, builds, perms, sess, templ).Register(router)
handler.NewSiteHandler(users, repos, commits, perms, sess, templ).Register(router)
// serve static assets
// TODO we need to replace this with go.rice

View file

@ -1,13 +0,0 @@
package committest
import (
"database/sql"
)
func Load(db *sql.DB) {
db.Exec("insert into builds values (1, 2, 1, '', 'Success', 'some output', 1398065345, 1398069999, 854, 1398065343, 1398065344);")
db.Exec("insert into builds values (2, 2, 2, '', 'Success', 'some output', 1398065345, 1398069999, 854, 1398065343, 1398065344);")
db.Exec("insert into builds values (3, 2, 3, '', 'Success', 'some output', 1398065345, 1398069999, 854, 1398065343, 1398065344);")
db.Exec("insert into builds values (4, 1, 1, '', 'Success', 'some output', 1398065345, 1398069999, 854, 1398065343, 1398065344);")
db.Exec("insert into builds values (5, 3, 1, '', 'Started', 'some output', 1398065345, 0, 0, 1398065343, 1398065344);")
}

View file

@ -1,128 +0,0 @@
package build
import (
"database/sql"
"time"
"github.com/russross/meddler"
)
type BuildManager interface {
// Find finds the build by ID.
Find(id int64) (*Build, error)
// FindNumber finds the build with the specified build number.
FindNumber(commit, number int64) (*Build, error)
// FindOutput finds the build's output.
FindOutput(commit, number int64) ([]byte, error)
// List finds all builds for the commit ID.
List(commit int64) ([]*Build, error)
// Insert persists the build to the datastore.
Insert(build *Build) error
// Update persists changes to the build to the datastore.
Update(build *Build) error
// UpdateOutput persists a build's stdout to the datastore.
UpdateOutput(build *Build, out []byte) error
// Delete removes the build from the datastore.
Delete(build *Build) error
}
// buildManager manages a list of builds in a SQL database.
type buildManager struct {
*sql.DB
}
// SQL query to retrieve a list of builds by commit ID.
const listQuery = `
SELECT build_id, commit_id, build_number, build_matrix, build_status,
build_started, build_finished, build_duration, build_created, build_updated
FROM builds
WHERE commit_id=?
ORDER BY build_number ASC
`
// SQL query to retrieve a build by commit ID and build number.
const findQuery = `
SELECT build_id, commit_id, build_number, build_matrix, build_status,
build_started, build_finished, build_duration, build_created, build_updated
FROM builds
WHERE commit_id=?
AND build_number=?
LIMIT 1
`
// SQL query to retrieve a build's console output by build ID.
const findOutputQuery = `
SELECT build_console
FROM builds
WHERE commit_id=?
AND build_number=?
LIMIT 1
`
// SQL statement to update a build's console output.
const updateStmt = `
UPDATE builds set build_console=? WHERE build_id=?
`
// SQL statement to delete a build by ID.
const deleteStmt = `
DELETE FROM builds WHERE build_id = ?
`
// NewManager initiales a new BuildManager intended to
// manage and persist builds.
func NewManager(db *sql.DB) BuildManager {
return &buildManager{db}
}
func (db *buildManager) Find(id int64) (*Build, error) {
dst := Build{}
err := meddler.Load(db, "builds", &dst, id)
return &dst, err
}
func (db *buildManager) FindNumber(commit, number int64) (*Build, error) {
dst := Build{}
err := meddler.QueryRow(db, &dst, findQuery, commit, number)
return &dst, err
}
func (db *buildManager) FindOutput(commit, number int64) ([]byte, error) {
var dst string
err := db.QueryRow(findOutputQuery, commit, number).Scan(&dst)
return []byte(dst), err
}
func (db *buildManager) List(commit int64) ([]*Build, error) {
var dst []*Build
err := meddler.QueryAll(db, &dst, listQuery, commit)
return dst, err
}
func (db *buildManager) Insert(build *Build) error {
build.Created = time.Now().Unix()
build.Updated = time.Now().Unix()
return meddler.Insert(db, "builds", build)
}
func (db *buildManager) Update(build *Build) error {
build.Updated = time.Now().Unix()
return meddler.Update(db, "builds", build)
}
func (db *buildManager) UpdateOutput(build *Build, out []byte) error {
_, err := db.Exec(updateStmt, out, build.ID)
return err
}
func (db *buildManager) Delete(build *Build) error {
_, err := db.Exec(deleteStmt, build.ID)
return err
}

View file

@ -1,248 +0,0 @@
package build
import (
"database/sql"
"strings"
"testing"
"time"
"github.com/drone/drone/pkg/database"
"github.com/drone/drone/pkg/resource/build/buildtest"
_ "github.com/mattn/go-sqlite3"
)
// in-memory database instance for unit testing
var db *sql.DB
// setup the test database and test fixtures
func setup() {
db, _ = sql.Open("sqlite3", ":memory:")
database.Load(db)
committest.Load(db)
}
// teardown the test database
func teardown() {
db.Close()
}
func TestFind(t *testing.T) {
setup()
defer teardown()
builds := NewManager(db)
build, err := builds.Find(1)
if err != nil {
t.Errorf("Want Build from ID, got %s", err)
}
testBuild(t, build)
}
func TestFindNumber(t *testing.T) {
setup()
defer teardown()
builds := NewManager(db)
build, err := builds.FindNumber(2, 1)
if err != nil {
t.Errorf("Want Build from Number, got %s", err)
}
testBuild(t, build)
}
func TestFindOutput(t *testing.T) {
setup()
defer teardown()
builds := NewManager(db)
out, err := builds.FindOutput(2, 1)
if err != nil {
t.Errorf("Want Build Output from Number, got %s", err)
}
var got, want = string(out), "some output"
if got != want {
t.Errorf("Want build output %v, got %v", want, got)
}
}
func TestList(t *testing.T) {
setup()
defer teardown()
builds := NewManager(db)
list, err := builds.List(2)
if err != nil {
t.Errorf("Want List from CommitID, got %s", err)
}
var got, want = len(list), 3
if got != want {
t.Errorf("Want List size %v, got %v", want, got)
}
testBuild(t, list[0])
}
func TestInsert(t *testing.T) {
setup()
defer teardown()
builds := NewManager(db)
if err := builds.Insert(&Build{CommitID: 4, Number: 1}); err != nil {
t.Errorf("Want Build created, got %s", err)
}
// verify that it is ok to add same commit ID for incremented build number
if err := builds.Insert(&Build{CommitID: 4, Number: 2}); err != nil {
t.Errorf("Want Build created, got %s", err)
}
// verify that unique constraint fails when commit ID and build number already exist
err := builds.Insert(&Build{CommitID: 4, Number: 2})
if err == nil || !strings.Contains(err.Error(), "commit_id, build_number are not unique") {
t.Errorf("Want unique constraint violated, got %s", err)
}
}
func TestUpdate(t *testing.T) {
setup()
defer teardown()
builds := NewManager(db)
build, err := builds.Find(5)
if err != nil {
t.Errorf("Want Build from ID, got %s", err)
}
// update the build's access token
build.Status = "Success"
build.Finished = time.Now().Unix()
build.Duration = 999
if err := builds.Update(build); err != nil {
t.Errorf("Want Build updated, got %s", err)
}
updated, _ := builds.Find(5)
var got, want = updated.Status, "Success"
if got != want {
t.Errorf("Want updated Status %v, got %v", want, got)
}
var gotInt64, wantInt64 = updated.ID, build.ID
if gotInt64 != wantInt64 {
t.Errorf("Want build ID %v, got %v", wantInt64, gotInt64)
}
gotInt64, wantInt64 = updated.Duration, build.Duration
if gotInt64 != wantInt64 {
t.Errorf("Want updated Duration %v, got %v", wantInt64, gotInt64)
}
gotInt64, wantInt64 = updated.Finished, build.Finished
if gotInt64 != wantInt64 {
t.Errorf("Want updated Finished %v, got %v", wantInt64, gotInt64)
}
}
func TestFindUpdateOutput(t *testing.T) {
setup()
defer teardown()
builds := NewManager(db)
build, err := builds.Find(5)
if err != nil {
t.Errorf("Want Build from ID, got %s", err)
}
if err := builds.UpdateOutput(build, []byte("some output ...")); err != nil {
t.Errorf("Want Build updated, got %s", err)
}
out, err := builds.FindOutput(build.CommitID, build.Number)
if err != nil {
t.Errorf("Want Build Output, got %s", err)
}
var got, want = string(out), "some output ..."
if got != want {
t.Errorf("Want Build Output %v, got %v", want, got)
}
}
func TestDelete(t *testing.T) {
setup()
defer teardown()
builds := NewManager(db)
build, err := builds.Find(1)
if err != nil {
t.Errorf("Want Build from ID, got %s", err)
}
// delete the builds
if err := builds.Delete(build); err != nil {
t.Errorf("Want Build deleted, got %s", err)
}
// check to see if the deleted build is actually gone
if _, err := builds.Find(1); err != sql.ErrNoRows {
t.Errorf("Want ErrNoRows, got %s", err)
}
}
// testBuild is a helper function that compares the build
// to an expected set of fixed field values.
func testBuild(t *testing.T, build *Build) {
var got, want = build.Status, "Success"
if got != want {
t.Errorf("Want Status %v, got %v", want, got)
}
got, want = build.Matrix, ""
if got != want {
t.Errorf("Want Matrix %v, got %v", want, got)
}
var gotInt64, wantInt64 = build.ID, int64(1)
if gotInt64 != wantInt64 {
t.Errorf("Want ID %v, got %v", wantInt64, gotInt64)
}
gotInt64, wantInt64 = build.CommitID, int64(2)
if gotInt64 != wantInt64 {
t.Errorf("Want CommitID %v, got %v", wantInt64, gotInt64)
}
gotInt64, wantInt64 = build.Number, int64(1)
if gotInt64 != wantInt64 {
t.Errorf("Want Number %v, got %v", wantInt64, gotInt64)
}
gotInt64, wantInt64 = build.Created, int64(1398065343)
if gotInt64 != wantInt64 {
t.Errorf("Want Created %v, got %v", wantInt64, gotInt64)
}
gotInt64, wantInt64 = build.Updated, int64(1398065344)
if gotInt64 != wantInt64 {
t.Errorf("Want Updated %v, got %v", wantInt64, gotInt64)
}
gotInt64, wantInt64 = build.Started, int64(1398065345)
if gotInt64 != wantInt64 {
t.Errorf("Want Started %v, got %v", wantInt64, gotInt64)
}
gotInt64, wantInt64 = build.Finished, int64(1398069999)
if gotInt64 != wantInt64 {
t.Errorf("Want Finished %v, got %v", wantInt64, gotInt64)
}
gotInt64, wantInt64 = build.Duration, int64(854)
if gotInt64 != wantInt64 {
t.Errorf("Want Duration %v, got %v", wantInt64, gotInt64)
}
}

View file

@ -1,29 +0,0 @@
package build
const (
StatusNone = "None"
StatusEnqueue = "Pending"
StatusStarted = "Started"
StatusSuccess = "Success"
StatusFailure = "Failure"
StatusError = "Error"
)
type Build struct {
ID int64 `meddler:"build_id,pk" json:"id"`
CommitID int64 `meddler:"commit_id" json:"-"`
Number int64 `meddler:"build_number" json:"number"`
Matrix string `meddler:"build_matrix" json:"matrix"`
Status string `meddler:"build_status" json:"status"`
Started int64 `meddler:"build_started" json:"started_at"`
Finished int64 `meddler:"build_finished" json:"finished_at"`
Duration int64 `meddler:"build_duration" json:"duration"`
Created int64 `meddler:"build_created" json:"created_at"`
Updated int64 `meddler:"build_updated" json:"updated_at"`
}
// IsRunning returns true if the Build statis is Started
// or Pending, indicating it is currently running.
func (b *Build) IsRunning() bool {
return (b.Status == StatusStarted || b.Status == StatusEnqueue)
}

View file

@ -18,7 +18,7 @@ type CommitManager interface {
FindLatest(repo int64, branch string) (*Commit, error)
// FindOutput finds the commit's output.
//FindOutput(id int64) ([]byte, error)
FindOutput(commit int64) ([]byte, error)
// List finds recent commits for the repository
List(repo int64) ([]*Commit, error)
@ -39,7 +39,7 @@ type CommitManager interface {
Update(commit *Commit) error
// UpdateOutput persists a commit's stdout to the datastore.
//UpdateOutput(commit *Commit, out []byte) error
UpdateOutput(commit *Commit, out []byte) error
// Delete removes the commit from the datastore.
Delete(commit *Commit) error
@ -121,6 +121,23 @@ WHERE commit_id IN (
AND commit_branch=?)
`
// SQL query to retrieve a Commit's stdout.
const findOutputQuery = `
SELECT output_raw
FROM output
WHERE commit_id = ?
`
// SQL statement to insert a Commit's stdout.
const insertOutputStmt = `
INSERT INTO output (commit_id, output_raw) values (?,?);
`
// SQL statement to update a Commit's stdout.
const updateOutputStmt = `
UPDATE output SET output_raw = ? WHERE commit_id = ?;
`
// SQL statement to delete a Commit by ID.
const deleteStmt = `
DELETE FROM commits WHERE commit_id = ?
@ -144,6 +161,12 @@ func (db *commitManager) FindLatest(repo int64, branch string) (*Commit, error)
return &dst, err
}
func (db *commitManager) FindOutput(commit int64) ([]byte, error) {
var dst string
err := db.QueryRow(findOutputQuery, commit).Scan(&dst)
return []byte(dst), err
}
func (db *commitManager) List(repo int64) ([]*Commit, error) {
var dst []*Commit
err := meddler.QueryAll(db, &dst, listQuery, repo)
@ -179,6 +202,15 @@ func (db *commitManager) Update(commit *Commit) error {
return meddler.Update(db, "commits", commit)
}
func (db *commitManager) UpdateOutput(commit *Commit, out []byte) error {
_, err := db.Exec(insertOutputStmt, commit.ID, out)
if err != nil {
return nil
}
_, err = db.Exec(updateOutputStmt, out, commit.ID)
return err
}
func (db *commitManager) Delete(commit *Commit) error {
_, err := db.Exec(deleteStmt, commit.ID)
return err

View file

@ -6,8 +6,8 @@ import (
"testing"
"time"
"github.com/drone/drone/pkg/database"
"github.com/drone/drone/pkg/resource/commit/committest"
"github.com/drone/drone/server/database"
"github.com/drone/drone/server/database/testdata"
_ "github.com/mattn/go-sqlite3"
)
@ -18,7 +18,7 @@ var db *sql.DB
func setup() {
db, _ = sql.Open("sqlite3", ":memory:")
database.Load(db)
committest.Load(db)
testdata.Load(db)
}
// teardown the test database
@ -65,6 +65,22 @@ func TestFindLatest(t *testing.T) {
testCommit(t, commit)
}
func TestFindOutput(t *testing.T) {
setup()
defer teardown()
commits := NewManager(db)
out, err := commits.FindOutput(1)
if err != nil {
t.Errorf("Want Commit stdout, got %s", err)
}
var want, got = "sample console output", string(out)
if want != got {
t.Errorf("Want stdout %v, got %v", want, got)
}
}
func TestList(t *testing.T) {
setup()
defer teardown()

View file

@ -5,10 +5,10 @@ import (
//"strings"
"testing"
"github.com/drone/drone/pkg/database"
"github.com/drone/drone/pkg/resource/perm/permdata"
"github.com/drone/drone/pkg/resource/repo"
"github.com/drone/drone/pkg/resource/user"
"github.com/drone/drone/server/database"
"github.com/drone/drone/server/database/testdata"
"github.com/drone/drone/server/resource/repo"
"github.com/drone/drone/server/resource/user"
_ "github.com/mattn/go-sqlite3"
)
@ -19,7 +19,7 @@ var db *sql.DB
func setup() {
db, _ = sql.Open("sqlite3", ":memory:")
database.Load(db)
permdata.Load(db)
testdata.Load(db)
}
// teardown the test database

View file

@ -1,11 +0,0 @@
package permdata
import (
"database/sql"
)
func Load(db *sql.DB) {
db.Exec("insert into perms values (1, 101, 200, 1, 1, 1, 1398065343, 1398065344);")
db.Exec("insert into perms values (2, 102, 200, 1, 1, 0, 1398065343, 1398065344);")
db.Exec("insert into perms values (3, 103, 200, 1, 0, 0, 1398065343, 1398065344);")
}

View file

@ -5,8 +5,8 @@ import (
"strings"
"testing"
"github.com/drone/drone/pkg/database"
"github.com/drone/drone/pkg/database/testdata"
"github.com/drone/drone/server/database"
"github.com/drone/drone/server/database/testdata"
_ "github.com/mattn/go-sqlite3"
)

View file

@ -32,6 +32,11 @@ func NewSession(users user.UserManager) Session {
// User gets the currently authenticated user from the secure cookie session.
func (s *session) User(r *http.Request) *user.User {
if true {
user, _ := s.users.Find(1)
return user
}
switch {
case r.FormValue("access_token") == "":
return s.UserCookie(r)

View file

@ -71,7 +71,7 @@ block append scripts
});
else
script
$.get("/v1/repos/"+#{Repo.Remote}+"/"+#{Repo.Owner}+"/"+#{Repo.Name}+"/branches/"+#{Commit.Branch}+"/commits/"+#{Commit.Sha}+"/builds/1/console", function( data ) {
$.get("/v1/repos/"+#{Repo.Remote}+"/"+#{Repo.Owner}+"/"+#{Repo.Name}+"/branches/"+#{Commit.Branch}+"/commits/"+#{Commit.Sha}+"/console", function( data ) {
var lineFormatter = new Drone.LineFormatter();
$( "#stdout" ).html(lineFormatter.format(data));
});