本站源代码
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.

423 lines
13KB

  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/gitea/modules/git"
  10. api "code.gitea.io/gitea/modules/structs"
  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. if p.IsPull {
  171. title = "New comment on pull request " + title
  172. } else {
  173. title = "New comment on issue " + title
  174. }
  175. content = p.Comment.Body
  176. case api.HookIssueCommentEdited:
  177. if p.IsPull {
  178. title = "Comment edited on pull request " + title
  179. } else {
  180. title = "Comment edited on issue " + title
  181. }
  182. content = p.Comment.Body
  183. case api.HookIssueCommentDeleted:
  184. if p.IsPull {
  185. title = "Comment deleted on pull request " + title
  186. } else {
  187. title = "Comment deleted on issue " + title
  188. }
  189. url = fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
  190. content = p.Comment.Body
  191. }
  192. title = fmt.Sprintf("[%s] %s", p.Repository.FullName, title)
  193. return &DingtalkPayload{
  194. MsgType: "actionCard",
  195. ActionCard: dingtalk.ActionCard{
  196. Text: title + "\r\n\r\n" + content,
  197. Title: title,
  198. HideAvatar: "0",
  199. SingleTitle: "view issue comment",
  200. SingleURL: url,
  201. },
  202. }, nil
  203. }
  204. func getDingtalkPullRequestPayload(p *api.PullRequestPayload) (*DingtalkPayload, error) {
  205. var text, title string
  206. switch p.Action {
  207. case api.HookIssueOpened:
  208. title = fmt.Sprintf("[%s] Pull request opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  209. text = p.PullRequest.Body
  210. case api.HookIssueClosed:
  211. if p.PullRequest.HasMerged {
  212. title = fmt.Sprintf("[%s] Pull request merged: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  213. } else {
  214. title = fmt.Sprintf("[%s] Pull request closed: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  215. }
  216. text = p.PullRequest.Body
  217. case api.HookIssueReOpened:
  218. title = fmt.Sprintf("[%s] Pull request re-opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  219. text = p.PullRequest.Body
  220. case api.HookIssueEdited:
  221. title = fmt.Sprintf("[%s] Pull request edited: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  222. text = p.PullRequest.Body
  223. case api.HookIssueAssigned:
  224. list := make([]string, len(p.PullRequest.Assignees))
  225. for i, user := range p.PullRequest.Assignees {
  226. list[i] = user.UserName
  227. }
  228. title = fmt.Sprintf("[%s] Pull request assigned to %s: #%d %s", p.Repository.FullName,
  229. strings.Join(list, ", "),
  230. p.Index, p.PullRequest.Title)
  231. text = p.PullRequest.Body
  232. case api.HookIssueUnassigned:
  233. title = fmt.Sprintf("[%s] Pull request unassigned: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  234. text = p.PullRequest.Body
  235. case api.HookIssueLabelUpdated:
  236. title = fmt.Sprintf("[%s] Pull request labels updated: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  237. text = p.PullRequest.Body
  238. case api.HookIssueLabelCleared:
  239. title = fmt.Sprintf("[%s] Pull request labels cleared: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  240. text = p.PullRequest.Body
  241. case api.HookIssueSynchronized:
  242. title = fmt.Sprintf("[%s] Pull request synchronized: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  243. text = p.PullRequest.Body
  244. case api.HookIssueMilestoned:
  245. title = fmt.Sprintf("[%s] Pull request milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  246. text = p.PullRequest.Body
  247. case api.HookIssueDemilestoned:
  248. title = fmt.Sprintf("[%s] Pull request clear milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  249. text = p.PullRequest.Body
  250. }
  251. return &DingtalkPayload{
  252. MsgType: "actionCard",
  253. ActionCard: dingtalk.ActionCard{
  254. Text: title + "\r\n\r\n" + text,
  255. //Markdown: "# " + title + "\n" + text,
  256. Title: title,
  257. HideAvatar: "0",
  258. SingleTitle: "view pull request",
  259. SingleURL: p.PullRequest.HTMLURL,
  260. },
  261. }, nil
  262. }
  263. func getDingtalkPullRequestApprovalPayload(p *api.PullRequestPayload, event HookEventType) (*DingtalkPayload, error) {
  264. var text, title string
  265. switch p.Action {
  266. case api.HookIssueSynchronized:
  267. action, err := parseHookPullRequestEventType(event)
  268. if err != nil {
  269. return nil, err
  270. }
  271. title = fmt.Sprintf("[%s] Pull request review %s : #%d %s", p.Repository.FullName, action, p.Index, p.PullRequest.Title)
  272. text = p.Review.Content
  273. }
  274. return &DingtalkPayload{
  275. MsgType: "actionCard",
  276. ActionCard: dingtalk.ActionCard{
  277. Text: title + "\r\n\r\n" + text,
  278. Title: title,
  279. HideAvatar: "0",
  280. SingleTitle: "view pull request",
  281. SingleURL: p.PullRequest.HTMLURL,
  282. },
  283. }, nil
  284. }
  285. func getDingtalkRepositoryPayload(p *api.RepositoryPayload) (*DingtalkPayload, error) {
  286. var title, url string
  287. switch p.Action {
  288. case api.HookRepoCreated:
  289. title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
  290. url = p.Repository.HTMLURL
  291. return &DingtalkPayload{
  292. MsgType: "actionCard",
  293. ActionCard: dingtalk.ActionCard{
  294. Text: title,
  295. Title: title,
  296. HideAvatar: "0",
  297. SingleTitle: "view repository",
  298. SingleURL: url,
  299. },
  300. }, nil
  301. case api.HookRepoDeleted:
  302. title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
  303. return &DingtalkPayload{
  304. MsgType: "text",
  305. Text: struct {
  306. Content string `json:"content"`
  307. }{
  308. Content: title,
  309. },
  310. }, nil
  311. }
  312. return nil, nil
  313. }
  314. func getDingtalkReleasePayload(p *api.ReleasePayload) (*DingtalkPayload, error) {
  315. var title, url string
  316. switch p.Action {
  317. case api.HookReleasePublished:
  318. title = fmt.Sprintf("[%s] Release created", p.Release.TagName)
  319. url = p.Release.URL
  320. return &DingtalkPayload{
  321. MsgType: "actionCard",
  322. ActionCard: dingtalk.ActionCard{
  323. Text: title,
  324. Title: title,
  325. HideAvatar: "0",
  326. SingleTitle: "view release",
  327. SingleURL: url,
  328. },
  329. }, nil
  330. case api.HookReleaseUpdated:
  331. title = fmt.Sprintf("[%s] Release updated", p.Release.TagName)
  332. url = p.Release.URL
  333. return &DingtalkPayload{
  334. MsgType: "actionCard",
  335. ActionCard: dingtalk.ActionCard{
  336. Text: title,
  337. Title: title,
  338. HideAvatar: "0",
  339. SingleTitle: "view release",
  340. SingleURL: url,
  341. },
  342. }, nil
  343. case api.HookReleaseDeleted:
  344. title = fmt.Sprintf("[%s] Release deleted", p.Release.TagName)
  345. url = p.Release.URL
  346. return &DingtalkPayload{
  347. MsgType: "actionCard",
  348. ActionCard: dingtalk.ActionCard{
  349. Text: title,
  350. Title: title,
  351. HideAvatar: "0",
  352. SingleTitle: "view release",
  353. SingleURL: url,
  354. },
  355. }, nil
  356. }
  357. return nil, nil
  358. }
  359. // GetDingtalkPayload converts a ding talk webhook into a DingtalkPayload
  360. func GetDingtalkPayload(p api.Payloader, event HookEventType, meta string) (*DingtalkPayload, error) {
  361. s := new(DingtalkPayload)
  362. switch event {
  363. case HookEventCreate:
  364. return getDingtalkCreatePayload(p.(*api.CreatePayload))
  365. case HookEventDelete:
  366. return getDingtalkDeletePayload(p.(*api.DeletePayload))
  367. case HookEventFork:
  368. return getDingtalkForkPayload(p.(*api.ForkPayload))
  369. case HookEventIssues:
  370. return getDingtalkIssuesPayload(p.(*api.IssuePayload))
  371. case HookEventIssueComment:
  372. return getDingtalkIssueCommentPayload(p.(*api.IssueCommentPayload))
  373. case HookEventPush:
  374. return getDingtalkPushPayload(p.(*api.PushPayload))
  375. case HookEventPullRequest:
  376. return getDingtalkPullRequestPayload(p.(*api.PullRequestPayload))
  377. case HookEventPullRequestApproved, HookEventPullRequestRejected, HookEventPullRequestComment:
  378. return getDingtalkPullRequestApprovalPayload(p.(*api.PullRequestPayload), event)
  379. case HookEventRepository:
  380. return getDingtalkRepositoryPayload(p.(*api.RepositoryPayload))
  381. case HookEventRelease:
  382. return getDingtalkReleasePayload(p.(*api.ReleasePayload))
  383. }
  384. return s, nil
  385. }
上海开阖软件有限公司 沪ICP备12045867号-1