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"
|
|
|
|
|
2015-05-17 18:10:43 +00:00
|
|
|
"github.com/drone/drone/pkg/bus"
|
2015-05-17 18:45:09 +00:00
|
|
|
"github.com/drone/drone/pkg/remote"
|
2015-05-17 18:10:43 +00:00
|
|
|
"github.com/drone/drone/pkg/store"
|
2015-06-19 00:36:52 +00:00
|
|
|
"github.com/drone/drone/pkg/types"
|
2015-05-05 08:04:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Updater interface {
|
2015-06-19 00:36:52 +00:00
|
|
|
SetCommit(*types.User, *types.Repo, *types.Commit) error
|
|
|
|
SetJob(*types.Repo, *types.Commit, *types.Job) error
|
|
|
|
SetLogs(*types.Repo, *types.Commit, *types.Job, 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-17 18:10:43 +00:00
|
|
|
func NewUpdater(bus bus.Bus, store store.Store, rem remote.Remote) Updater {
|
2015-05-10 03:46:32 +00:00
|
|
|
return &updater{bus, store, rem}
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type updater struct {
|
2015-05-17 18:10:43 +00:00
|
|
|
bus bus.Bus
|
|
|
|
store store.Store
|
2015-05-10 03:46:32 +00:00
|
|
|
remote remote.Remote
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 00:36:52 +00:00
|
|
|
func (u *updater) SetCommit(user *types.User, r *types.Repo, c *types.Commit) error {
|
2015-05-11 07:45:31 +00:00
|
|
|
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-19 04:53:34 +00:00
|
|
|
// we need this because builds coming from
|
|
|
|
// a remote agent won't have the embedded
|
|
|
|
// build list. we should probably just rethink
|
|
|
|
// the messaging instead of this hack.
|
|
|
|
if c.Builds == nil || len(c.Builds) == 0 {
|
2015-06-19 00:36:52 +00:00
|
|
|
c.Builds, _ = u.store.JobList(c)
|
2015-05-19 04:53:34 +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-17 18:10:43 +00:00
|
|
|
u.bus.Send(&bus.Event{
|
2015-05-06 02:46:26 +00:00
|
|
|
Name: r.FullName,
|
2015-05-17 18:10:43 +00:00
|
|
|
Kind: bus.EventRepo,
|
2015-05-06 02:46:26 +00:00
|
|
|
Msg: msg,
|
2015-05-05 08:04:20 +00:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-19 00:36:52 +00:00
|
|
|
func (u *updater) SetJob(r *types.Repo, c *types.Commit, j *types.Job) error {
|
|
|
|
err := u.store.SetJob(j)
|
2015-05-05 08:04:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-06 02:46:26 +00:00
|
|
|
|
2015-05-19 04:53:34 +00:00
|
|
|
// we need this because builds coming from
|
|
|
|
// a remote agent won't have the embedded
|
|
|
|
// build list. we should probably just rethink
|
|
|
|
// the messaging instead of this hack.
|
|
|
|
if c.Builds == nil || len(c.Builds) == 0 {
|
2015-06-19 00:36:52 +00:00
|
|
|
c.Builds, _ = u.store.JobList(c)
|
2015-05-19 04:53:34 +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-17 18:10:43 +00:00
|
|
|
u.bus.Send(&bus.Event{
|
2015-05-06 02:46:26 +00:00
|
|
|
Name: r.FullName,
|
2015-05-17 18:10:43 +00:00
|
|
|
Kind: bus.EventRepo,
|
2015-05-06 02:46:26 +00:00
|
|
|
Msg: msg,
|
2015-05-05 08:04:20 +00:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-19 00:36:52 +00:00
|
|
|
func (u *updater) SetLogs(r *types.Repo, c *types.Commit, j *types.Job, rc io.ReadCloser) error {
|
|
|
|
path := fmt.Sprintf("/logs/%s/%v/%v", r.FullName, c.Sequence, j.Number)
|
2015-05-11 07:45:31 +00:00
|
|
|
return u.store.SetBlobReader(path, rc)
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|