gitea源码

wechatwork.go 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. type wechatworkConvertor struct{}
  39. // Create implements PayloadConvertor Create method
  40. func (wc wechatworkConvertor) Create(p *api.CreatePayload) (WechatworkPayload, error) {
  41. // created tag/branch
  42. refName := git.RefName(p.Ref).ShortName()
  43. title := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName)
  44. return newWechatworkMarkdownPayload(title), nil
  45. }
  46. // Delete implements PayloadConvertor Delete method
  47. func (wc wechatworkConvertor) Delete(p *api.DeletePayload) (WechatworkPayload, error) {
  48. // created tag/branch
  49. refName := git.RefName(p.Ref).ShortName()
  50. title := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName)
  51. return newWechatworkMarkdownPayload(title), nil
  52. }
  53. // Fork implements PayloadConvertor Fork method
  54. func (wc wechatworkConvertor) Fork(p *api.ForkPayload) (WechatworkPayload, error) {
  55. title := fmt.Sprintf("%s is forked to %s", p.Forkee.FullName, p.Repo.FullName)
  56. return newWechatworkMarkdownPayload(title), nil
  57. }
  58. // Push implements PayloadConvertor Push method
  59. func (wc wechatworkConvertor) Push(p *api.PushPayload) (WechatworkPayload, error) {
  60. var (
  61. branchName = git.RefName(p.Ref).ShortName()
  62. commitDesc string
  63. )
  64. title := fmt.Sprintf("# %s:%s <font color=\"warning\"> %s </font>", p.Repo.FullName, branchName, commitDesc)
  65. var text string
  66. // for each commit, generate attachment text
  67. for i, commit := range p.Commits {
  68. var authorName string
  69. if commit.Author != nil {
  70. authorName = "Author: " + commit.Author.Name
  71. }
  72. message := strings.ReplaceAll(commit.Message, "\n\n", "\r\n")
  73. text += fmt.Sprintf(" > [%s](%s) \r\n ><font color=\"info\">%s</font> \n ><font color=\"warning\">%s</font>", commit.ID[:7], commit.URL,
  74. message, authorName)
  75. // add linebreak to each commit but the last
  76. if i < len(p.Commits)-1 {
  77. text += "\n"
  78. }
  79. }
  80. return newWechatworkMarkdownPayload(title + "\r\n\r\n" + text), nil
  81. }
  82. // Issue implements PayloadConvertor Issue method
  83. func (wc wechatworkConvertor) Issue(p *api.IssuePayload) (WechatworkPayload, error) {
  84. text, issueTitle, extraMarkdown, _ := getIssuesPayloadInfo(p, noneLinkFormatter, true)
  85. var content string
  86. content += fmt.Sprintf(" ><font color=\"info\">%s</font>\n >%s \n ><font color=\"warning\"> %s</font> \n [%s](%s)", text, extraMarkdown, issueTitle, p.Issue.HTMLURL, p.Issue.HTMLURL)
  87. return newWechatworkMarkdownPayload(content), nil
  88. }
  89. // IssueComment implements PayloadConvertor IssueComment method
  90. func (wc wechatworkConvertor) IssueComment(p *api.IssueCommentPayload) (WechatworkPayload, error) {
  91. text, issueTitle, _ := getIssueCommentPayloadInfo(p, noneLinkFormatter, true)
  92. var content string
  93. 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)
  94. return newWechatworkMarkdownPayload(content), nil
  95. }
  96. // PullRequest implements PayloadConvertor PullRequest method
  97. func (wc wechatworkConvertor) PullRequest(p *api.PullRequestPayload) (WechatworkPayload, error) {
  98. text, issueTitle, extraMarkdown, _ := getPullRequestPayloadInfo(p, noneLinkFormatter, true)
  99. pr := fmt.Sprintf("> <font color=\"info\"> %s </font> \r\n > <font color=\"comment\">%s </font> \r\n > <font color=\"comment\">%s </font> \r\n",
  100. text, issueTitle, extraMarkdown)
  101. return newWechatworkMarkdownPayload(pr), nil
  102. }
  103. // Review implements PayloadConvertor Review method
  104. func (wc wechatworkConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (WechatworkPayload, error) {
  105. var text, title string
  106. switch p.Action {
  107. case api.HookIssueReviewed:
  108. action, err := parseHookPullRequestEventType(event)
  109. if err != nil {
  110. return WechatworkPayload{}, err
  111. }
  112. title = fmt.Sprintf("[%s] Pull request review %s : #%d %s", p.Repository.FullName, action, p.Index, p.PullRequest.Title)
  113. text = p.Review.Content
  114. }
  115. return newWechatworkMarkdownPayload("# " + title + "\r\n\r\n >" + text), nil
  116. }
  117. // Repository implements PayloadConvertor Repository method
  118. func (wc wechatworkConvertor) Repository(p *api.RepositoryPayload) (WechatworkPayload, error) {
  119. var title string
  120. switch p.Action {
  121. case api.HookRepoCreated:
  122. title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
  123. return newWechatworkMarkdownPayload(title), nil
  124. case api.HookRepoDeleted:
  125. title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
  126. return newWechatworkMarkdownPayload(title), nil
  127. }
  128. return WechatworkPayload{}, nil
  129. }
  130. // Wiki implements PayloadConvertor Wiki method
  131. func (wc wechatworkConvertor) Wiki(p *api.WikiPayload) (WechatworkPayload, error) {
  132. text, _, _ := getWikiPayloadInfo(p, noneLinkFormatter, true)
  133. return newWechatworkMarkdownPayload(text), nil
  134. }
  135. // Release implements PayloadConvertor Release method
  136. func (wc wechatworkConvertor) Release(p *api.ReleasePayload) (WechatworkPayload, error) {
  137. text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true)
  138. return newWechatworkMarkdownPayload(text), nil
  139. }
  140. func (wc wechatworkConvertor) Package(p *api.PackagePayload) (WechatworkPayload, error) {
  141. text, _ := getPackagePayloadInfo(p, noneLinkFormatter, true)
  142. return newWechatworkMarkdownPayload(text), nil
  143. }
  144. func (wc wechatworkConvertor) Status(p *api.CommitStatusPayload) (WechatworkPayload, error) {
  145. text, _ := getStatusPayloadInfo(p, noneLinkFormatter, true)
  146. return newWechatworkMarkdownPayload(text), nil
  147. }
  148. func (wc wechatworkConvertor) WorkflowRun(p *api.WorkflowRunPayload) (WechatworkPayload, error) {
  149. text, _ := getWorkflowRunPayloadInfo(p, noneLinkFormatter, true)
  150. return newWechatworkMarkdownPayload(text), nil
  151. }
  152. func (wc wechatworkConvertor) WorkflowJob(p *api.WorkflowJobPayload) (WechatworkPayload, error) {
  153. text, _ := getWorkflowJobPayloadInfo(p, noneLinkFormatter, true)
  154. return newWechatworkMarkdownPayload(text), nil
  155. }
  156. func newWechatworkRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
  157. var pc payloadConvertor[WechatworkPayload] = wechatworkConvertor{}
  158. return newJSONRequest(pc, w, t, true)
  159. }
  160. func init() {
  161. RegisterWebhookRequester(webhook_module.WECHATWORK, newWechatworkRequest)
  162. }