webhook_dingtalk.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 database
  5. import (
  6. "fmt"
  7. "strings"
  8. jsoniter "github.com/json-iterator/go"
  9. "github.com/pkg/errors"
  10. "github.com/gogs/git-module"
  11. api "github.com/gogs/go-gogs-client"
  12. )
  13. const (
  14. DingtalkNotificationTitle = "Gogs Notification"
  15. )
  16. // Refer: https://open-doc.dingtalk.com/docs/doc.htm?treeId=257&articleId=105735&docType=1
  17. type DingtalkActionCard struct {
  18. Title string `json:"title"`
  19. Text string `json:"text"`
  20. HideAvatar string `json:"hideAvatar"`
  21. BtnOrientation string `json:"btnOrientation"`
  22. SingleTitle string `json:"singleTitle"`
  23. SingleURL string `json:"singleURL"`
  24. }
  25. // Refer: https://open-doc.dingtalk.com/docs/doc.htm?treeId=257&articleId=105735&docType=1
  26. type DingtalkAtObject struct {
  27. AtMobiles []string `json:"atMobiles"`
  28. IsAtAll bool `json:"isAtAll"`
  29. }
  30. // Refer: https://open-doc.dingtalk.com/docs/doc.htm?treeId=257&articleId=105735&docType=1
  31. type DingtalkPayload struct {
  32. MsgType string `json:"msgtype"`
  33. At DingtalkAtObject `json:"at"`
  34. ActionCard DingtalkActionCard `json:"actionCard"`
  35. }
  36. func (p *DingtalkPayload) JSONPayload() ([]byte, error) {
  37. data, err := jsoniter.MarshalIndent(p, "", " ")
  38. if err != nil {
  39. return []byte{}, err
  40. }
  41. return data, nil
  42. }
  43. func NewDingtalkActionCard(singleTitle, singleURL string) DingtalkActionCard {
  44. return DingtalkActionCard{
  45. Title: DingtalkNotificationTitle,
  46. SingleURL: singleURL,
  47. SingleTitle: singleTitle,
  48. }
  49. }
  50. // TODO: add content
  51. func GetDingtalkPayload(p api.Payloader, event HookEventType) (payload *DingtalkPayload, err error) {
  52. switch event {
  53. case HookEventTypeCreate:
  54. payload = getDingtalkCreatePayload(p.(*api.CreatePayload))
  55. case HookEventTypeDelete:
  56. payload = getDingtalkDeletePayload(p.(*api.DeletePayload))
  57. case HookEventTypeFork:
  58. payload = getDingtalkForkPayload(p.(*api.ForkPayload))
  59. case HookEventTypePush:
  60. payload = getDingtalkPushPayload(p.(*api.PushPayload))
  61. case HookEventTypeIssues:
  62. payload = getDingtalkIssuesPayload(p.(*api.IssuesPayload))
  63. case HookEventTypeIssueComment:
  64. payload = getDingtalkIssueCommentPayload(p.(*api.IssueCommentPayload))
  65. case HookEventTypePullRequest:
  66. payload = getDingtalkPullRequestPayload(p.(*api.PullRequestPayload))
  67. case HookEventTypeRelease:
  68. payload = getDingtalkReleasePayload(p.(*api.ReleasePayload))
  69. default:
  70. return nil, errors.Errorf("unexpected event %q", event)
  71. }
  72. return payload, nil
  73. }
  74. func getDingtalkCreatePayload(p *api.CreatePayload) *DingtalkPayload {
  75. refName := git.RefShortName(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{
  82. MsgType: "actionCard",
  83. ActionCard: actionCard,
  84. }
  85. }
  86. func getDingtalkDeletePayload(p *api.DeletePayload) *DingtalkPayload {
  87. refName := git.RefShortName(p.Ref)
  88. refType := strings.Title(p.RefType)
  89. actionCard := NewDingtalkActionCard("View Repo", p.Repo.HTMLURL)
  90. actionCard.Text += "# " + refType + " Delete Event"
  91. actionCard.Text += "\n- Repo: **" + MarkdownLinkFormatter(p.Repo.HTMLURL, p.Repo.Name) + "**"
  92. actionCard.Text += "\n- " + refType + ": **" + refName + "**"
  93. return &DingtalkPayload{
  94. MsgType: "actionCard",
  95. ActionCard: actionCard,
  96. }
  97. }
  98. func getDingtalkForkPayload(p *api.ForkPayload) *DingtalkPayload {
  99. actionCard := NewDingtalkActionCard("View Fork", p.Forkee.HTMLURL)
  100. actionCard.Text += "# Repo Fork Event"
  101. actionCard.Text += "\n- From Repo: **" + MarkdownLinkFormatter(p.Repo.HTMLURL, p.Repo.Name) + "**"
  102. actionCard.Text += "\n- To Repo: **" + MarkdownLinkFormatter(p.Forkee.HTMLURL, p.Forkee.FullName) + "**"
  103. return &DingtalkPayload{
  104. MsgType: "actionCard",
  105. ActionCard: actionCard,
  106. }
  107. }
  108. func getDingtalkPushPayload(p *api.PushPayload) *DingtalkPayload {
  109. refName := git.RefShortName(p.Ref)
  110. pusher := p.Pusher.FullName
  111. if pusher == "" {
  112. pusher = p.Pusher.UserName
  113. }
  114. var detail string
  115. for i, commit := range p.Commits {
  116. msg := strings.Split(commit.Message, "\n")[0]
  117. commitLink := MarkdownLinkFormatter(commit.URL, commit.ID[:7])
  118. detail += fmt.Sprintf("> %d. %s %s - %s\n", i, commitLink, commit.Author.Name, msg)
  119. }
  120. actionCard := NewDingtalkActionCard("View Changes", p.CompareURL)
  121. actionCard.Text += "# Repo Push Event"
  122. actionCard.Text += "\n- Repo: **" + MarkdownLinkFormatter(p.Repo.HTMLURL, p.Repo.Name) + "**"
  123. actionCard.Text += "\n- Ref: **" + MarkdownLinkFormatter(p.Repo.HTMLURL+"/src/"+refName, refName) + "**"
  124. actionCard.Text += "\n- Pusher: **" + pusher + "**"
  125. actionCard.Text += "\n## " + fmt.Sprintf("Total %d commits(s)", len(p.Commits))
  126. actionCard.Text += "\n" + detail
  127. return &DingtalkPayload{
  128. MsgType: "actionCard",
  129. ActionCard: actionCard,
  130. }
  131. }
  132. func getDingtalkIssuesPayload(p *api.IssuesPayload) *DingtalkPayload {
  133. issueName := fmt.Sprintf("#%d %s", p.Index, p.Issue.Title)
  134. issueURL := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Index)
  135. actionCard := NewDingtalkActionCard("View Issue", issueURL)
  136. actionCard.Text += "# Issue Event " + strings.Title(string(p.Action))
  137. actionCard.Text += "\n- Issue: **" + MarkdownLinkFormatter(issueURL, issueName) + "**"
  138. switch p.Action {
  139. case api.HOOK_ISSUE_ASSIGNED:
  140. actionCard.Text += "\n- New Assignee: **" + p.Issue.Assignee.UserName + "**"
  141. case api.HOOK_ISSUE_MILESTONED:
  142. actionCard.Text += "\n- New Milestone: **" + p.Issue.Milestone.Title + "**"
  143. case api.HOOK_ISSUE_LABEL_UPDATED:
  144. if len(p.Issue.Labels) > 0 {
  145. labels := make([]string, len(p.Issue.Labels))
  146. for i, label := range p.Issue.Labels {
  147. labels[i] = "**" + label.Name + "**"
  148. }
  149. actionCard.Text += "\n- Labels: " + strings.Join(labels, ",")
  150. } else {
  151. actionCard.Text += "\n- Labels: **empty**"
  152. }
  153. }
  154. if p.Issue.Body != "" {
  155. actionCard.Text += "\n> " + p.Issue.Body
  156. }
  157. return &DingtalkPayload{
  158. MsgType: "actionCard",
  159. ActionCard: actionCard,
  160. }
  161. }
  162. func getDingtalkIssueCommentPayload(p *api.IssueCommentPayload) *DingtalkPayload {
  163. issueName := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
  164. commentURL := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
  165. if p.Action != api.HOOK_ISSUE_COMMENT_DELETED {
  166. commentURL += "#" + CommentHashTag(p.Comment.ID)
  167. }
  168. issueURL := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
  169. actionCard := NewDingtalkActionCard("View Issue Comment", commentURL)
  170. actionCard.Text += "# Issue Comment " + strings.Title(string(p.Action))
  171. actionCard.Text += "\n- Issue: " + MarkdownLinkFormatter(issueURL, issueName)
  172. actionCard.Text += "\n- Comment content: "
  173. actionCard.Text += "\n> " + p.Comment.Body
  174. return &DingtalkPayload{
  175. MsgType: "actionCard",
  176. ActionCard: actionCard,
  177. }
  178. }
  179. func getDingtalkPullRequestPayload(p *api.PullRequestPayload) *DingtalkPayload {
  180. title := "# Pull Request " + strings.Title(string(p.Action))
  181. if p.Action == api.HOOK_ISSUE_CLOSED && p.PullRequest.HasMerged {
  182. title = "# Pull Request Merged"
  183. }
  184. pullRequestURL := fmt.Sprintf("%s/pulls/%d", p.Repository.HTMLURL, p.Index)
  185. content := "- PR: " + MarkdownLinkFormatter(pullRequestURL, fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title))
  186. switch p.Action {
  187. case api.HOOK_ISSUE_ASSIGNED:
  188. content += "\n- New Assignee: **" + p.PullRequest.Assignee.UserName + "**"
  189. case api.HOOK_ISSUE_MILESTONED:
  190. content += "\n- New Milestone: *" + p.PullRequest.Milestone.Title + "*"
  191. case api.HOOK_ISSUE_LABEL_UPDATED:
  192. labels := make([]string, len(p.PullRequest.Labels))
  193. for i, label := range p.PullRequest.Labels {
  194. labels[i] = "**" + label.Name + "**"
  195. }
  196. content += "\n- New Labels: " + strings.Join(labels, ",")
  197. }
  198. actionCard := NewDingtalkActionCard("View Pull Request", pullRequestURL)
  199. actionCard.Text += title + "\n" + content
  200. if p.Action == api.HOOK_ISSUE_OPENED || p.Action == api.HOOK_ISSUE_EDITED {
  201. actionCard.Text += "\n> " + p.PullRequest.Body
  202. }
  203. return &DingtalkPayload{
  204. MsgType: "actionCard",
  205. ActionCard: actionCard,
  206. }
  207. }
  208. func getDingtalkReleasePayload(p *api.ReleasePayload) *DingtalkPayload {
  209. releaseURL := p.Repository.HTMLURL + "/src/" + p.Release.TagName
  210. author := p.Release.Author.FullName
  211. if author == "" {
  212. author = p.Release.Author.UserName
  213. }
  214. actionCard := NewDingtalkActionCard("View Release", releaseURL)
  215. actionCard.Text += "# New Release Published"
  216. actionCard.Text += "\n- Repo: " + MarkdownLinkFormatter(p.Repository.HTMLURL, p.Repository.Name)
  217. actionCard.Text += "\n- Tag: " + MarkdownLinkFormatter(releaseURL, p.Release.TagName)
  218. actionCard.Text += "\n- Author: " + author
  219. actionCard.Text += fmt.Sprintf("\n- Draft?: %t", p.Release.Draft)
  220. actionCard.Text += fmt.Sprintf("\n- Pre Release?: %t", p.Release.Prerelease)
  221. actionCard.Text += "\n- Title: " + p.Release.Name
  222. if p.Release.Body != "" {
  223. actionCard.Text += "\n- Note: " + p.Release.Body
  224. }
  225. return &DingtalkPayload{
  226. MsgType: "actionCard",
  227. ActionCard: actionCard,
  228. }
  229. }
  230. // MarkdownLinkFormatter formats link address and title into Markdown style.
  231. func MarkdownLinkFormatter(link, text string) string {
  232. return "[" + text + "](" + link + ")"
  233. }