diff --git a/plugin/notify/flowdock/client.go b/plugin/notify/flowdock/client.go new file mode 100644 index 000000000..ba0c7835c --- /dev/null +++ b/plugin/notify/flowdock/client.go @@ -0,0 +1,105 @@ +package flowdock + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" +) + +const ( + ENDPOINT = "https://api.flowdock.com/v1/messages/team_inbox/" +) + +var ( + // Required default client settings + Token = "" + Source = "" + FromAddress = "" + + // Optional default client settings + FromName = "" + ReplyTo = "" + Project = "" + Link = "" + Tags = []string{} +) + +type Client struct { + // Required + Token string + Source string + FromAddress string + Subject string + Content string + + // Optional + FromName string + ReplyTo string + Project string + Link string + Tags []string +} + +func (c *Client) Inbox(subject, content string) error { + return send(c.Token, c.Source, c.FromAddress, subject, content, c.FromName, c.ReplyTo, c.Project, c.Link, c.Tags) +} + +func Inbox(subject, content string) error { + return send(Token, Source, FromAddress, subject, content, FromName, ReplyTo, Project, Link, Tags) +} + +func send(token, source, fromAddress, subject, content, fromName, replyTo, project, link string, tags []string) error { + // Required validation + if len(token) == 0 { + return fmt.Errorf(`"Token" is required`) + } + if len(source) == 0 { + return fmt.Errorf(`"Source" is required`) + } + if len(fromAddress) == 0 { + return fmt.Errorf(`"FromAddress" is required`) + } + if len(subject) == 0 { + return fmt.Errorf(`"Subject" is required`) + } + + // Build payload + payload := map[string]interface{}{ + "source": source, + "from_address": fromAddress, + "subject": subject, + "content": content, + } + if len(fromName) > 0 { + payload["from_name"] = fromName + } + if len(replyTo) > 0 { + payload["reply_to"] = replyTo + } + if len(project) > 0 { + payload["project"] = project + } + if len(link) > 0 { + payload["link"] = link + } + if len(tags) > 0 { + payload["tags"] = tags + } + jsonPayload, err := json.Marshal(payload) + if err != nil { + return err + } + + // Send to Flowdock + resp, err := http.Post(ENDPOINT+token, "application/json", bytes.NewReader(jsonPayload)) + defer resp.Body.Close() + + if resp.StatusCode == 200 { + return nil + } else { + bodyBytes, _ := ioutil.ReadAll(resp.Body) + return fmt.Errorf("Unexpected response from Flowdock: %s %s", resp.Status, string(bodyBytes)) + } +} diff --git a/plugin/notify/flowdock.go b/plugin/notify/flowdock/flowdock.go similarity index 94% rename from plugin/notify/flowdock.go rename to plugin/notify/flowdock/flowdock.go index 27c81e7d7..e3b1d06f9 100644 --- a/plugin/notify/flowdock.go +++ b/plugin/notify/flowdock/flowdock.go @@ -1,11 +1,10 @@ -package notify +package flowdock import ( "fmt" "strings" "github.com/drone/drone/shared/model" - "github.com/stvp/flowdock" ) const ( @@ -78,7 +77,7 @@ func (f *Flowdock) sendSuccess(context *model.Request) error { // 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} + c := Client{Token: f.Token, Source: f.Source, FromName: "drone.io", FromAddress: fromAddress, Tags: tags} go c.Inbox(subject, message) return nil } diff --git a/plugin/notify/notification.go b/plugin/notify/notification.go index c836752f7..da6701ace 100644 --- a/plugin/notify/notification.go +++ b/plugin/notify/notification.go @@ -7,6 +7,7 @@ import ( "net/http" "github.com/drone/drone/plugin/notify/email" + "github.com/drone/drone/plugin/notify/flowdock" "github.com/drone/drone/plugin/notify/github" "github.com/drone/drone/plugin/notify/irc" "github.com/drone/drone/plugin/notify/katoim" @@ -22,14 +23,14 @@ type Sender interface { // for notifying a user, or group of users, // when their Build has completed. type Notification struct { - Email *email.Email `yaml:"email,omitempty"` - Webhook *webhook.Webhook `yaml:"webhook,omitempty"` - Hipchat *Hipchat `yaml:"hipchat,omitempty"` - Irc *irc.IRC `yaml:"irc,omitempty"` - Slack *Slack `yaml:"slack,omitempty"` - Gitter *Gitter `yaml:"gitter,omitempty"` - Flowdock *Flowdock `yaml:"flowdock,omitempty"` - KatoIM *katoim.KatoIM `yaml:"katoim,omitempty"` + Email *email.Email `yaml:"email,omitempty"` + Webhook *webhook.Webhook `yaml:"webhook,omitempty"` + Hipchat *Hipchat `yaml:"hipchat,omitempty"` + Irc *irc.IRC `yaml:"irc,omitempty"` + Slack *Slack `yaml:"slack,omitempty"` + Gitter *Gitter `yaml:"gitter,omitempty"` + Flowdock *flowdock.Flowdock `yaml:"flowdock,omitempty"` + KatoIM *katoim.KatoIM `yaml:"katoim,omitempty"` GitHub github.GitHub `yaml:"--"` }