gitea源码

feishu.go 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package webhook
  4. import (
  5. "context"
  6. "crypto/hmac"
  7. "crypto/sha256"
  8. "encoding/base64"
  9. "fmt"
  10. "net/http"
  11. "strings"
  12. "time"
  13. webhook_model "code.gitea.io/gitea/models/webhook"
  14. "code.gitea.io/gitea/modules/git"
  15. api "code.gitea.io/gitea/modules/structs"
  16. webhook_module "code.gitea.io/gitea/modules/webhook"
  17. )
  18. type (
  19. // FeishuPayload represents the payload for Feishu webhook
  20. FeishuPayload struct {
  21. Timestamp int64 `json:"timestamp,omitempty"` // Unix timestamp for signature verification
  22. Sign string `json:"sign,omitempty"` // Signature for verification
  23. MsgType string `json:"msg_type"` // text / post / image / share_chat / interactive / file /audio / media
  24. Content struct {
  25. Text string `json:"text"`
  26. } `json:"content"`
  27. }
  28. )
  29. func newFeishuTextPayload(text string) FeishuPayload {
  30. return FeishuPayload{
  31. MsgType: "text",
  32. Content: struct {
  33. Text string `json:"text"`
  34. }{
  35. Text: strings.TrimSpace(text),
  36. },
  37. }
  38. }
  39. type feishuConvertor struct{}
  40. // Create implements PayloadConvertor Create method
  41. func (fc feishuConvertor) Create(p *api.CreatePayload) (FeishuPayload, error) {
  42. // created tag/branch
  43. refName := git.RefName(p.Ref).ShortName()
  44. text := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName)
  45. return newFeishuTextPayload(text), nil
  46. }
  47. // Delete implements PayloadConvertor Delete method
  48. func (fc feishuConvertor) Delete(p *api.DeletePayload) (FeishuPayload, error) {
  49. // created tag/branch
  50. refName := git.RefName(p.Ref).ShortName()
  51. text := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName)
  52. return newFeishuTextPayload(text), nil
  53. }
  54. // Fork implements PayloadConvertor Fork method
  55. func (fc feishuConvertor) Fork(p *api.ForkPayload) (FeishuPayload, error) {
  56. text := fmt.Sprintf("%s is forked to %s", p.Forkee.FullName, p.Repo.FullName)
  57. return newFeishuTextPayload(text), nil
  58. }
  59. // Push implements PayloadConvertor Push method
  60. func (fc feishuConvertor) Push(p *api.PushPayload) (FeishuPayload, error) {
  61. var (
  62. branchName = git.RefName(p.Ref).ShortName()
  63. commitDesc string
  64. )
  65. text := fmt.Sprintf("[%s:%s] %s\r\n", p.Repo.FullName, branchName, commitDesc)
  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 = " - " + commit.Author.Name
  71. }
  72. text += fmt.Sprintf("[%s](%s) %s", commit.ID[:7], commit.URL,
  73. strings.TrimRight(commit.Message, "\r\n")) + authorName
  74. // add linebreak to each commit but the last
  75. if i < len(p.Commits)-1 {
  76. text += "\r\n"
  77. }
  78. }
  79. return newFeishuTextPayload(text), nil
  80. }
  81. // Issue implements PayloadConvertor Issue method
  82. func (fc feishuConvertor) Issue(p *api.IssuePayload) (FeishuPayload, error) {
  83. title, link, by, operator, result, assignees := getIssuesInfo(p)
  84. if assignees != "" {
  85. if p.Action == api.HookIssueAssigned || p.Action == api.HookIssueUnassigned || p.Action == api.HookIssueMilestoned {
  86. return newFeishuTextPayload(fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s\n\n%s", title, link, by, operator, result, assignees, p.Issue.Body)), nil
  87. }
  88. return newFeishuTextPayload(fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n\n%s", title, link, by, operator, assignees, p.Issue.Body)), nil
  89. }
  90. return newFeishuTextPayload(fmt.Sprintf("%s\n%s\n%s\n%s\n\n%s", title, link, by, operator, p.Issue.Body)), nil
  91. }
  92. // IssueComment implements PayloadConvertor IssueComment method
  93. func (fc feishuConvertor) IssueComment(p *api.IssueCommentPayload) (FeishuPayload, error) {
  94. title, link, by, operator := getIssuesCommentInfo(p)
  95. return newFeishuTextPayload(fmt.Sprintf("%s\n%s\n%s\n%s\n\n%s", title, link, by, operator, p.Comment.Body)), nil
  96. }
  97. // PullRequest implements PayloadConvertor PullRequest method
  98. func (fc feishuConvertor) PullRequest(p *api.PullRequestPayload) (FeishuPayload, error) {
  99. title, link, by, operator, result, assignees := getPullRequestInfo(p)
  100. if assignees != "" {
  101. if p.Action == api.HookIssueAssigned || p.Action == api.HookIssueUnassigned || p.Action == api.HookIssueMilestoned {
  102. return newFeishuTextPayload(fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s\n\n%s", title, link, by, operator, result, assignees, p.PullRequest.Body)), nil
  103. }
  104. return newFeishuTextPayload(fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n\n%s", title, link, by, operator, assignees, p.PullRequest.Body)), nil
  105. }
  106. return newFeishuTextPayload(fmt.Sprintf("%s\n%s\n%s\n%s\n\n%s", title, link, by, operator, p.PullRequest.Body)), nil
  107. }
  108. // Review implements PayloadConvertor Review method
  109. func (fc feishuConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (FeishuPayload, error) {
  110. action, err := parseHookPullRequestEventType(event)
  111. if err != nil {
  112. return FeishuPayload{}, err
  113. }
  114. title := fmt.Sprintf("[%s] Pull request review %s : #%d %s", p.Repository.FullName, action, p.Index, p.PullRequest.Title)
  115. text := p.Review.Content
  116. return newFeishuTextPayload(title + "\r\n\r\n" + text), nil
  117. }
  118. // Repository implements PayloadConvertor Repository method
  119. func (fc feishuConvertor) Repository(p *api.RepositoryPayload) (FeishuPayload, error) {
  120. var text string
  121. switch p.Action {
  122. case api.HookRepoCreated:
  123. text = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
  124. return newFeishuTextPayload(text), nil
  125. case api.HookRepoDeleted:
  126. text = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
  127. return newFeishuTextPayload(text), nil
  128. }
  129. return FeishuPayload{}, nil
  130. }
  131. // Wiki implements PayloadConvertor Wiki method
  132. func (fc feishuConvertor) Wiki(p *api.WikiPayload) (FeishuPayload, error) {
  133. text, _, _ := getWikiPayloadInfo(p, noneLinkFormatter, true)
  134. return newFeishuTextPayload(text), nil
  135. }
  136. // Release implements PayloadConvertor Release method
  137. func (fc feishuConvertor) Release(p *api.ReleasePayload) (FeishuPayload, error) {
  138. text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true)
  139. return newFeishuTextPayload(text), nil
  140. }
  141. func (fc feishuConvertor) Package(p *api.PackagePayload) (FeishuPayload, error) {
  142. text, _ := getPackagePayloadInfo(p, noneLinkFormatter, true)
  143. return newFeishuTextPayload(text), nil
  144. }
  145. func (fc feishuConvertor) Status(p *api.CommitStatusPayload) (FeishuPayload, error) {
  146. text, _ := getStatusPayloadInfo(p, noneLinkFormatter, true)
  147. return newFeishuTextPayload(text), nil
  148. }
  149. func (feishuConvertor) WorkflowRun(p *api.WorkflowRunPayload) (FeishuPayload, error) {
  150. text, _ := getWorkflowRunPayloadInfo(p, noneLinkFormatter, true)
  151. return newFeishuTextPayload(text), nil
  152. }
  153. func (feishuConvertor) WorkflowJob(p *api.WorkflowJobPayload) (FeishuPayload, error) {
  154. text, _ := getWorkflowJobPayloadInfo(p, noneLinkFormatter, true)
  155. return newFeishuTextPayload(text), nil
  156. }
  157. // feishuGenSign generates a signature for Feishu webhook
  158. // https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot
  159. func feishuGenSign(secret string, timestamp int64) string {
  160. // key="{timestamp}\n{secret}", then hmac-sha256, then base64 encode
  161. stringToSign := fmt.Sprintf("%d\n%s", timestamp, secret)
  162. h := hmac.New(sha256.New, []byte(stringToSign))
  163. return base64.StdEncoding.EncodeToString(h.Sum(nil))
  164. }
  165. func newFeishuRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
  166. payload, err := newPayload(feishuConvertor{}, []byte(t.PayloadContent), t.EventType)
  167. if err != nil {
  168. return nil, nil, err
  169. }
  170. // Add timestamp and signature if secret is provided
  171. if w.Secret != "" {
  172. timestamp := time.Now().Unix()
  173. payload.Timestamp = timestamp
  174. payload.Sign = feishuGenSign(w.Secret, timestamp)
  175. }
  176. return prepareJSONRequest(payload, w, t, false /* no default headers */)
  177. }
  178. func init() {
  179. RegisterWebhookRequester(webhook_module.FEISHU, newFeishuRequest)
  180. }