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.

379 lines
12 KiB

  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 ref %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 ref %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] Issue 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] Issue 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] Issue synchronized: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  144. text = p.Issue.Body
  145. case api.HookIssueMilestoned:
  146. title = fmt.Sprintf("[%s] Issue milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  147. text = p.Issue.Body
  148. case api.HookIssueDemilestoned:
  149. title = fmt.Sprintf("[%s] Issue clear milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  150. text = p.Issue.Body
  151. }
  152. return &DingtalkPayload{
  153. MsgType: "actionCard",
  154. ActionCard: dingtalk.ActionCard{
  155. Text: title + "\r\n\r\n" + text,
  156. //Markdown: "# " + title + "\n" + text,
  157. Title: title,
  158. HideAvatar: "0",
  159. SingleTitle: "view issue",
  160. SingleURL: p.Issue.URL,
  161. },
  162. }, nil
  163. }
  164. func getDingtalkIssueCommentPayload(p *api.IssueCommentPayload) (*DingtalkPayload, error) {
  165. title := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
  166. url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, CommentHashTag(p.Comment.ID))
  167. var content string
  168. switch p.Action {
  169. case api.HookIssueCommentCreated:
  170. title = "New comment: " + title
  171. content = p.Comment.Body
  172. case api.HookIssueCommentEdited:
  173. title = "Comment edited: " + title
  174. content = p.Comment.Body
  175. case api.HookIssueCommentDeleted:
  176. title = "Comment deleted: " + title
  177. url = fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
  178. content = p.Comment.Body
  179. }
  180. return &DingtalkPayload{
  181. MsgType: "actionCard",
  182. ActionCard: dingtalk.ActionCard{
  183. Text: title + "\r\n\r\n" + content,
  184. Title: title,
  185. HideAvatar: "0",
  186. SingleTitle: "view issue comment",
  187. SingleURL: url,
  188. },
  189. }, nil
  190. }
  191. func getDingtalkPullRequestPayload(p *api.PullRequestPayload) (*DingtalkPayload, error) {
  192. var text, title string
  193. switch p.Action {
  194. case api.HookIssueOpened:
  195. title = fmt.Sprintf("[%s] Pull request opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  196. text = p.PullRequest.Body
  197. case api.HookIssueClosed:
  198. if p.PullRequest.HasMerged {
  199. title = fmt.Sprintf("[%s] Pull request merged: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  200. } else {
  201. title = fmt.Sprintf("[%s] Pull request closed: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  202. }
  203. text = p.PullRequest.Body
  204. case api.HookIssueReOpened:
  205. title = fmt.Sprintf("[%s] Pull request re-opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  206. text = p.PullRequest.Body
  207. case api.HookIssueEdited:
  208. title = fmt.Sprintf("[%s] Pull request edited: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  209. text = p.PullRequest.Body
  210. case api.HookIssueAssigned:
  211. list, err := MakeAssigneeList(&Issue{ID: p.PullRequest.ID})
  212. if err != nil {
  213. return &DingtalkPayload{}, err
  214. }
  215. title = fmt.Sprintf("[%s] Pull request assigned to %s: #%d %s", p.Repository.FullName,
  216. list, p.Index, p.PullRequest.Title)
  217. text = p.PullRequest.Body
  218. case api.HookIssueUnassigned:
  219. title = fmt.Sprintf("[%s] Pull request unassigned: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  220. text = p.PullRequest.Body
  221. case api.HookIssueLabelUpdated:
  222. title = fmt.Sprintf("[%s] Pull request labels updated: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  223. text = p.PullRequest.Body
  224. case api.HookIssueLabelCleared:
  225. title = fmt.Sprintf("[%s] Pull request labels cleared: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  226. text = p.PullRequest.Body
  227. case api.HookIssueSynchronized:
  228. title = fmt.Sprintf("[%s] Pull request synchronized: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  229. text = p.PullRequest.Body
  230. case api.HookIssueMilestoned:
  231. title = fmt.Sprintf("[%s] Pull request milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  232. text = p.PullRequest.Body
  233. case api.HookIssueDemilestoned:
  234. title = fmt.Sprintf("[%s] Pull request clear milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  235. text = p.PullRequest.Body
  236. }
  237. return &DingtalkPayload{
  238. MsgType: "actionCard",
  239. ActionCard: dingtalk.ActionCard{
  240. Text: title + "\r\n\r\n" + text,
  241. //Markdown: "# " + title + "\n" + text,
  242. Title: title,
  243. HideAvatar: "0",
  244. SingleTitle: "view pull request",
  245. SingleURL: p.PullRequest.HTMLURL,
  246. },
  247. }, nil
  248. }
  249. func getDingtalkRepositoryPayload(p *api.RepositoryPayload) (*DingtalkPayload, error) {
  250. var title, url string
  251. switch p.Action {
  252. case api.HookRepoCreated:
  253. title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
  254. url = p.Repository.HTMLURL
  255. return &DingtalkPayload{
  256. MsgType: "actionCard",
  257. ActionCard: dingtalk.ActionCard{
  258. Text: title,
  259. Title: title,
  260. HideAvatar: "0",
  261. SingleTitle: "view repository",
  262. SingleURL: url,
  263. },
  264. }, nil
  265. case api.HookRepoDeleted:
  266. title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
  267. return &DingtalkPayload{
  268. MsgType: "text",
  269. Text: struct {
  270. Content string `json:"content"`
  271. }{
  272. Content: title,
  273. },
  274. }, nil
  275. }
  276. return nil, nil
  277. }
  278. func getDingtalkReleasePayload(p *api.ReleasePayload) (*DingtalkPayload, error) {
  279. var title, url string
  280. switch p.Action {
  281. case api.HookReleasePublished:
  282. title = fmt.Sprintf("[%s] Release created", p.Release.TagName)
  283. url = p.Release.URL
  284. return &DingtalkPayload{
  285. MsgType: "actionCard",
  286. ActionCard: dingtalk.ActionCard{
  287. Text: title,
  288. Title: title,
  289. HideAvatar: "0",
  290. SingleTitle: "view release",
  291. SingleURL: url,
  292. },
  293. }, nil
  294. case api.HookReleaseUpdated:
  295. title = fmt.Sprintf("[%s] Release updated", p.Release.TagName)
  296. url = p.Release.URL
  297. return &DingtalkPayload{
  298. MsgType: "actionCard",
  299. ActionCard: dingtalk.ActionCard{
  300. Text: title,
  301. Title: title,
  302. HideAvatar: "0",
  303. SingleTitle: "view release",
  304. SingleURL: url,
  305. },
  306. }, nil
  307. case api.HookReleaseDeleted:
  308. title = fmt.Sprintf("[%s] Release deleted", p.Release.TagName)
  309. url = p.Release.URL
  310. return &DingtalkPayload{
  311. MsgType: "actionCard",
  312. ActionCard: dingtalk.ActionCard{
  313. Text: title,
  314. Title: title,
  315. HideAvatar: "0",
  316. SingleTitle: "view release",
  317. SingleURL: url,
  318. },
  319. }, nil
  320. }
  321. return nil, nil
  322. }
  323. // GetDingtalkPayload converts a ding talk webhook into a DingtalkPayload
  324. func GetDingtalkPayload(p api.Payloader, event HookEventType, meta string) (*DingtalkPayload, error) {
  325. s := new(DingtalkPayload)
  326. switch event {
  327. case HookEventCreate:
  328. return getDingtalkCreatePayload(p.(*api.CreatePayload))
  329. case HookEventDelete:
  330. return getDingtalkDeletePayload(p.(*api.DeletePayload))
  331. case HookEventFork:
  332. return getDingtalkForkPayload(p.(*api.ForkPayload))
  333. case HookEventIssues:
  334. return getDingtalkIssuesPayload(p.(*api.IssuePayload))
  335. case HookEventIssueComment:
  336. return getDingtalkIssueCommentPayload(p.(*api.IssueCommentPayload))
  337. case HookEventPush:
  338. return getDingtalkPushPayload(p.(*api.PushPayload))
  339. case HookEventPullRequest:
  340. return getDingtalkPullRequestPayload(p.(*api.PullRequestPayload))
  341. case HookEventRepository:
  342. return getDingtalkRepositoryPayload(p.(*api.RepositoryPayload))
  343. case HookEventRelease:
  344. return getDingtalkReleasePayload(p.(*api.ReleasePayload))
  345. }
  346. return s, nil
  347. }