mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-11-27 04:11:03 +00:00
updated notification plugins to use model.Request
This commit is contained in:
parent
ecff154c56
commit
91f7a63d21
7 changed files with 31 additions and 46 deletions
|
@ -1,6 +1,8 @@
|
|||
package notify
|
||||
|
||||
//import "github.com/drone/drone/pkg/mail"
|
||||
import (
|
||||
"github.com/drone/drone/shared/model"
|
||||
)
|
||||
|
||||
type Email struct {
|
||||
Recipients []string `yaml:"recipients,omitempty"`
|
||||
|
@ -10,7 +12,7 @@ type Email struct {
|
|||
|
||||
// Send will send an email, either success or failure,
|
||||
// based on the Commit Status.
|
||||
func (e *Email) Send(context *Context) error {
|
||||
func (e *Email) Send(context *model.Request) error {
|
||||
switch {
|
||||
case context.Commit.Status == "Success" && e.Success != "never":
|
||||
return e.sendSuccess(context)
|
||||
|
@ -23,7 +25,7 @@ func (e *Email) Send(context *Context) error {
|
|||
|
||||
// sendFailure sends email notifications to the list of
|
||||
// recipients indicating the build failed.
|
||||
func (e *Email) sendFailure(context *Context) error {
|
||||
func (e *Email) sendFailure(context *model.Request) error {
|
||||
// loop through and email recipients
|
||||
//for _, email := range e.Recipients {
|
||||
//if err := mail.SendFailure(context.Repo.Name, context.Commit.HashShort(), email, context); err != nil {
|
||||
|
@ -35,7 +37,7 @@ func (e *Email) sendFailure(context *Context) error {
|
|||
|
||||
// sendSuccess sends email notifications to the list of
|
||||
// recipients indicating the build was a success.
|
||||
func (e *Email) sendSuccess(context *Context) error {
|
||||
func (e *Email) sendSuccess(context *model.Request) error {
|
||||
// loop through and email recipients
|
||||
//for _, email := range e.Recipients {
|
||||
// if err := mail.SendSuccess(context.Repo.Name, context.Commit.HashShort(), email, context); err != nil {
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
|
||||
"github.com/andybons/hipchat"
|
||||
"github.com/drone/drone/shared/model"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -20,7 +21,7 @@ type Hipchat struct {
|
|||
Failure bool `yaml:"on_failure,omitempty"`
|
||||
}
|
||||
|
||||
func (h *Hipchat) Send(context *Context) error {
|
||||
func (h *Hipchat) Send(context *model.Request) error {
|
||||
switch {
|
||||
case context.Commit.Status == "Started" && h.Started:
|
||||
return h.sendStarted(context)
|
||||
|
@ -33,17 +34,17 @@ func (h *Hipchat) Send(context *Context) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (h *Hipchat) sendStarted(context *Context) error {
|
||||
func (h *Hipchat) sendStarted(context *model.Request) error {
|
||||
msg := fmt.Sprintf(startedMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author)
|
||||
return h.send(hipchat.ColorYellow, hipchat.FormatHTML, msg)
|
||||
}
|
||||
|
||||
func (h *Hipchat) sendFailure(context *Context) error {
|
||||
func (h *Hipchat) sendFailure(context *model.Request) error {
|
||||
msg := fmt.Sprintf(failureMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author)
|
||||
return h.send(hipchat.ColorRed, hipchat.FormatHTML, msg)
|
||||
}
|
||||
|
||||
func (h *Hipchat) sendSuccess(context *Context) error {
|
||||
func (h *Hipchat) sendSuccess(context *model.Request) error {
|
||||
msg := fmt.Sprintf(successMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author)
|
||||
return h.send(hipchat.ColorGreen, hipchat.FormatHTML, msg)
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package notify
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/drone/drone/shared/model"
|
||||
irc "github.com/fluffle/goirc/client"
|
||||
)
|
||||
|
||||
|
@ -39,7 +40,7 @@ func (i *IRC) Connect() {
|
|||
i.Client = c
|
||||
}
|
||||
|
||||
func (i *IRC) Send(context *Context) error {
|
||||
func (i *IRC) Send(context *model.Request) error {
|
||||
switch {
|
||||
case context.Commit.Status == "Started" && i.Started:
|
||||
return i.sendStarted(context)
|
||||
|
@ -51,13 +52,13 @@ func (i *IRC) Send(context *Context) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (i *IRC) sendStarted(context *Context) error {
|
||||
func (i *IRC) sendStarted(context *model.Request) error {
|
||||
msg := fmt.Sprintf(ircStartedMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author)
|
||||
i.send(i.Channel, msg)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *IRC) sendFailure(context *Context) error {
|
||||
func (i *IRC) sendFailure(context *model.Request) error {
|
||||
msg := fmt.Sprintf(ircFailureMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author)
|
||||
i.send(i.Channel, msg)
|
||||
if i.ClientStarted {
|
||||
|
@ -66,7 +67,7 @@ func (i *IRC) sendFailure(context *Context) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (i *IRC) sendSuccess(context *Context) error {
|
||||
func (i *IRC) sendSuccess(context *model.Request) error {
|
||||
msg := fmt.Sprintf(ircSuccessMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author)
|
||||
i.send(i.Channel, msg)
|
||||
if i.ClientStarted {
|
||||
|
|
|
@ -4,24 +4,8 @@ import (
|
|||
"github.com/drone/drone/shared/model"
|
||||
)
|
||||
|
||||
// Context represents the context of an
|
||||
// in-progress build request.
|
||||
type Context struct {
|
||||
// Global settings
|
||||
Host string
|
||||
|
||||
// User that owns the repository
|
||||
User *model.User
|
||||
|
||||
// Repository being built.
|
||||
Repo *model.Repo
|
||||
|
||||
// Commit being built
|
||||
Commit *model.Commit
|
||||
}
|
||||
|
||||
type Sender interface {
|
||||
Send(context *Context) error
|
||||
Send(context *model.Request) error
|
||||
}
|
||||
|
||||
// Notification stores the configuration details
|
||||
|
@ -35,12 +19,7 @@ type Notification struct {
|
|||
Slack *Slack `yaml:"slack,omitempty"`
|
||||
}
|
||||
|
||||
func (n *Notification) Send(context *Context) error {
|
||||
// send email notifications
|
||||
if n.Email != nil {
|
||||
n.Email.Send(context)
|
||||
}
|
||||
|
||||
func (n *Notification) Send(context *model.Request) error {
|
||||
// send email notifications
|
||||
if n.Webhook != nil {
|
||||
n.Webhook.Send(context)
|
||||
|
|
|
@ -3,6 +3,8 @@ package notify
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/drone/drone/shared/model"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -22,7 +24,7 @@ type Slack struct {
|
|||
Failure bool `yaml:"on_failure,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Slack) Send(context *Context) error {
|
||||
func (s *Slack) Send(context *model.Request) error {
|
||||
switch {
|
||||
case context.Commit.Status == "Started" && s.Started:
|
||||
return s.sendStarted(context)
|
||||
|
@ -35,24 +37,24 @@ func (s *Slack) Send(context *Context) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func getBuildUrl(context *Context) string {
|
||||
return fmt.Sprintf("%s/%s/%s/%s/branch/%s/commit/%s", context.Host, context.Repo.Host, context.Repo.Owner, context.Repo.Name, context.Commit.Branch, context.Commit.Sha)
|
||||
func getBuildUrl(context *model.Request) string {
|
||||
return fmt.Sprintf("%s/%s/%s/%s/%s/%s", context.Host, context.Repo.Host, context.Repo.Owner, context.Repo.Name, context.Commit.Branch, context.Commit.Sha)
|
||||
}
|
||||
|
||||
func getMessage(context *Context, message string) string {
|
||||
func getMessage(context *model.Request, message string) string {
|
||||
url := getBuildUrl(context)
|
||||
return fmt.Sprintf(message, context.Repo.Name, url, context.Commit.ShaShort(), context.Commit.Author)
|
||||
}
|
||||
|
||||
func (s *Slack) sendStarted(context *Context) error {
|
||||
func (s *Slack) sendStarted(context *model.Request) error {
|
||||
return s.send(getMessage(context, slackStartedMessage))
|
||||
}
|
||||
|
||||
func (s *Slack) sendSuccess(context *Context) error {
|
||||
func (s *Slack) sendSuccess(context *model.Request) error {
|
||||
return s.send(getMessage(context, slackSuccessMessage))
|
||||
}
|
||||
|
||||
func (s *Slack) sendFailure(context *Context) error {
|
||||
func (s *Slack) sendFailure(context *model.Request) error {
|
||||
return s.send(getMessage(context, slackFailureMessage))
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ import (
|
|||
)
|
||||
|
||||
func Test_getBuildUrl(t *testing.T) {
|
||||
c := &Context{
|
||||
c := &model.Request{
|
||||
Host: "http://examplehost.com",
|
||||
Repo: &model.Repo{
|
||||
Host: "examplegit.com",
|
||||
|
@ -18,7 +18,7 @@ func Test_getBuildUrl(t *testing.T) {
|
|||
Branch: "example",
|
||||
},
|
||||
}
|
||||
expected := "http://examplehost.com/examplegit.com/owner/repo/branch/example/commit/abc"
|
||||
expected := "http://examplehost.com/examplegit.com/owner/repo/example/abc"
|
||||
output := getBuildUrl(c)
|
||||
|
||||
if output != expected {
|
||||
|
|
|
@ -14,7 +14,7 @@ type Webhook struct {
|
|||
Failure bool `yaml:"on_failure,omitempty"`
|
||||
}
|
||||
|
||||
func (w *Webhook) Send(context *Context) error {
|
||||
func (w *Webhook) Send(context *model.Request) error {
|
||||
switch {
|
||||
case context.Commit.Status == "Success" && w.Success:
|
||||
return w.send(context)
|
||||
|
@ -26,7 +26,7 @@ func (w *Webhook) Send(context *Context) error {
|
|||
}
|
||||
|
||||
// helper function to send HTTP requests
|
||||
func (w *Webhook) send(context *Context) error {
|
||||
func (w *Webhook) send(context *model.Request) error {
|
||||
// data will get posted in this format
|
||||
data := struct {
|
||||
Owner *model.User `json:"owner"`
|
||||
|
|
Loading…
Reference in a new issue