Merge pull request #774 from movableink/notify-after-failure-after-success

Add support for after_success and after_failure to email notifications
This commit is contained in:
Brad Rydzewski 2014-12-30 10:47:49 -08:00
commit b3070a0d22
5 changed files with 50 additions and 3 deletions

View file

@ -12,9 +12,10 @@ import (
) )
const ( const (
NotifyAlways = "always" // always send email notification NotifyAlways = "always" // always send email notification
NotifyNever = "never" // never send email notifications NotifyNever = "never" // never send email notifications
NotifyAuthor = "author" // only send email notifications to the author NotifyAuthor = "author" // only send email notifications to the author
NotifyAfterChange = "change" // only if the previous commit has a different status
NotifyTrue = "true" // alias for NotifyTrue NotifyTrue = "true" // alias for NotifyTrue
NotifyFalse = "false" // alias for NotifyFalse NotifyFalse = "false" // alias for NotifyFalse
@ -70,6 +71,11 @@ func (e *Email) sendFailure(context *model.Request) error {
switch e.Failure { switch e.Failure {
case NotifyFalse, NotifyNever, NotifyOff: case NotifyFalse, NotifyNever, NotifyOff:
return nil return nil
// if the last commit in this branch was a different status, notify
case NotifyAfterChange:
if context.Prior.Status == context.Commit.Status {
return nil
}
// if configured to email the author, replace // if configured to email the author, replace
// the recipiends with the commit author email. // the recipiends with the commit author email.
case NotifyBlame, NotifyAuthor: case NotifyBlame, NotifyAuthor:
@ -103,6 +109,11 @@ func (e *Email) sendSuccess(context *model.Request) error {
switch e.Success { switch e.Success {
case NotifyFalse, NotifyNever, NotifyOff: case NotifyFalse, NotifyNever, NotifyOff:
return nil return nil
// if the last commit in this branch was a different status, notify
case NotifyAfterChange:
if context.Prior.Status == context.Commit.Status {
return nil
}
// if configured to email the author, replace // if configured to email the author, replace
// the recipiends with the commit author email. // the recipiends with the commit author email.
case NotifyBlame, NotifyAuthor: case NotifyBlame, NotifyAuthor:

View file

@ -31,6 +31,10 @@ type Commitstore interface {
// from the datastore accessible to the specified user. // from the datastore accessible to the specified user.
GetCommitListActivity(user *model.User) ([]*model.CommitRepo, error) GetCommitListActivity(user *model.User) ([]*model.CommitRepo, error)
// GetCommitPrior retrieves the latest commit
// from the datastore for the specified repository and branch.
GetCommitPrior(commit *model.Commit) (*model.Commit, error)
// PostCommit saves a commit in the datastore. // PostCommit saves a commit in the datastore.
PostCommit(commit *model.Commit) error PostCommit(commit *model.Commit) error
@ -82,6 +86,12 @@ func GetCommitListActivity(c context.Context, user *model.User) ([]*model.Commit
return FromContext(c).GetCommitListActivity(user) return FromContext(c).GetCommitListActivity(user)
} }
// GetCommitPrior retrieves the latest commit
// from the datastore for the specified repository and branch.
func GetCommitPrior(c context.Context, commit *model.Commit) (*model.Commit, error) {
return FromContext(c).GetCommitPrior(commit)
}
// PostCommit saves a commit in the datastore. // PostCommit saves a commit in the datastore.
func PostCommit(c context.Context, commit *model.Commit) error { func PostCommit(c context.Context, commit *model.Commit) error {
return FromContext(c).PostCommit(commit) return FromContext(c).PostCommit(commit)

View file

@ -64,6 +64,14 @@ func (db *Commitstore) GetCommitListActivity(user *model.User) ([]*model.CommitR
return commits, err return commits, err
} }
// GetCommitPrior retrieves the latest commit
// from the datastore for the specified repository and branch.
func (db *Commitstore) GetCommitPrior(oldCommit *model.Commit) (*model.Commit, error) {
var commit = new(model.Commit)
var err = meddler.QueryRow(db, commit, rebind(commitPriorQuery), oldCommit.RepoID, oldCommit.Branch, oldCommit.ID)
return commit, err
}
// PostCommit saves a commit in the datastore. // PostCommit saves a commit in the datastore.
func (db *Commitstore) PostCommit(commit *model.Commit) error { func (db *Commitstore) PostCommit(commit *model.Commit) error {
if commit.Created == 0 { if commit.Created == 0 {
@ -170,6 +178,17 @@ ORDER BY commit_id DESC
LIMIT 1 LIMIT 1
` `
// SQL query to retrieve the prior Commit (by commit_created) in the same branch and repo as the specified Commit.
const commitPriorQuery = `
SELECT *
FROM commits
WHERE repo_id = ?
AND commit_branch = ?
AND commit_id < ?
ORDER BY commit_id DESC
LIMIT 1
`
// SQL statement to cancel all running Commits. // SQL statement to cancel all running Commits.
const commitKillStmt = ` const commitKillStmt = `
UPDATE commits SET commit_status = 'Killed' UPDATE commits SET commit_status = 'Killed'

View file

@ -120,6 +120,8 @@ func (d *Docker) Do(c context.Context, r *worker.Work) {
Depth: git.GitDepth(script.Git), Depth: git.GitDepth(script.Git),
} }
priorCommit, _ := datastore.GetCommitPrior(c, r.Commit)
// send all "started" notifications // send all "started" notifications
if script.Notifications == nil { if script.Notifications == nil {
script.Notifications = &notify.Notification{} script.Notifications = &notify.Notification{}
@ -129,6 +131,7 @@ func (d *Docker) Do(c context.Context, r *worker.Work) {
Repo: r.Repo, Repo: r.Repo,
Commit: r.Commit, Commit: r.Commit,
Host: r.Host, Host: r.Host,
Prior: priorCommit,
}) })
// create an instance of the Docker builder // create an instance of the Docker builder
@ -168,11 +171,14 @@ func (d *Docker) Do(c context.Context, r *worker.Work) {
// notify all listeners that the build is finished // notify all listeners that the build is finished
commitc.Publish(r) commitc.Publish(r)
priorCommit, _ = datastore.GetCommitPrior(c, r.Commit)
// send all "finished" notifications // send all "finished" notifications
script.Notifications.Send(&model.Request{ script.Notifications.Send(&model.Request{
User: r.User, User: r.User,
Repo: r.Repo, Repo: r.Repo,
Commit: r.Commit, Commit: r.Commit,
Host: r.Host, Host: r.Host,
Prior: priorCommit,
}) })
} }

View file

@ -5,4 +5,5 @@ type Request struct {
User *User `json:"-"` User *User `json:"-"`
Repo *Repo `json:"repo"` Repo *Repo `json:"repo"`
Commit *Commit `json:"commit"` Commit *Commit `json:"commit"`
Prior *Commit `json:"prior_commit"`
} }