fixed slack test
This commit is contained in:
parent
ea05ec725a
commit
76f76c0bf6
1 changed files with 16 additions and 29 deletions
|
@ -3,14 +3,15 @@ package notify
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
|
||||||
|
"github.com/drone/drone/shared/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
slackEndpoint = "https://%s.slack.com/services/hooks/incoming-webhook?token=%s"
|
slackEndpoint = "https://%s.slack.com/services/hooks/incoming-webhook?token=%s"
|
||||||
slackStartedMessage = "*Building* %s <%s|%s>, by %s:\n> %s"
|
slackStartedMessage = "*Building* %s, commit <%s|%s>, author %s"
|
||||||
slackSuccessMessage = "*Success* %s <%s|%s>, by %s:\n> %s"
|
slackSuccessMessage = "*Success* %s, commit <%s|%s>, author %s"
|
||||||
slackFailureMessage = "*Failed* %s <%s|%s>, by %s:\n> %s"
|
slackFailureMessage = "*Failed* %s, commit <%s|%s>, author %s"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Slack struct {
|
type Slack struct {
|
||||||
|
@ -23,7 +24,7 @@ type Slack struct {
|
||||||
Failure bool `yaml:"on_failure,omitempty"`
|
Failure bool `yaml:"on_failure,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Slack) Send(context *Context) error {
|
func (s *Slack) Send(context *model.Request) error {
|
||||||
switch {
|
switch {
|
||||||
case context.Commit.Status == "Started" && s.Started:
|
case context.Commit.Status == "Started" && s.Started:
|
||||||
return s.sendStarted(context)
|
return s.sendStarted(context)
|
||||||
|
@ -36,36 +37,21 @@ func (s *Slack) Send(context *Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBuildUrl(context *Context) string {
|
func (s *Slack) getMessage(context *model.Request, message string) 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 getMessage(context *Context, message string) string {
|
|
||||||
url := getBuildUrl(context)
|
url := getBuildUrl(context)
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(message, context.Repo.Name, url, context.Commit.ShaShort(), context.Commit.Author)
|
||||||
message,
|
|
||||||
context.Repo.Name,
|
|
||||||
url,
|
|
||||||
context.Commit.HashShort(),
|
|
||||||
context.Commit.Author,
|
|
||||||
context.Commit.Message)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Slack) sendStarted(context *Context) error {
|
func (s *Slack) sendStarted(context *model.Request) error {
|
||||||
return s.send(getMessage(context, slackStartedMessage), "warning")
|
return s.send(s.getMessage(context, slackStartedMessage), "warning")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Slack) sendSuccess(context *Context) error {
|
func (s *Slack) sendSuccess(context *model.Request) error {
|
||||||
return s.send(getMessage(context, slackSuccessMessage), "good")
|
return s.send(s.getMessage(context, slackSuccessMessage), "good")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Slack) sendFailure(context *Context) error {
|
func (s *Slack) sendFailure(context *model.Request) error {
|
||||||
return s.send(getMessage(context, slackFailureMessage), "danger")
|
return s.send(s.getMessage(context, slackFailureMessage), "danger")
|
||||||
}
|
}
|
||||||
|
|
||||||
// helper function to send HTTP requests
|
// helper function to send HTTP requests
|
||||||
|
@ -100,7 +86,8 @@ func (s *Slack) send(msg string, color string) error {
|
||||||
|
|
||||||
// send payload
|
// send payload
|
||||||
url := fmt.Sprintf(slackEndpoint, s.Team, s.Token)
|
url := fmt.Sprintf(slackEndpoint, s.Team, s.Token)
|
||||||
go sendJson(url, payload)
|
|
||||||
|
go sendJson(url, payload, nil)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue