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

webhook_dingtalk.go 6.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2017 The Gitea 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 models
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "strings"
  9. "code.gitea.io/git"
  10. api "code.gitea.io/sdk/gitea"
  11. dingtalk "github.com/lunny/dingtalk_webhook"
  12. )
  13. type (
  14. // DingtalkPayload represents
  15. DingtalkPayload dingtalk.Payload
  16. )
  17. // SetSecret sets the dingtalk secret
  18. func (p *DingtalkPayload) SetSecret(_ string) {}
  19. // JSONPayload Marshals the DingtalkPayload to json
  20. func (p *DingtalkPayload) JSONPayload() ([]byte, error) {
  21. data, err := json.MarshalIndent(p, "", " ")
  22. if err != nil {
  23. return []byte{}, err
  24. }
  25. return data, nil
  26. }
  27. func getDingtalkCreatePayload(p *api.CreatePayload) (*DingtalkPayload, error) {
  28. // created tag/branch
  29. refName := git.RefEndName(p.Ref)
  30. title := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName)
  31. return &DingtalkPayload{
  32. MsgType: "actionCard",
  33. ActionCard: dingtalk.ActionCard{
  34. Text: title,
  35. Title: title,
  36. HideAvatar: "0",
  37. SingleTitle: fmt.Sprintf("view branch %s", refName),
  38. SingleURL: p.Repo.HTMLURL + "/src/" + refName,
  39. },
  40. }, nil
  41. }
  42. func getDingtalkPushPayload(p *api.PushPayload) (*DingtalkPayload, error) {
  43. var (
  44. branchName = git.RefEndName(p.Ref)
  45. commitDesc string
  46. )
  47. var titleLink, linkText string
  48. if len(p.Commits) == 1 {
  49. commitDesc = "1 new commit"
  50. titleLink = p.Commits[0].URL
  51. linkText = fmt.Sprintf("view commit %s", p.Commits[0].ID[:7])
  52. } else {
  53. commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
  54. titleLink = p.CompareURL
  55. linkText = fmt.Sprintf("view commit %s...%s", p.Commits[0].ID[:7], p.Commits[len(p.Commits)-1].ID[:7])
  56. }
  57. if titleLink == "" {
  58. titleLink = p.Repo.HTMLURL + "/src/" + branchName
  59. }
  60. title := fmt.Sprintf("[%s:%s] %s", p.Repo.FullName, branchName, commitDesc)
  61. var text string
  62. // for each commit, generate attachment text
  63. for i, commit := range p.Commits {
  64. var authorName string
  65. if commit.Author != nil {
  66. authorName = " - " + commit.Author.Name
  67. }
  68. text += fmt.Sprintf("[%s](%s) %s", commit.ID[:7], commit.URL,
  69. strings.TrimRight(commit.Message, "\r\n")) + authorName
  70. // add linebreak to each commit but the last
  71. if i < len(p.Commits)-1 {
  72. text += "\n"
  73. }
  74. }
  75. return &DingtalkPayload{
  76. MsgType: "actionCard",
  77. ActionCard: dingtalk.ActionCard{
  78. Text: text,
  79. Title: title,
  80. HideAvatar: "0",
  81. SingleTitle: linkText,
  82. SingleURL: titleLink,
  83. },
  84. }, nil
  85. }
  86. func getDingtalkPullRequestPayload(p *api.PullRequestPayload) (*DingtalkPayload, error) {
  87. var text, title string
  88. switch p.Action {
  89. case api.HookIssueOpened:
  90. title = fmt.Sprintf("[%s] Pull request opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  91. text = p.PullRequest.Body
  92. case api.HookIssueClosed:
  93. if p.PullRequest.HasMerged {
  94. title = fmt.Sprintf("[%s] Pull request merged: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  95. } else {
  96. title = fmt.Sprintf("[%s] Pull request closed: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  97. }
  98. text = p.PullRequest.Body
  99. case api.HookIssueReOpened:
  100. title = fmt.Sprintf("[%s] Pull request re-opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  101. text = p.PullRequest.Body
  102. case api.HookIssueEdited:
  103. title = fmt.Sprintf("[%s] Pull request edited: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  104. text = p.PullRequest.Body
  105. case api.HookIssueAssigned:
  106. list, err := MakeAssigneeList(&Issue{ID: p.PullRequest.ID})
  107. if err != nil {
  108. return &DingtalkPayload{}, err
  109. }
  110. title = fmt.Sprintf("[%s] Pull request assigned to %s: #%d %s", p.Repository.FullName,
  111. list, p.Index, p.PullRequest.Title)
  112. text = p.PullRequest.Body
  113. case api.HookIssueUnassigned:
  114. title = fmt.Sprintf("[%s] Pull request unassigned: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  115. text = p.PullRequest.Body
  116. case api.HookIssueLabelUpdated:
  117. title = fmt.Sprintf("[%s] Pull request labels updated: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  118. text = p.PullRequest.Body
  119. case api.HookIssueLabelCleared:
  120. title = fmt.Sprintf("[%s] Pull request labels cleared: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  121. text = p.PullRequest.Body
  122. case api.HookIssueSynchronized:
  123. title = fmt.Sprintf("[%s] Pull request synchronized: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  124. text = p.PullRequest.Body
  125. }
  126. return &DingtalkPayload{
  127. MsgType: "actionCard",
  128. ActionCard: dingtalk.ActionCard{
  129. Text: text,
  130. Title: title,
  131. HideAvatar: "0",
  132. SingleTitle: "view pull request",
  133. SingleURL: p.PullRequest.HTMLURL,
  134. },
  135. }, nil
  136. }
  137. func getDingtalkRepositoryPayload(p *api.RepositoryPayload) (*DingtalkPayload, error) {
  138. var title, url string
  139. switch p.Action {
  140. case api.HookRepoCreated:
  141. title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
  142. url = p.Repository.HTMLURL
  143. return &DingtalkPayload{
  144. MsgType: "actionCard",
  145. ActionCard: dingtalk.ActionCard{
  146. Text: title,
  147. Title: title,
  148. HideAvatar: "0",
  149. SingleTitle: "view repository",
  150. SingleURL: url,
  151. },
  152. }, nil
  153. case api.HookRepoDeleted:
  154. title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
  155. return &DingtalkPayload{
  156. MsgType: "text",
  157. Text: struct {
  158. Content string `json:"content"`
  159. }{
  160. Content: title,
  161. },
  162. }, nil
  163. }
  164. return nil, nil
  165. }
  166. // GetDingtalkPayload converts a ding talk webhook into a DingtalkPayload
  167. func GetDingtalkPayload(p api.Payloader, event HookEventType, meta string) (*DingtalkPayload, error) {
  168. s := new(DingtalkPayload)
  169. switch event {
  170. case HookEventCreate:
  171. return getDingtalkCreatePayload(p.(*api.CreatePayload))
  172. case HookEventPush:
  173. return getDingtalkPushPayload(p.(*api.PushPayload))
  174. case HookEventPullRequest:
  175. return getDingtalkPullRequestPayload(p.(*api.PullRequestPayload))
  176. case HookEventRepository:
  177. return getDingtalkRepositoryPayload(p.(*api.RepositoryPayload))
  178. }
  179. return s, nil
  180. }