webhook_dingtalk.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Copyright 2017 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "strings"
  9. "github.com/gogits/git-module"
  10. api "github.com/gogits/go-gogs-client"
  11. )
  12. const (
  13. DingtalkNotificationTitle = "Gogs Notification"
  14. )
  15. //Refer: https://open-doc.dingtalk.com/docs/doc.htm?treeId=257&articleId=105735&docType=1
  16. type DingtalkActionCard struct {
  17. Title string `json:"title"`
  18. Text string `json:"text"`
  19. HideAvatar string `json:"hideAvatar"`
  20. BtnOrientation string `json:"btnOrientation"`
  21. SingleTitle string `json:"singleTitle"`
  22. SingleURL string `json:"singleURL"`
  23. }
  24. //Refer: https://open-doc.dingtalk.com/docs/doc.htm?treeId=257&articleId=105735&docType=1
  25. type DingtalkAtObject struct {
  26. AtMobiles []string `json:"atMobiles"`
  27. IsAtAll bool `json:"isAtAll"`
  28. }
  29. //Refer: https://open-doc.dingtalk.com/docs/doc.htm?treeId=257&articleId=105735&docType=1
  30. type DingtalkPayload struct {
  31. MsgType string `json:"msgtype"`
  32. At DingtalkAtObject `json:"at"`
  33. ActionCard DingtalkActionCard `json:"actionCard"`
  34. }
  35. func (p *DingtalkPayload) JSONPayload() ([]byte, error) {
  36. data, err := json.MarshalIndent(p, "", " ")
  37. if err != nil {
  38. return []byte{}, err
  39. }
  40. return data, nil
  41. }
  42. func NewDingtalkActionCard(singleTitle, singleURL string) DingtalkActionCard {
  43. return DingtalkActionCard{
  44. Title: DingtalkNotificationTitle,
  45. SingleURL: singleURL,
  46. SingleTitle: singleTitle,
  47. }
  48. }
  49. //TODO: add content
  50. func GetDingtalkPayload(p api.Payloader, event HookEventType) (payload *DingtalkPayload, err error) {
  51. switch event {
  52. case HOOK_EVENT_CREATE:
  53. payload, err = getDingtalkCreatePayload(p.(*api.CreatePayload))
  54. case HOOK_EVENT_DELETE:
  55. payload, err = getDingtalkDeletePayload(p.(*api.DeletePayload))
  56. case HOOK_EVENT_FORK:
  57. payload, err = getDingtalkForkPayload(p.(*api.ForkPayload))
  58. case HOOK_EVENT_PUSH:
  59. payload, err = getDingtalkPushPayload(p.(*api.PushPayload))
  60. case HOOK_EVENT_ISSUES:
  61. payload, err = getDingtalkIssuesPayload(p.(*api.IssuesPayload))
  62. case HOOK_EVENT_ISSUE_COMMENT:
  63. payload, err = getDingtalkIssueCommentPayload(p.(*api.IssueCommentPayload))
  64. case HOOK_EVENT_PULL_REQUEST:
  65. payload, err = getDingtalkPullRequestPayload(p.(*api.PullRequestPayload))
  66. case HOOK_EVENT_RELEASE:
  67. payload, err = getDingtalkReleasePayload(p.(*api.ReleasePayload))
  68. }
  69. if err != nil {
  70. return nil, fmt.Errorf("event '%s': %v", event, err)
  71. }
  72. return payload, nil
  73. }
  74. func getDingtalkCreatePayload(p *api.CreatePayload) (*DingtalkPayload, error) {
  75. refName := git.RefEndName(p.Ref)
  76. refType := strings.Title(p.RefType)
  77. actionCard := NewDingtalkActionCard("View "+refType, p.Repo.HTMLURL+"/src/"+refName)
  78. actionCard.Text += "# New " + refType + " Create Event"
  79. actionCard.Text += "\n- Repo: **" + MarkdownLinkFormatter(p.Repo.HTMLURL, p.Repo.Name) + "**"
  80. actionCard.Text += "\n- New " + refType + ": **" + MarkdownLinkFormatter(p.Repo.HTMLURL+"/src/"+refName, refName) + "**"
  81. return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
  82. }
  83. func getDingtalkDeletePayload(p *api.DeletePayload) (*DingtalkPayload, error) {
  84. refName := git.RefEndName(p.Ref)
  85. refType := strings.Title(p.RefType)
  86. actionCard := NewDingtalkActionCard("View Repo", p.Repo.HTMLURL)
  87. actionCard.Text += "# " + refType + " Delete Event"
  88. actionCard.Text += "\n- Repo: **" + MarkdownLinkFormatter(p.Repo.HTMLURL, p.Repo.Name) + "**"
  89. actionCard.Text += "\n- " + refType + ": **" + refName + "**"
  90. return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
  91. }
  92. func getDingtalkForkPayload(p *api.ForkPayload) (*DingtalkPayload, error) {
  93. actionCard := NewDingtalkActionCard("View Forkee", p.Forkee.HTMLURL)
  94. actionCard.Text += "# Repo Fork Event"
  95. actionCard.Text += "\n- From Repo: **" + MarkdownLinkFormatter(p.Repo.HTMLURL, p.Repo.Name) + "**"
  96. actionCard.Text += "\n- To Repo: **" + MarkdownLinkFormatter(p.Forkee.HTMLURL, p.Forkee.FullName) + "**"
  97. return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
  98. }
  99. func getDingtalkPushPayload(p *api.PushPayload) (*DingtalkPayload, error) {
  100. refName := git.RefEndName(p.Ref)
  101. pusher := p.Pusher.FullName
  102. if pusher == "" {
  103. pusher = p.Pusher.UserName
  104. }
  105. var detail string
  106. for i, commit := range p.Commits {
  107. msg := strings.Split(commit.Message, "\n")[0]
  108. commitLink := MarkdownLinkFormatter(commit.URL, commit.ID[:7])
  109. detail += fmt.Sprintf("> %d. %s %s - %s\n", i, commitLink, commit.Author.Name, msg)
  110. }
  111. actionCard := NewDingtalkActionCard("View Changes", p.CompareURL)
  112. actionCard.Text += "# Repo Push Event"
  113. actionCard.Text += "\n- Repo: **" + MarkdownLinkFormatter(p.Repo.HTMLURL, p.Repo.Name) + "**"
  114. actionCard.Text += "\n- Ref: **" + MarkdownLinkFormatter(p.Repo.HTMLURL+"/src/"+refName, refName) + "**"
  115. actionCard.Text += "\n- Pusher: **" + pusher + "**"
  116. actionCard.Text += "\n## " + fmt.Sprintf("Total %d commits(s)", len(p.Commits))
  117. actionCard.Text += "\n" + detail
  118. return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
  119. }
  120. func getDingtalkIssuesPayload(p *api.IssuesPayload) (*DingtalkPayload, error) {
  121. issueName := fmt.Sprintf("#%d %s", p.Index, p.Issue.Title)
  122. issueURL := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Index)
  123. actionCard := NewDingtalkActionCard("View Issue", issueURL)
  124. actionCard.Text += "# Issue Event " + strings.Title(string(p.Action))
  125. actionCard.Text += "\n- Issue: **" + MarkdownLinkFormatter(issueURL, issueName) + "**"
  126. if p.Action == api.HOOK_ISSUE_ASSIGNED {
  127. actionCard.Text += "\n- New Assignee: **" + p.Issue.Assignee.UserName + "**"
  128. } else if p.Action == api.HOOK_ISSUE_MILESTONED {
  129. actionCard.Text += "\n- New Milestone: **" + p.Issue.Milestone.Title + "**"
  130. } else if p.Action == api.HOOK_ISSUE_LABEL_UPDATED {
  131. if len(p.Issue.Labels) > 0 {
  132. labels := make([]string, len(p.Issue.Labels))
  133. for i, label := range p.Issue.Labels {
  134. labels[i] = "**" + label.Name + "**"
  135. }
  136. actionCard.Text += "\n- Labels: " + strings.Join(labels, ",")
  137. } else {
  138. actionCard.Text += "\n- Labels: **empty**"
  139. }
  140. }
  141. if p.Issue.Body != "" {
  142. actionCard.Text += "\n> " + p.Issue.Body
  143. }
  144. return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
  145. }
  146. func getDingtalkIssueCommentPayload(p *api.IssueCommentPayload) (*DingtalkPayload, error) {
  147. issueName := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
  148. commentURL := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
  149. if p.Action != api.HOOK_ISSUE_COMMENT_DELETED {
  150. commentURL += "#" + CommentHashTag(p.Comment.ID)
  151. }
  152. issueURL := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
  153. actionCard := NewDingtalkActionCard("View Issue Comment", commentURL)
  154. actionCard.Text += "# Issue Comment " + strings.Title(string(p.Action))
  155. actionCard.Text += "\n- Issue: " + MarkdownLinkFormatter(issueURL, issueName)
  156. actionCard.Text += "\n- Comment content: "
  157. actionCard.Text += "\n> " + p.Comment.Body
  158. return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
  159. }
  160. func getDingtalkPullRequestPayload(p *api.PullRequestPayload) (*DingtalkPayload, error) {
  161. title := "# Pull Request " + strings.Title(string(p.Action))
  162. if p.Action == api.HOOK_ISSUE_CLOSED && p.PullRequest.HasMerged {
  163. title = "# Pull Request Merged"
  164. }
  165. pullRequestURL := fmt.Sprintf("%s/pulls/%d", p.Repository.HTMLURL, p.Index)
  166. content := "- PR: " + MarkdownLinkFormatter(pullRequestURL, fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title))
  167. if p.Action == api.HOOK_ISSUE_ASSIGNED {
  168. content += "\n- New Assignee: **" + p.PullRequest.Assignee.UserName + "**"
  169. } else if p.Action == api.HOOK_ISSUE_MILESTONED {
  170. content += "\n- New Milestone: *" + p.PullRequest.Milestone.Title + "*"
  171. } else if p.Action == api.HOOK_ISSUE_LABEL_UPDATED {
  172. labels := make([]string, len(p.PullRequest.Labels))
  173. for i, label := range p.PullRequest.Labels {
  174. labels[i] = "**" + label.Name + "**"
  175. }
  176. content += "\n- New Labels: " + strings.Join(labels, ",")
  177. }
  178. actionCard := NewDingtalkActionCard("View Pull Request", pullRequestURL)
  179. actionCard.Text += title + "\n" + content
  180. if p.Action == api.HOOK_ISSUE_OPENED || p.Action == api.HOOK_ISSUE_EDITED {
  181. actionCard.Text += "\n> " + p.PullRequest.Body
  182. }
  183. return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
  184. }
  185. func getDingtalkReleasePayload(p *api.ReleasePayload) (*DingtalkPayload, error) {
  186. releaseURL := p.Repository.HTMLURL + "/src/" + p.Release.TagName
  187. author := p.Release.Author.FullName
  188. if author == "" {
  189. author = p.Release.Author.UserName
  190. }
  191. actionCard := NewDingtalkActionCard("View Release", releaseURL)
  192. actionCard.Text += "# New Release Published"
  193. actionCard.Text += "\n- Repo: " + MarkdownLinkFormatter(p.Repository.HTMLURL, p.Repository.Name)
  194. actionCard.Text += "\n- Tag: " + MarkdownLinkFormatter(releaseURL, p.Release.TagName)
  195. actionCard.Text += "\n- Author: " + author
  196. actionCard.Text += fmt.Sprintf("\n- Draft?: %t", p.Release.Draft)
  197. actionCard.Text += fmt.Sprintf("\n- Pre Release?: %t", p.Release.Prerelease)
  198. actionCard.Text += "\n- Title: " + p.Release.Name
  199. if p.Release.Body != "" {
  200. actionCard.Text += "\n- Note: " + p.Release.Body
  201. }
  202. return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
  203. }
  204. //Format link addr and title into markdown style
  205. func MarkdownLinkFormatter(link, text string) string {
  206. return "[" + text + "](" + link + ")"
  207. }