mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-25 09:50:31 +00:00
implement initial flowdock notification
This commit is contained in:
parent
3e3367cc97
commit
ac1c7bb9fa
2 changed files with 100 additions and 5 deletions
89
pkg/plugin/notify/flowdock.go
Normal file
89
pkg/plugin/notify/flowdock.go
Normal file
|
@ -0,0 +1,89 @@
|
|||
package notify
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"net/url"
|
||||
"github.com/stvp/flowdock"
|
||||
)
|
||||
|
||||
const (
|
||||
flowdockStartedSubject = "Building %s (%s)"
|
||||
flowdockSuccessSubject = "Build: %s (%s) is SUCCESS"
|
||||
flowdockFailureSubject = "Build: %s (%s) is FAILED"
|
||||
flowdockMessage = "<h2>%s </h2>\nBuild: %s <br/>\nResult: %s <br/>\nAuthor: %s <br/>Commit: <span class=\"commit-message\">%s</span> <br/>\nRepository Url: %s"
|
||||
flowdockBuildOkEmail = "build+ok@flowdock.com"
|
||||
flowdockBuildFailEmail = "build+fail@flowdock.com";
|
||||
)
|
||||
|
||||
type Flowdock struct {
|
||||
Token string `yaml:"token,omitempty"`
|
||||
Source string `yaml:"source,omitempty"`
|
||||
Tags string `yaml:"tags,omitempty"`
|
||||
Started bool `yaml:"on_started,omitempty"`
|
||||
Success bool `yaml:"on_success,omitempty"`
|
||||
Failure bool `yaml:"on_failure,omitempty"`
|
||||
}
|
||||
|
||||
func (f *Flowdock) Send(context *Context) error {
|
||||
switch {
|
||||
case context.Commit.Status == "Started" && f.Started:
|
||||
return f.sendStarted(context)
|
||||
case context.Commit.Status == "Success" && f.Success:
|
||||
return f.sendSuccess(context)
|
||||
case context.Commit.Status == "Failure" && f.Failure:
|
||||
return f.sendFailure(context)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getBuildUrl(context *Context) string {
|
||||
branchQuery := url.Values{}
|
||||
if context.Commit.Branch != "" {
|
||||
branchQuery.Set("branch", context.Commit.Branch)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s/%s/commit/%s?%s", context.Host, context.Repo.Slug, context.Commit.Hash, branchQuery.Encode())
|
||||
}
|
||||
|
||||
func getRepoUrl(context *Context) string {
|
||||
return fmt.Sprintf("%s/%s", context.Host, context.Repo.Slug)
|
||||
}
|
||||
|
||||
func getMessage(context *Context) string {
|
||||
buildUrl := fmt.Sprintf("<a href=\"%s\"><span class=\"commit-sha\">%s</span></a>", getBuildUrl(context), context.Commit.HashShort())
|
||||
return fmt.Sprintf(flowdockMessage, context.Repo.Name, buildUrl, context.Commit.Status, context.Commit.Author, context.Commit.Message, getRepoUrl(context))
|
||||
}
|
||||
|
||||
func (f *Flowdock) sendStarted(context *Context) error {
|
||||
fromAddress := context.Commit.Author
|
||||
subject := fmt.Sprintf(flowdockStartedSubject, context.Repo.Name, context.Commit.Branch)
|
||||
msg := getMessage(context)
|
||||
tags := strings.Split(f.Tags, ",")
|
||||
return f.send(fromAddress, subject, msg, tags)
|
||||
}
|
||||
|
||||
func (f *Flowdock) sendFailure(context *Context) error {
|
||||
fromAddress := flowdockBuildFailEmail
|
||||
tags := strings.Split(f.Tags, ",")
|
||||
subject := fmt.Sprintf(flowdockFailureSubject, context.Repo.Name, context.Commit.Branch)
|
||||
msg := getMessage(context)
|
||||
return f.send(fromAddress, subject, msg, tags)
|
||||
}
|
||||
|
||||
func (f *Flowdock) sendSuccess(context *Context) error {
|
||||
fromAddress := flowdockBuildOkEmail
|
||||
tags := strings.Split(f.Tags, ",")
|
||||
subject := fmt.Sprintf(flowdockSuccessSubject, context.Repo.Name, context.Commit.Branch)
|
||||
msg := getMessage(context)
|
||||
return f.send(fromAddress, subject, msg, tags)
|
||||
}
|
||||
|
||||
// helper function to send Flowdock requests
|
||||
func (f *Flowdock) send(fromAddress, subject, message string, tags []string) error {
|
||||
|
||||
c := flowdock.Client{Token: f.Token, Source: f.Source, FromName: "drone.io", FromAddress: fromAddress, Tags: tags}
|
||||
|
||||
return c.Inbox(subject, message)
|
||||
}
|
|
@ -28,11 +28,12 @@ type Sender interface {
|
|||
// for notifying a user, or group of users,
|
||||
// when their Build has completed.
|
||||
type Notification struct {
|
||||
Email *Email `yaml:"email,omitempty"`
|
||||
Webhook *Webhook `yaml:"webhook,omitempty"`
|
||||
Hipchat *Hipchat `yaml:"hipchat,omitempty"`
|
||||
Irc *IRC `yaml:"irc,omitempty"`
|
||||
Slack *Slack `yaml:"slack,omitempty"`
|
||||
Email *Email `yaml:"email,omitempty"`
|
||||
Webhook *Webhook `yaml:"webhook,omitempty"`
|
||||
Hipchat *Hipchat `yaml:"hipchat,omitempty"`
|
||||
Irc *IRC `yaml:"irc,omitempty"`
|
||||
Slack *Slack `yaml:"slack,omitempty"`
|
||||
Flowdock *Flowdock `yaml:"flowdock,omitempty"`
|
||||
}
|
||||
|
||||
func (n *Notification) Send(context *Context) error {
|
||||
|
@ -61,5 +62,10 @@ func (n *Notification) Send(context *Context) error {
|
|||
n.Slack.Send(context)
|
||||
}
|
||||
|
||||
// send flowdock notifications
|
||||
if n.Flowdock != nil {
|
||||
n.Flowdock.Send(context)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue