You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wechatwork.go 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package webhook
  4. import (
  5. "context"
  6. "fmt"
  7. "net/http"
  8. "strings"
  9. webhook_model "code.gitea.io/gitea/models/webhook"
  10. "code.gitea.io/gitea/modules/git"
  11. api "code.gitea.io/gitea/modules/structs"
  12. webhook_module "code.gitea.io/gitea/modules/webhook"
  13. )
  14. type (
  15. // WechatworkPayload represents
  16. WechatworkPayload struct {
  17. Msgtype string `json:"msgtype"`
  18. Text struct {
  19. Content string `json:"content"`
  20. MentionedList []string `json:"mentioned_list"`
  21. MentionedMobileList []string `json:"mentioned_mobile_list"`
  22. } `json:"text"`
  23. Markdown struct {
  24. Content string `json:"content"`
  25. } `json:"markdown"`
  26. }
  27. )
  28. func newWechatworkMarkdownPayload(title string) WechatworkPayload {
  29. return WechatworkPayload{
  30. Msgtype: "markdown",
  31. Markdown: struct {
  32. Content string `json:"content"`
  33. }{
  34. Content: title,
  35. },
  36. }
  37. }
  38. // Create implements PayloadConvertor Create method
  39. func (wc wechatworkConvertor) Create(p *api.CreatePayload) (WechatworkPayload, error) {
  40. // created tag/branch
  41. refName := git.RefName(p.Ref).ShortName()
  42. title := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName)
  43. return newWechatworkMarkdownPayload(title), nil
  44. }
  45. // Delete implements PayloadConvertor Delete method
  46. func (wc wechatworkConvertor) Delete(p *api.DeletePayload) (WechatworkPayload, error) {
  47. // created tag/branch
  48. refName := git.RefName(p.Ref).ShortName()
  49. title := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName)
  50. return newWechatworkMarkdownPayload(title), nil
  51. }
  52. // Fork implements PayloadConvertor Fork method
  53. func (wc wechatworkConvertor) Fork(p *api.ForkPayload) (WechatworkPayload, error) {
  54. title := fmt.Sprintf("%s is forked to %s", p.Forkee.FullName, p.Repo.FullName)
  55. return newWechatworkMarkdownPayload(title), nil
  56. }
  57. // Push implements PayloadConvertor Push method
  58. func (wc wechatworkConvertor) Push(p *api.PushPayload) (WechatworkPayload, error) {
  59. var (
  60. branchName = git.RefName(p.Ref).ShortName()
  61. commitDesc string
  62. )
  63. title := fmt.Sprintf("# %s:%s <font color=\"warning\"> %s </font>", p.Repo.FullName, branchName, commitDesc)
  64. var text string
  65. // for each commit, generate attachment text
  66. for i, commit := range p.Commits {
  67. var authorName string
  68. if commit.Author != nil {
  69. authorName = "Author: " + commit.Author.Name
  70. }
  71. message := strings.ReplaceAll(commit.Message, "\n\n", "\r\n")
  72. text += fmt.Sprintf(" > [%s](%s) \r\n ><font color=\"info\">%s</font> \n ><font color=\"warning\">%s</font>", commit.ID[:7], commit.URL,
  73. message, authorName)
  74. // add linebreak to each commit but the last
  75. if i < len(p.Commits)-1 {
  76. text += "\n"
  77. }
  78. }
  79. return newWechatworkMarkdownPayload(title + "\r\n\r\n" + text), nil
  80. }
  81. // Issue implements PayloadConvertor Issue method
  82. func (wc wechatworkConvertor) Issue(p *api.IssuePayload) (WechatworkPayload, error) {
  83. text, issueTitle, attachmentText, _ := getIssuesPayloadInfo(p, noneLinkFormatter, true)
  84. var content string
  85. content += fmt.Sprintf(" ><font color=\"info\">%s</font>\n >%s \n ><font color=\"warning\"> %s</font> \n [%s](%s)", text, attachmentText, issueTitle, p.Issue.HTMLURL, p.Issue.HTMLURL)
  86. return newWechatworkMarkdownPayload(content), nil
  87. }
  88. // IssueComment implements PayloadConvertor IssueComment method
  89. func (wc wechatworkConvertor) IssueComment(p *api.IssueCommentPayload) (WechatworkPayload, error) {
  90. text, issueTitle, _ := getIssueCommentPayloadInfo(p, noneLinkFormatter, true)
  91. var content string
  92. content += fmt.Sprintf(" ><font color=\"info\">%s</font>\n >%s \n ><font color=\"warning\">%s</font> \n [%s](%s)", text, p.Comment.Body, issueTitle, p.Comment.HTMLURL, p.Comment.HTMLURL)
  93. return newWechatworkMarkdownPayload(content), nil
  94. }
  95. // PullRequest implements PayloadConvertor PullRequest method
  96. func (wc wechatworkConvertor) PullRequest(p *api.PullRequestPayload) (WechatworkPayload, error) {
  97. text, issueTitle, attachmentText, _ := getPullRequestPayloadInfo(p, noneLinkFormatter, true)
  98. pr := fmt.Sprintf("> <font color=\"info\"> %s </font> \r\n > <font color=\"comment\">%s </font> \r\n > <font color=\"comment\">%s </font> \r\n",
  99. text, issueTitle, attachmentText)
  100. return newWechatworkMarkdownPayload(pr), nil
  101. }
  102. // Review implements PayloadConvertor Review method
  103. func (wc wechatworkConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (WechatworkPayload, error) {
  104. var text, title string
  105. switch p.Action {
  106. case api.HookIssueReviewed:
  107. action, err := parseHookPullRequestEventType(event)
  108. if err != nil {
  109. return WechatworkPayload{}, err
  110. }
  111. title = fmt.Sprintf("[%s] Pull request review %s : #%d %s", p.Repository.FullName, action, p.Index, p.PullRequest.Title)
  112. text = p.Review.Content
  113. }
  114. return newWechatworkMarkdownPayload("# " + title + "\r\n\r\n >" + text), nil
  115. }
  116. // Repository implements PayloadConvertor Repository method
  117. func (wc wechatworkConvertor) Repository(p *api.RepositoryPayload) (WechatworkPayload, error) {
  118. var title string
  119. switch p.Action {
  120. case api.HookRepoCreated:
  121. title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
  122. return newWechatworkMarkdownPayload(title), nil
  123. case api.HookRepoDeleted:
  124. title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
  125. return newWechatworkMarkdownPayload(title), nil
  126. }
  127. return WechatworkPayload{}, nil
  128. }
  129. // Wiki implements PayloadConvertor Wiki method
  130. func (wc wechatworkConvertor) Wiki(p *api.WikiPayload) (WechatworkPayload, error) {
  131. text, _, _ := getWikiPayloadInfo(p, noneLinkFormatter, true)
  132. return newWechatworkMarkdownPayload(text), nil
  133. }
  134. // Release implements PayloadConvertor Release method
  135. func (wc wechatworkConvertor) Release(p *api.ReleasePayload) (WechatworkPayload, error) {
  136. text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true)
  137. return newWechatworkMarkdownPayload(text), nil
  138. }
  139. func (wc wechatworkConvertor) Package(p *api.PackagePayload) (WechatworkPayload, error) {
  140. text, _ := getPackagePayloadInfo(p, noneLinkFormatter, true)
  141. return newWechatworkMarkdownPayload(text), nil
  142. }
  143. type wechatworkConvertor struct{}
  144. var _ payloadConvertor[WechatworkPayload] = wechatworkConvertor{}
  145. func newWechatworkRequest(ctx context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
  146. return newJSONRequest(wechatworkConvertor{}, w, t, true)
  147. }