webhook_dingtalk.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 db
  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 HOOK_EVENT_CREATE:
  54. payload = getDingtalkCreatePayload(p.(*api.CreatePayload))
  55. case HOOK_EVENT_DELETE:
  56. payload = getDingtalkDeletePayload(p.(*api.DeletePayload))
  57. case HOOK_EVENT_FORK:
  58. payload = getDingtalkForkPayload(p.(*api.ForkPayload))
  59. case HOOK_EVENT_PUSH:
  60. payload = getDingtalkPushPayload(p.(*api.PushPayload))
  61. case HOOK_EVENT_ISSUES:
  62. payload = getDingtalkIssuesPayload(p.(*api.IssuesPayload))
  63. case HOOK_EVENT_ISSUE_COMMENT:
  64. payload = getDingtalkIssueCommentPayload(p.(*api.IssueCommentPayload))
  65. case HOOK_EVENT_PULL_REQUEST:
  66. payload = getDingtalkPullRequestPayload(p.(*api.PullRequestPayload))
  67. case HOOK_EVENT_RELEASE:
  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. if p.Action == api.HOOK_ISSUE_ASSIGNED {
  139. actionCard.Text += "\n- New Assignee: **" + p.Issue.Assignee.UserName + "**"
  140. } else if p.Action == api.HOOK_ISSUE_MILESTONED {
  141. actionCard.Text += "\n- New Milestone: **" + p.Issue.Milestone.Title + "**"
  142. } else if p.Action == api.HOOK_ISSUE_LABEL_UPDATED {
  143. if len(p.Issue.Labels) > 0 {
  144. labels := make([]string, len(p.Issue.Labels))
  145. for i, label := range p.Issue.Labels {
  146. labels[i] = "**" + label.Name + "**"
  147. }
  148. actionCard.Text += "\n- Labels: " + strings.Join(labels, ",")
  149. } else {
  150. actionCard.Text += "\n- Labels: **empty**"
  151. }
  152. }
  153. if p.Issue.Body != "" {
  154. actionCard.Text += "\n> " + p.Issue.Body
  155. }
  156. return &DingtalkPayload{
  157. MsgType: "actionCard",
  158. ActionCard: actionCard,
  159. }
  160. }
  161. func getDingtalkIssueCommentPayload(p *api.IssueCommentPayload) *DingtalkPayload {
  162. issueName := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
  163. commentURL := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
  164. if p.Action != api.HOOK_ISSUE_COMMENT_DELETED {
  165. commentURL += "#" + CommentHashTag(p.Comment.ID)
  166. }
  167. issueURL := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
  168. actionCard := NewDingtalkActionCard("View Issue Comment", commentURL)
  169. actionCard.Text += "# Issue Comment " + strings.Title(string(p.Action))
  170. actionCard.Text += "\n- Issue: " + MarkdownLinkFormatter(issueURL, issueName)
  171. actionCard.Text += "\n- Comment content: "
  172. actionCard.Text += "\n> " + p.Comment.Body
  173. return &DingtalkPayload{
  174. MsgType: "actionCard",
  175. ActionCard: actionCard,
  176. }
  177. }
  178. func getDingtalkPullRequestPayload(p *api.PullRequestPayload) *DingtalkPayload {
  179. title := "# Pull Request " + strings.Title(string(p.Action))
  180. if p.Action == api.HOOK_ISSUE_CLOSED && p.PullRequest.HasMerged {
  181. title = "# Pull Request Merged"
  182. }
  183. pullRequestURL := fmt.Sprintf("%s/pulls/%d", p.Repository.HTMLURL, p.Index)
  184. content := "- PR: " + MarkdownLinkFormatter(pullRequestURL, fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title))
  185. if p.Action == api.HOOK_ISSUE_ASSIGNED {
  186. content += "\n- New Assignee: **" + p.PullRequest.Assignee.UserName + "**"
  187. } else if p.Action == api.HOOK_ISSUE_MILESTONED {
  188. content += "\n- New Milestone: *" + p.PullRequest.Milestone.Title + "*"
  189. } else if p.Action == api.HOOK_ISSUE_LABEL_UPDATED {
  190. labels := make([]string, len(p.PullRequest.Labels))
  191. for i, label := range p.PullRequest.Labels {
  192. labels[i] = "**" + label.Name + "**"
  193. }
  194. content += "\n- New Labels: " + strings.Join(labels, ",")
  195. }
  196. actionCard := NewDingtalkActionCard("View Pull Request", pullRequestURL)
  197. actionCard.Text += title + "\n" + content
  198. if p.Action == api.HOOK_ISSUE_OPENED || p.Action == api.HOOK_ISSUE_EDITED {
  199. actionCard.Text += "\n> " + p.PullRequest.Body
  200. }
  201. return &DingtalkPayload{
  202. MsgType: "actionCard",
  203. ActionCard: actionCard,
  204. }
  205. }
  206. func getDingtalkReleasePayload(p *api.ReleasePayload) *DingtalkPayload {
  207. releaseURL := p.Repository.HTMLURL + "/src/" + p.Release.TagName
  208. author := p.Release.Author.FullName
  209. if author == "" {
  210. author = p.Release.Author.UserName
  211. }
  212. actionCard := NewDingtalkActionCard("View Release", releaseURL)
  213. actionCard.Text += "# New Release Published"
  214. actionCard.Text += "\n- Repo: " + MarkdownLinkFormatter(p.Repository.HTMLURL, p.Repository.Name)
  215. actionCard.Text += "\n- Tag: " + MarkdownLinkFormatter(releaseURL, p.Release.TagName)
  216. actionCard.Text += "\n- Author: " + author
  217. actionCard.Text += fmt.Sprintf("\n- Draft?: %t", p.Release.Draft)
  218. actionCard.Text += fmt.Sprintf("\n- Pre Release?: %t", p.Release.Prerelease)
  219. actionCard.Text += "\n- Title: " + p.Release.Name
  220. if p.Release.Body != "" {
  221. actionCard.Text += "\n- Note: " + p.Release.Body
  222. }
  223. return &DingtalkPayload{
  224. MsgType: "actionCard",
  225. ActionCard: actionCard,
  226. }
  227. }
  228. // MarkdownLinkFormatter formats link address and title into Markdown style.
  229. func MarkdownLinkFormatter(link, text string) string {
  230. return "[" + text + "](" + link + ")"
  231. }