1
0

webhook_dingtalk.go 9.2 KB

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