2015-05-05 08:04:20 +00:00
|
|
|
package builtin
|
|
|
|
|
|
|
|
import (
|
2015-05-06 02:46:26 +00:00
|
|
|
"encoding/json"
|
2015-05-11 07:45:31 +00:00
|
|
|
"fmt"
|
2015-05-05 08:04:20 +00:00
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/drone/drone/common"
|
|
|
|
"github.com/drone/drone/datastore"
|
|
|
|
"github.com/drone/drone/eventbus"
|
2015-05-10 03:46:32 +00:00
|
|
|
"github.com/drone/drone/remote"
|
2015-05-05 08:04:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Updater interface {
|
2015-05-11 07:45:31 +00:00
|
|
|
SetCommit(*common.User, *common.Repo, *common.Commit) error
|
|
|
|
SetBuild(*common.Repo, *common.Commit, *common.Build) error
|
|
|
|
SetLogs(*common.Repo, *common.Commit, *common.Build, io.ReadCloser) error
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewUpdater returns an implementation of the Updater interface
|
|
|
|
// that directly modifies the database and sends messages to the bus.
|
2015-05-10 03:46:32 +00:00
|
|
|
func NewUpdater(bus eventbus.Bus, store datastore.Datastore, rem remote.Remote) Updater {
|
|
|
|
return &updater{bus, store, rem}
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type updater struct {
|
2015-05-10 03:46:32 +00:00
|
|
|
bus eventbus.Bus
|
|
|
|
store datastore.Datastore
|
|
|
|
remote remote.Remote
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
func (u *updater) SetCommit(user *common.User, r *common.Repo, c *common.Commit) error {
|
|
|
|
err := u.store.SetCommit(c)
|
2015-05-05 08:04:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-06 02:46:26 +00:00
|
|
|
|
2015-05-13 06:58:30 +00:00
|
|
|
err = u.remote.Status(user, r, c)
|
|
|
|
if err != nil {
|
|
|
|
// log err
|
|
|
|
}
|
2015-05-10 03:46:32 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
msg, err := json.Marshal(c)
|
2015-05-06 02:46:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-05 08:04:20 +00:00
|
|
|
u.bus.Send(&eventbus.Event{
|
2015-05-06 02:46:26 +00:00
|
|
|
Name: r.FullName,
|
|
|
|
Kind: eventbus.EventRepo,
|
|
|
|
Msg: msg,
|
2015-05-05 08:04:20 +00:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
func (u *updater) SetBuild(r *common.Repo, c *common.Commit, b *common.Build) error {
|
|
|
|
err := u.store.SetBuild(b)
|
2015-05-05 08:04:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-06 02:46:26 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
msg, err := json.Marshal(c)
|
2015-05-06 02:46:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-05 08:04:20 +00:00
|
|
|
u.bus.Send(&eventbus.Event{
|
2015-05-06 02:46:26 +00:00
|
|
|
Name: r.FullName,
|
|
|
|
Kind: eventbus.EventRepo,
|
|
|
|
Msg: msg,
|
2015-05-05 08:04:20 +00:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
func (u *updater) SetLogs(r *common.Repo, c *common.Commit, b *common.Build, rc io.ReadCloser) error {
|
|
|
|
path := fmt.Sprintf("/logs/%s/%v/%v", r.FullName, c.Sequence, b.Sequence)
|
|
|
|
return u.store.SetBlobReader(path, rc)
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|