diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 0ca983eb..6ded8527 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -174,6 +174,10 @@ "Comment": "1.5.0-168-g2abea07", "Rev": "2abea075ec076abf0572d29bdb28ae7da64cfb7a" }, + { + "ImportPath": "github.com/stvp/flowdock", + "Rev": "50362abeabebf40b0f56a326d62f17e61a59302b" + }, { "ImportPath": "launchpad.net/goyaml", "Comment": "51", diff --git a/Godeps/_workspace/src/github.com/stvp/flowdock/README.md b/Godeps/_workspace/src/github.com/stvp/flowdock/README.md new file mode 100644 index 00000000..16da2b06 --- /dev/null +++ b/Godeps/_workspace/src/github.com/stvp/flowdock/README.md @@ -0,0 +1,53 @@ +flowdock +======== + +A Flowdock client for Golang. See the [Flowdock API docs][api_docs] for more +information on the individual attributes. + +[Go API Documentation][godocs] + +Examples +-------- + +You can set global defaults and use `flowdock.Inbox()` directly: + +```go +import ( + "github.com/stvp/flowdock" +) + +func main() { + flowdock.Token = "732da505d0284d5b1d909b1a32426345" + flowdock.Source = "My Cool App" + flowdock.FromAddress = "cool@dudes.com" + // See API docs for more variables + + go flowdock.Inbox("My subject", "My content goes here.") +} +``` + +Or you can create a `flowdock.Client` to use different Flowdock message +settings in the same app: + +```go +import ( + "github.com/stvp/flowdock" +) + +func main() { + client := flowdock.Client{ + Token: "732da505d0284d5b1d909b1a32426345", + Source: "App A", + FromAddress: "email@stovepipestudios.com", + FromName: "Client A", + ReplyTo: "app_a@stovepipestudios.com", + Tags: []string{"app_a"}, + } + + go client.Inbox("Subject", "Content") +} +``` + +[api_docs]: https://www.flowdock.com/api/team-inbox +[godocs]: http://godoc.org/github.com/stvp/flowdock + diff --git a/Godeps/_workspace/src/github.com/stvp/flowdock/flowdock.go b/Godeps/_workspace/src/github.com/stvp/flowdock/flowdock.go new file mode 100644 index 00000000..ba0c7835 --- /dev/null +++ b/Godeps/_workspace/src/github.com/stvp/flowdock/flowdock.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/pkg/plugin/notify/flowdock.go b/pkg/plugin/notify/flowdock.go new file mode 100644 index 00000000..5837fc3b --- /dev/null +++ b/pkg/plugin/notify/flowdock.go @@ -0,0 +1,90 @@ +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 = "