Fixing TODO comments in code /cc @oliveiradan

1. server/login.go:49 (// TODO(bradrydzewski) return an error message instead). Added error message if authorization fails.
2. server/repos.go:178 (TODO(bradrydzewski) verify repo not exists). Added a checking for the repo and return an error in case it does not exist.
3. server/queue.go:170:  // TODO (bradrydzewski) change this interface to accept an io.Reader. All references to the API change been in question SetLogs() have been modified.
4. remote/github/github.go:106  // Fixed a crash in case *repo_.Language is nil , when de-referencing it. This could happen when a repo only has a readme, so github hasn't set the language yet.
5. ./server/queue.go:170:  // TODO (bradrydzewski) change this interface to accept an io.Reader. All references to the API change been in question SetLogs() have been modified.
6. .remote/github/github.go:106  // Fixed a crash in case *repo_.Language is nil , when de-referencing it. This could happen when a repo only has a readme, so github hasn't set the language yet.
This commit is contained in:
Daniel Oliveira 2015-05-07 23:31:25 -06:00 committed by Ben Schumacher
parent 0034c12141
commit ba159976a0
10 changed files with 39 additions and 28 deletions

View file

@ -1,6 +1,7 @@
package builtin package builtin
import ( import (
"bytes"
"github.com/drone/drone/common" "github.com/drone/drone/common"
. "github.com/franela/goblin" . "github.com/franela/goblin"
"io/ioutil" "io/ioutil"
@ -63,7 +64,7 @@ func TestRepo(t *testing.T) {
db.SetBuild(testRepo, &common.Build{State: "success"}) db.SetBuild(testRepo, &common.Build{State: "success"})
db.SetBuild(testRepo, &common.Build{State: "success"}) db.SetBuild(testRepo, &common.Build{State: "success"})
db.SetBuild(testRepo, &common.Build{State: "pending"}) db.SetBuild(testRepo, &common.Build{State: "pending"})
db.SetLogs(testRepo, 1, 1, []byte("foo")) db.SetLogs(testRepo, 1, 1, (bytes.NewBuffer([]byte("foo"))))
// first a little sanity to validate our test conditions // first a little sanity to validate our test conditions
_, err = db.BuildLast(testRepo) _, err = db.BuildLast(testRepo)

View file

@ -2,19 +2,27 @@ package builtin
import ( import (
"bytes" "bytes"
"github.com/boltdb/bolt"
"io" "io"
"io/ioutil"
"strconv" "strconv"
"github.com/boltdb/bolt"
) )
// SetLogs inserts or updates a task logs for the // SetLogs inserts or updates a task logs for the
// named repository and build number. // named repository and build number.
func (db *DB) SetLogs(repo string, build int, task int, log []byte) error { func (db *DB) SetLogs(repo string, build int, task int, rd io.Reader) error {
key := []byte(repo + "/" + strconv.Itoa(build) + "/" + strconv.Itoa(task)) key := []byte(repo + "/" + strconv.Itoa(build) + "/" + strconv.Itoa(task))
t, err := db.Begin(true) t, err := db.Begin(true)
if err != nil { if err != nil {
return err return err
} }
log, err := ioutil.ReadAll(rd)
if err != nil {
return err
}
err = t.Bucket(bucketBuildLogs).Put(key, log) err = t.Bucket(bucketBuildLogs).Put(key, log)
if err != nil { if err != nil {
t.Rollback() t.Rollback()

View file

@ -1,6 +1,7 @@
package builtin package builtin
import ( import (
"bytes"
"github.com/drone/drone/common" "github.com/drone/drone/common"
. "github.com/franela/goblin" . "github.com/franela/goblin"
"io/ioutil" "io/ioutil"
@ -30,13 +31,13 @@ func TestTask(t *testing.T) {
g.It("Should set Logs", func() { g.It("Should set Logs", func() {
db.SetRepo(&common.Repo{FullName: testRepo}) db.SetRepo(&common.Repo{FullName: testRepo})
err := db.SetLogs(testRepo, testBuild, testTask, testLogInfo) err := db.SetLogs(testRepo, testBuild, testTask, (bytes.NewBuffer(testLogInfo)))
g.Assert(err).Equal(nil) g.Assert(err).Equal(nil)
}) })
g.It("Should get logs", func() { g.It("Should get logs", func() {
db.SetRepo(&common.Repo{FullName: testRepo}) db.SetRepo(&common.Repo{FullName: testRepo})
db.SetLogs(testRepo, testBuild, testTask, testLogInfo) db.SetLogs(testRepo, testBuild, testTask, (bytes.NewBuffer(testLogInfo)))
buf, err := db.LogReader(testRepo, testBuild, testTask) buf, err := db.LogReader(testRepo, testBuild, testTask)
g.Assert(err).Equal(nil) g.Assert(err).Equal(nil)
logInfo, err := ioutil.ReadAll(buf) logInfo, err := ioutil.ReadAll(buf)

View file

@ -139,5 +139,6 @@ type Datastore interface {
// SetLogs inserts or updates a task logs for the // SetLogs inserts or updates a task logs for the
// named repository and build number. // named repository and build number.
SetLogs(string, int, int, []byte) error SetLogs(string, int, int, io.Reader) error
} }

View file

@ -291,7 +291,8 @@ func (m *Datastore) LogReader(_a0 string, _a1 int, _a2 int) (io.Reader, error) {
return r0, r1 return r0, r1
} }
func (m *Datastore) SetLogs(_a0 string, _a1 int, _a2 int, _a3 []byte) error {
func (m *Datastore) SetLogs(_a0 string, _a1 int, _a2 int, _a3 io.Reader) error {
ret := m.Called(_a0, _a1, _a2, _a3) ret := m.Called(_a0, _a1, _a2, _a3)
r0 := ret.Error(0) r0 := ret.Error(0)

View file

@ -103,7 +103,10 @@ func (g *GitHub) Repo(u *common.User, owner, name string) (*common.Repo, error)
repo := &common.Repo{} repo := &common.Repo{}
repo.Owner = owner repo.Owner = owner
repo.Name = name repo.Name = name
if repo_.Language != nil {
repo.Language = *repo_.Language repo.Language = *repo_.Language
}
repo.FullName = *repo_.FullName repo.FullName = *repo_.FullName
repo.Link = *repo_.HTMLURL repo.Link = *repo_.HTMLURL
repo.Private = *repo_.Private repo.Private = *repo_.Private

View file

@ -3,7 +3,7 @@ package builtin
import ( import (
"encoding/json" "encoding/json"
"io" "io"
"io/ioutil" //"io/ioutil"
"github.com/drone/drone/common" "github.com/drone/drone/common"
"github.com/drone/drone/datastore" "github.com/drone/drone/datastore"
@ -77,10 +77,11 @@ func (u *updater) SetTask(r *common.Repo, b *common.Build, t *common.Task) error
} }
func (u *updater) SetLogs(r *common.Repo, b *common.Build, t *common.Task, rc io.ReadCloser) error { func (u *updater) SetLogs(r *common.Repo, b *common.Build, t *common.Task, rc io.ReadCloser) error {
defer rc.Close() //defer rc.Close()
out, err := ioutil.ReadAll(rc) //out, err := ioutil.ReadAll(rc)
if err != nil { //if err != nil {
return err // return err
} //}
return u.store.SetLogs(r.FullName, b.Number, t.Number, out) //return u.store.SetLogs(r.FullName, b.Number, t.Number, out)
return u.store.SetLogs(r.FullName, b.Number, t.Number, rc)
} }

View file

@ -45,8 +45,8 @@ func GetLogin(c *gin.Context) {
} }
// exit if authorization fails // exit if authorization fails
// TODO(bradrydzewski) return an error message instead
if c.Writer.Status() != 200 { if c.Writer.Status() != 200 {
log.Errorf("authorization failed.")
return return
} }

View file

@ -3,7 +3,6 @@ package server
import ( import (
"encoding/json" "encoding/json"
"io" "io"
"io/ioutil"
"net" "net"
"strconv" "strconv"
@ -167,16 +166,8 @@ func PushLogs(c *gin.Context) {
bnum, _ := strconv.Atoi(c.Params.ByName("build")) bnum, _ := strconv.Atoi(c.Params.ByName("build"))
tnum, _ := strconv.Atoi(c.Params.ByName("task")) tnum, _ := strconv.Atoi(c.Params.ByName("task"))
// TODO (bradrydzewski) change this interface to accept an io.Reader const maxBuffToRead int64 = 5000000 // 5MB.
// instead of a byte array so that we can buffer the write and so that err := store.SetLogs(repo.FullName, bnum, tnum, io.LimitReader(c.Request.Body, maxBuffToRead))
// we avoid unnecessary copies of the data in memory.
logs, err := ioutil.ReadAll(io.LimitReader(c.Request.Body, 5000000)) //5MB
defer c.Request.Body.Close()
if err != nil {
c.Fail(500, err)
return
}
err = store.SetLogs(repo.FullName, bnum, tnum, logs)
if err != nil { if err != nil {
c.Fail(500, err) c.Fail(500, err)
return return

View file

@ -171,7 +171,11 @@ func PostRepo(c *gin.Context) {
owner := c.Params.ByName("owner") owner := c.Params.ByName("owner")
name := c.Params.ByName("name") name := c.Params.ByName("name")
// TODO(bradrydzewski) verify repo not exists _, err := store.Repo(owner + "/" + name)
if err == nil {
c.String(409, "Repository already exists")
return
}
// get the repository and user permissions // get the repository and user permissions
// from the remote system. // from the remote system.