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.

webhook_dingtalk.go 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. title = fmt.Sprintf("[%s] Pull request assigned to %s: #%d %s", p.Repository.FullName,
  107. p.PullRequest.Assignee.UserName, p.Index, p.PullRequest.Title)
  108. text = p.PullRequest.Body
  109. case api.HookIssueUnassigned:
  110. title = fmt.Sprintf("[%s] Pull request unassigned: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  111. text = p.PullRequest.Body
  112. case api.HookIssueLabelUpdated:
  113. title = fmt.Sprintf("[%s] Pull request labels updated: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  114. text = p.PullRequest.Body
  115. case api.HookIssueLabelCleared:
  116. title = fmt.Sprintf("[%s] Pull request labels cleared: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  117. text = p.PullRequest.Body
  118. case api.HookIssueSynchronized:
  119. title = fmt.Sprintf("[%s] Pull request synchronized: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  120. text = p.PullRequest.Body
  121. }
  122. return &DingtalkPayload{
  123. MsgType: "actionCard",
  124. ActionCard: dingtalk.ActionCard{
  125. Text: text,
  126. Title: title,
  127. HideAvatar: "0",
  128. SingleTitle: "view pull request",
  129. SingleURL: p.PullRequest.HTMLURL,
  130. },
  131. }, nil
  132. }
  133. func getDingtalkRepositoryPayload(p *api.RepositoryPayload) (*DingtalkPayload, error) {
  134. var title, url string
  135. switch p.Action {
  136. case api.HookRepoCreated:
  137. title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
  138. url = p.Repository.HTMLURL
  139. return &DingtalkPayload{
  140. MsgType: "actionCard",
  141. ActionCard: dingtalk.ActionCard{
  142. Text: title,
  143. Title: title,
  144. HideAvatar: "0",
  145. SingleTitle: "view repository",
  146. SingleURL: url,
  147. },
  148. }, nil
  149. case api.HookRepoDeleted:
  150. title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
  151. return &DingtalkPayload{
  152. MsgType: "text",
  153. Text: struct {
  154. Content string `json:"content"`
  155. }{
  156. Content: title,
  157. },
  158. }, nil
  159. }
  160. return nil, nil
  161. }
  162. // GetDingtalkPayload converts a ding talk webhook into a DingtalkPayload
  163. func GetDingtalkPayload(p api.Payloader, event HookEventType, meta string) (*DingtalkPayload, error) {
  164. s := new(DingtalkPayload)
  165. switch event {
  166. case HookEventCreate:
  167. return getDingtalkCreatePayload(p.(*api.CreatePayload))
  168. case HookEventPush:
  169. return getDingtalkPushPayload(p.(*api.PushPayload))
  170. case HookEventPullRequest:
  171. return getDingtalkPullRequestPayload(p.(*api.PullRequestPayload))
  172. case HookEventRepository:
  173. return getDingtalkRepositoryPayload(p.(*api.RepositoryPayload))
  174. }
  175. return s, nil
  176. }