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 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 getDingtalkDeletePayload(p *api.DeletePayload) (*DingtalkPayload, error) {
  43. // created tag/branch
  44. refName := git.RefEndName(p.Ref)
  45. title := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName)
  46. return &DingtalkPayload{
  47. MsgType: "actionCard",
  48. ActionCard: dingtalk.ActionCard{
  49. Text: title,
  50. Title: title,
  51. HideAvatar: "0",
  52. SingleTitle: fmt.Sprintf("view branch %s", refName),
  53. SingleURL: p.Repo.HTMLURL + "/src/" + refName,
  54. },
  55. }, nil
  56. }
  57. func getDingtalkForkPayload(p *api.ForkPayload) (*DingtalkPayload, error) {
  58. title := fmt.Sprintf("%s is forked to %s", p.Forkee.FullName, p.Repo.FullName)
  59. return &DingtalkPayload{
  60. MsgType: "actionCard",
  61. ActionCard: dingtalk.ActionCard{
  62. Text: title,
  63. Title: title,
  64. HideAvatar: "0",
  65. SingleTitle: fmt.Sprintf("view forked repo %s", p.Repo.FullName),
  66. SingleURL: p.Repo.HTMLURL,
  67. },
  68. }, nil
  69. }
  70. func getDingtalkPushPayload(p *api.PushPayload) (*DingtalkPayload, error) {
  71. var (
  72. branchName = git.RefEndName(p.Ref)
  73. commitDesc string
  74. )
  75. var titleLink, linkText string
  76. if len(p.Commits) == 1 {
  77. commitDesc = "1 new commit"
  78. titleLink = p.Commits[0].URL
  79. linkText = fmt.Sprintf("view commit %s", p.Commits[0].ID[:7])
  80. } else {
  81. commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
  82. titleLink = p.CompareURL
  83. linkText = fmt.Sprintf("view commit %s...%s", p.Commits[0].ID[:7], p.Commits[len(p.Commits)-1].ID[:7])
  84. }
  85. if titleLink == "" {
  86. titleLink = p.Repo.HTMLURL + "/src/" + branchName
  87. }
  88. title := fmt.Sprintf("[%s:%s] %s", p.Repo.FullName, branchName, commitDesc)
  89. var text string
  90. // for each commit, generate attachment text
  91. for i, commit := range p.Commits {
  92. var authorName string
  93. if commit.Author != nil {
  94. authorName = " - " + commit.Author.Name
  95. }
  96. text += fmt.Sprintf("[%s](%s) %s", commit.ID[:7], commit.URL,
  97. strings.TrimRight(commit.Message, "\r\n")) + authorName
  98. // add linebreak to each commit but the last
  99. if i < len(p.Commits)-1 {
  100. text += "\n"
  101. }
  102. }
  103. return &DingtalkPayload{
  104. MsgType: "actionCard",
  105. ActionCard: dingtalk.ActionCard{
  106. Text: text,
  107. Title: title,
  108. HideAvatar: "0",
  109. SingleTitle: linkText,
  110. SingleURL: titleLink,
  111. },
  112. }, nil
  113. }
  114. func getDingtalkIssuesPayload(p *api.IssuePayload) (*DingtalkPayload, error) {
  115. var text, title string
  116. switch p.Action {
  117. case api.HookIssueOpened:
  118. title = fmt.Sprintf("[%s] Issue opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  119. text = p.Issue.Body
  120. case api.HookIssueClosed:
  121. title = fmt.Sprintf("[%s] Issue closed: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  122. text = p.Issue.Body
  123. case api.HookIssueReOpened:
  124. title = fmt.Sprintf("[%s] Issue re-opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  125. text = p.Issue.Body
  126. case api.HookIssueEdited:
  127. title = fmt.Sprintf("[%s] Issue edited: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  128. text = p.Issue.Body
  129. case api.HookIssueAssigned:
  130. title = fmt.Sprintf("[%s] Issue assigned to %s: #%d %s", p.Repository.FullName,
  131. p.Issue.Assignee.UserName, p.Index, p.Issue.Title)
  132. text = p.Issue.Body
  133. case api.HookIssueUnassigned:
  134. title = fmt.Sprintf("[%s] Issue unassigned: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  135. text = p.Issue.Body
  136. case api.HookIssueLabelUpdated:
  137. title = fmt.Sprintf("[%s] Pull request labels updated: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  138. text = p.Issue.Body
  139. case api.HookIssueLabelCleared:
  140. title = fmt.Sprintf("[%s] Pull request labels cleared: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  141. text = p.Issue.Body
  142. case api.HookIssueSynchronized:
  143. title = fmt.Sprintf("[%s] Pull request synchronized: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  144. text = p.Issue.Body
  145. }
  146. return &DingtalkPayload{
  147. MsgType: "actionCard",
  148. ActionCard: dingtalk.ActionCard{
  149. Text: text,
  150. Title: title,
  151. HideAvatar: "0",
  152. SingleTitle: "view pull request",
  153. SingleURL: p.Issue.URL,
  154. },
  155. }, nil
  156. }
  157. func getDingtalkIssueCommentPayload(p *api.IssueCommentPayload) (*DingtalkPayload, error) {
  158. title := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
  159. url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, CommentHashTag(p.Comment.ID))
  160. var content string
  161. switch p.Action {
  162. case api.HookIssueCommentCreated:
  163. title = "New comment: " + title
  164. content = p.Comment.Body
  165. case api.HookIssueCommentEdited:
  166. title = "Comment edited: " + title
  167. content = p.Comment.Body
  168. case api.HookIssueCommentDeleted:
  169. title = "Comment deleted: " + title
  170. url = fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
  171. content = p.Comment.Body
  172. }
  173. return &DingtalkPayload{
  174. MsgType: "actionCard",
  175. ActionCard: dingtalk.ActionCard{
  176. Text: content,
  177. Title: title,
  178. HideAvatar: "0",
  179. SingleTitle: "view pull request",
  180. SingleURL: url,
  181. },
  182. }, nil
  183. }
  184. func getDingtalkPullRequestPayload(p *api.PullRequestPayload) (*DingtalkPayload, error) {
  185. var text, title string
  186. switch p.Action {
  187. case api.HookIssueOpened:
  188. title = fmt.Sprintf("[%s] Pull request opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  189. text = p.PullRequest.Body
  190. case api.HookIssueClosed:
  191. if p.PullRequest.HasMerged {
  192. title = fmt.Sprintf("[%s] Pull request merged: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  193. } else {
  194. title = fmt.Sprintf("[%s] Pull request closed: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  195. }
  196. text = p.PullRequest.Body
  197. case api.HookIssueReOpened:
  198. title = fmt.Sprintf("[%s] Pull request re-opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  199. text = p.PullRequest.Body
  200. case api.HookIssueEdited:
  201. title = fmt.Sprintf("[%s] Pull request edited: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  202. text = p.PullRequest.Body
  203. case api.HookIssueAssigned:
  204. list, err := MakeAssigneeList(&Issue{ID: p.PullRequest.ID})
  205. if err != nil {
  206. return &DingtalkPayload{}, err
  207. }
  208. title = fmt.Sprintf("[%s] Pull request assigned to %s: #%d %s", p.Repository.FullName,
  209. list, p.Index, p.PullRequest.Title)
  210. text = p.PullRequest.Body
  211. case api.HookIssueUnassigned:
  212. title = fmt.Sprintf("[%s] Pull request unassigned: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  213. text = p.PullRequest.Body
  214. case api.HookIssueLabelUpdated:
  215. title = fmt.Sprintf("[%s] Pull request labels updated: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  216. text = p.PullRequest.Body
  217. case api.HookIssueLabelCleared:
  218. title = fmt.Sprintf("[%s] Pull request labels cleared: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  219. text = p.PullRequest.Body
  220. case api.HookIssueSynchronized:
  221. title = fmt.Sprintf("[%s] Pull request synchronized: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  222. text = p.PullRequest.Body
  223. }
  224. return &DingtalkPayload{
  225. MsgType: "actionCard",
  226. ActionCard: dingtalk.ActionCard{
  227. Text: text,
  228. Title: title,
  229. HideAvatar: "0",
  230. SingleTitle: "view pull request",
  231. SingleURL: p.PullRequest.HTMLURL,
  232. },
  233. }, nil
  234. }
  235. func getDingtalkRepositoryPayload(p *api.RepositoryPayload) (*DingtalkPayload, error) {
  236. var title, url string
  237. switch p.Action {
  238. case api.HookRepoCreated:
  239. title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
  240. url = p.Repository.HTMLURL
  241. return &DingtalkPayload{
  242. MsgType: "actionCard",
  243. ActionCard: dingtalk.ActionCard{
  244. Text: title,
  245. Title: title,
  246. HideAvatar: "0",
  247. SingleTitle: "view repository",
  248. SingleURL: url,
  249. },
  250. }, nil
  251. case api.HookRepoDeleted:
  252. title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
  253. return &DingtalkPayload{
  254. MsgType: "text",
  255. Text: struct {
  256. Content string `json:"content"`
  257. }{
  258. Content: title,
  259. },
  260. }, nil
  261. }
  262. return nil, nil
  263. }
  264. func getDingtalkReleasePayload(p *api.ReleasePayload) (*DingtalkPayload, error) {
  265. var title, url string
  266. switch p.Action {
  267. case api.HookReleasePublished:
  268. title = fmt.Sprintf("[%s] Release created", p.Release.TagName)
  269. url = p.Release.URL
  270. return &DingtalkPayload{
  271. MsgType: "actionCard",
  272. ActionCard: dingtalk.ActionCard{
  273. Text: title,
  274. Title: title,
  275. HideAvatar: "0",
  276. SingleTitle: "view repository",
  277. SingleURL: url,
  278. },
  279. }, nil
  280. }
  281. return nil, nil
  282. }
  283. // GetDingtalkPayload converts a ding talk webhook into a DingtalkPayload
  284. func GetDingtalkPayload(p api.Payloader, event HookEventType, meta string) (*DingtalkPayload, error) {
  285. s := new(DingtalkPayload)
  286. switch event {
  287. case HookEventCreate:
  288. return getDingtalkCreatePayload(p.(*api.CreatePayload))
  289. case HookEventDelete:
  290. return getDingtalkDeletePayload(p.(*api.DeletePayload))
  291. case HookEventFork:
  292. return getDingtalkForkPayload(p.(*api.ForkPayload))
  293. case HookEventIssues:
  294. return getDingtalkIssuesPayload(p.(*api.IssuePayload))
  295. case HookEventIssueComment:
  296. return getDingtalkIssueCommentPayload(p.(*api.IssueCommentPayload))
  297. case HookEventPush:
  298. return getDingtalkPushPayload(p.(*api.PushPayload))
  299. case HookEventPullRequest:
  300. return getDingtalkPullRequestPayload(p.(*api.PullRequestPayload))
  301. case HookEventRepository:
  302. return getDingtalkRepositoryPayload(p.(*api.RepositoryPayload))
  303. case HookEventRelease:
  304. return getDingtalkReleasePayload(p.(*api.ReleasePayload))
  305. }
  306. return s, nil
  307. }