func getSlackCreatePayload(p *api.CreatePayload, slack *SlackMeta) (*SlackPayload, error) { // created tag/branch refName := git.RefEndName(p.Ref) repoLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.Name) refLink := SlackLinkFormatter(p.Repo.HTMLURL+"/src/"+refName, refName) text := fmt.Sprintf("[%s:%s] %s created by %s", repoLink, refLink, p.RefType, p.Sender.UserName) return &SlackPayload{ Channel: slack.Channel, Text: text, Username: slack.Username, IconURL: slack.IconURL, }, nil }
func getSlackPushPayload(p *api.PushPayload, slack *SlackMeta) (*SlackPayload, error) { // n new commits var ( branchName = git.RefEndName(p.Ref) commitDesc string commitString string ) if len(p.Commits) == 1 { commitDesc = "1 new commit" } else { commitDesc = fmt.Sprintf("%d new commits", len(p.Commits)) } if len(p.CompareURL) > 0 { commitString = SlackLinkFormatter(p.CompareURL, commitDesc) } else { commitString = commitDesc } repoLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.Name) branchLink := SlackLinkFormatter(p.Repo.HTMLURL+"/src/"+branchName, branchName) text := fmt.Sprintf("[%s:%s] %s pushed by %s", repoLink, branchLink, commitString, p.Pusher.UserName) var attachmentText string // for each commit, generate attachment text for i, commit := range p.Commits { attachmentText += fmt.Sprintf("%s: %s - %s", SlackLinkFormatter(commit.URL, commit.ID[:7]), SlackShortTextFormatter(commit.Message), SlackTextFormatter(commit.Author.Name)) // add linebreak to each commit but the last if i < len(p.Commits)-1 { attachmentText += "\n" } } return &SlackPayload{ Channel: slack.Channel, Text: text, Username: slack.Username, IconURL: slack.IconURL, Attachments: []SlackAttachment{{ Color: slack.Color, Text: attachmentText, }}, }, nil }