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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. 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 := make([]string, len(p.PullRequest.Assignees))
  212. for i, user := range p.PullRequest.Assignees {
  213. list[i] = user.UserName
  214. }
  215. title = fmt.Sprintf("[%s] Pull request assigned to %s: #%d %s", p.Repository.FullName,
  216. strings.Join(list, ", "),
  217. p.Index, p.PullRequest.Title)
  218. text = p.PullRequest.Body
  219. case api.HookIssueUnassigned:
  220. title = fmt.Sprintf("[%s] Pull request unassigned: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  221. text = p.PullRequest.Body
  222. case api.HookIssueLabelUpdated:
  223. title = fmt.Sprintf("[%s] Pull request labels updated: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  224. text = p.PullRequest.Body
  225. case api.HookIssueLabelCleared:
  226. title = fmt.Sprintf("[%s] Pull request labels cleared: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  227. text = p.PullRequest.Body
  228. case api.HookIssueSynchronized:
  229. title = fmt.Sprintf("[%s] Pull request synchronized: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  230. text = p.PullRequest.Body
  231. case api.HookIssueMilestoned:
  232. title = fmt.Sprintf("[%s] Pull request milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  233. text = p.PullRequest.Body
  234. case api.HookIssueDemilestoned:
  235. title = fmt.Sprintf("[%s] Pull request clear milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  236. text = p.PullRequest.Body
  237. }
  238. return &DingtalkPayload{
  239. MsgType: "actionCard",
  240. ActionCard: dingtalk.ActionCard{
  241. Text: title + "\r\n\r\n" + text,
  242. //Markdown: "# " + title + "\n" + text,
  243. Title: title,
  244. HideAvatar: "0",
  245. SingleTitle: "view pull request",
  246. SingleURL: p.PullRequest.HTMLURL,
  247. },
  248. }, nil
  249. }
  250. func getDingtalkPullRequestApprovalPayload(p *api.PullRequestPayload, event HookEventType) (*DingtalkPayload, error) {
  251. var text, title string
  252. switch p.Action {
  253. case api.HookIssueSynchronized:
  254. action, err := parseHookPullRequestEventType(event)
  255. if err != nil {
  256. return nil, err
  257. }
  258. title = fmt.Sprintf("[%s] Pull request review %s : #%d %s", p.Repository.FullName, action, p.Index, p.PullRequest.Title)
  259. text = p.PullRequest.Body
  260. }
  261. return &DingtalkPayload{
  262. MsgType: "actionCard",
  263. ActionCard: dingtalk.ActionCard{
  264. Text: title + "\r\n\r\n" + text,
  265. Title: title,
  266. HideAvatar: "0",
  267. SingleTitle: "view pull request",
  268. SingleURL: p.PullRequest.HTMLURL,
  269. },
  270. }, nil
  271. }
  272. func getDingtalkRepositoryPayload(p *api.RepositoryPayload) (*DingtalkPayload, error) {
  273. var title, url string
  274. switch p.Action {
  275. case api.HookRepoCreated:
  276. title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
  277. url = p.Repository.HTMLURL
  278. return &DingtalkPayload{
  279. MsgType: "actionCard",
  280. ActionCard: dingtalk.ActionCard{
  281. Text: title,
  282. Title: title,
  283. HideAvatar: "0",
  284. SingleTitle: "view repository",
  285. SingleURL: url,
  286. },
  287. }, nil
  288. case api.HookRepoDeleted:
  289. title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
  290. return &DingtalkPayload{
  291. MsgType: "text",
  292. Text: struct {
  293. Content string `json:"content"`
  294. }{
  295. Content: title,
  296. },
  297. }, nil
  298. }
  299. return nil, nil
  300. }
  301. func getDingtalkReleasePayload(p *api.ReleasePayload) (*DingtalkPayload, error) {
  302. var title, url string
  303. switch p.Action {
  304. case api.HookReleasePublished:
  305. title = fmt.Sprintf("[%s] Release created", p.Release.TagName)
  306. url = p.Release.URL
  307. return &DingtalkPayload{
  308. MsgType: "actionCard",
  309. ActionCard: dingtalk.ActionCard{
  310. Text: title,
  311. Title: title,
  312. HideAvatar: "0",
  313. SingleTitle: "view release",
  314. SingleURL: url,
  315. },
  316. }, nil
  317. case api.HookReleaseUpdated:
  318. title = fmt.Sprintf("[%s] Release updated", 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.HookReleaseDeleted:
  331. title = fmt.Sprintf("[%s] Release deleted", 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. }
  344. return nil, nil
  345. }
  346. // GetDingtalkPayload converts a ding talk webhook into a DingtalkPayload
  347. func GetDingtalkPayload(p api.Payloader, event HookEventType, meta string) (*DingtalkPayload, error) {
  348. s := new(DingtalkPayload)
  349. switch event {
  350. case HookEventCreate:
  351. return getDingtalkCreatePayload(p.(*api.CreatePayload))
  352. case HookEventDelete:
  353. return getDingtalkDeletePayload(p.(*api.DeletePayload))
  354. case HookEventFork:
  355. return getDingtalkForkPayload(p.(*api.ForkPayload))
  356. case HookEventIssues:
  357. return getDingtalkIssuesPayload(p.(*api.IssuePayload))
  358. case HookEventIssueComment:
  359. return getDingtalkIssueCommentPayload(p.(*api.IssueCommentPayload))
  360. case HookEventPush:
  361. return getDingtalkPushPayload(p.(*api.PushPayload))
  362. case HookEventPullRequest:
  363. return getDingtalkPullRequestPayload(p.(*api.PullRequestPayload))
  364. case HookEventPullRequestApproved, HookEventPullRequestRejected, HookEventPullRequestComment:
  365. return getDingtalkPullRequestApprovalPayload(p.(*api.PullRequestPayload), event)
  366. case HookEventRepository:
  367. return getDingtalkRepositoryPayload(p.(*api.RepositoryPayload))
  368. case HookEventRelease:
  369. return getDingtalkReleasePayload(p.(*api.ReleasePayload))
  370. }
  371. return s, nil
  372. }