github.com/unclejack/drone@v0.2.1-0.20140918182345-831b034aa33b/pkg/plugin/notify/slack.go (about)

     1  package notify
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/url"
     7  )
     8  
     9  const (
    10  	slackEndpoint       = "https://%s.slack.com/services/hooks/incoming-webhook?token=%s"
    11  	slackStartedMessage = "*Building* %s <%s|%s>, by %s:\n> %s"
    12  	slackSuccessMessage = "*Success* %s <%s|%s>, by %s:\n> %s"
    13  	slackFailureMessage = "*Failed* %s <%s|%s>, by %s:\n> %s"
    14  )
    15  
    16  type Slack struct {
    17  	Team     string `yaml:"team,omitempty"`
    18  	Channel  string `yaml:"channel,omitempty"`
    19  	Username string `yaml:"username,omitempty"`
    20  	Token    string `yaml:"token,omitempty"`
    21  	Started  bool   `yaml:"on_started,omitempty"`
    22  	Success  bool   `yaml:"on_success,omitempty"`
    23  	Failure  bool   `yaml:"on_failure,omitempty"`
    24  }
    25  
    26  func (s *Slack) Send(context *Context) error {
    27  	switch {
    28  	case context.Commit.Status == "Started" && s.Started:
    29  		return s.sendStarted(context)
    30  	case context.Commit.Status == "Success" && s.Success:
    31  		return s.sendSuccess(context)
    32  	case context.Commit.Status == "Failure" && s.Failure:
    33  		return s.sendFailure(context)
    34  	}
    35  
    36  	return nil
    37  }
    38  
    39  func getBuildUrl(context *Context) string {
    40  	branchQuery := url.Values{}
    41  	if context.Commit.Branch != "" {
    42  		branchQuery.Set("branch", context.Commit.Branch)
    43  	}
    44  
    45  	return fmt.Sprintf("%s/%s/commit/%s?%s", context.Host, context.Repo.Slug, context.Commit.Hash, branchQuery.Encode())
    46  }
    47  
    48  func getMessage(context *Context, message string) string {
    49  	url := getBuildUrl(context)
    50  	return fmt.Sprintf(
    51  		message,
    52  		context.Repo.Name,
    53  		url,
    54  		context.Commit.HashShort(),
    55  		context.Commit.Author,
    56  		context.Commit.Message)
    57  }
    58  
    59  func (s *Slack) sendStarted(context *Context) error {
    60  	return s.send(getMessage(context, slackStartedMessage))
    61  }
    62  
    63  func (s *Slack) sendSuccess(context *Context) error {
    64  	return s.send(getMessage(context, slackSuccessMessage))
    65  }
    66  
    67  func (s *Slack) sendFailure(context *Context) error {
    68  	return s.send(getMessage(context, slackFailureMessage))
    69  }
    70  
    71  // helper function to send HTTP requests
    72  func (s *Slack) send(msg string) error {
    73  	// data will get posted in this format
    74  	data := struct {
    75  		Channel  string `json:"channel"`
    76  		Username string `json:"username"`
    77  		Text     string `json:"text"`
    78  	}{s.Channel, s.Username, msg}
    79  
    80  	// data json encoded
    81  	payload, err := json.Marshal(data)
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	// send payload
    87  	url := fmt.Sprintf(slackEndpoint, s.Team, s.Token)
    88  	go sendJson(url, payload)
    89  
    90  	return nil
    91  }