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.

dingtalk.go 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 webhook
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "strings"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/git"
  11. api "code.gitea.io/gitea/modules/structs"
  12. dingtalk "github.com/lunny/dingtalk_webhook"
  13. )
  14. type (
  15. // DingtalkPayload represents
  16. DingtalkPayload dingtalk.Payload
  17. )
  18. // SetSecret sets the dingtalk secret
  19. func (p *DingtalkPayload) SetSecret(_ string) {}
  20. // JSONPayload Marshals the DingtalkPayload to json
  21. func (p *DingtalkPayload) JSONPayload() ([]byte, error) {
  22. data, err := json.MarshalIndent(p, "", " ")
  23. if err != nil {
  24. return []byte{}, err
  25. }
  26. return data, nil
  27. }
  28. func getDingtalkCreatePayload(p *api.CreatePayload) (*DingtalkPayload, error) {
  29. // created tag/branch
  30. refName := git.RefEndName(p.Ref)
  31. title := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName)
  32. return &DingtalkPayload{
  33. MsgType: "actionCard",
  34. ActionCard: dingtalk.ActionCard{
  35. Text: title,
  36. Title: title,
  37. HideAvatar: "0",
  38. SingleTitle: fmt.Sprintf("view ref %s", refName),
  39. SingleURL: p.Repo.HTMLURL + "/src/" + refName,
  40. },
  41. }, nil
  42. }
  43. func getDingtalkDeletePayload(p *api.DeletePayload) (*DingtalkPayload, error) {
  44. // created tag/branch
  45. refName := git.RefEndName(p.Ref)
  46. title := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName)
  47. return &DingtalkPayload{
  48. MsgType: "actionCard",
  49. ActionCard: dingtalk.ActionCard{
  50. Text: title,
  51. Title: title,
  52. HideAvatar: "0",
  53. SingleTitle: fmt.Sprintf("view ref %s", refName),
  54. SingleURL: p.Repo.HTMLURL + "/src/" + refName,
  55. },
  56. }, nil
  57. }
  58. func getDingtalkForkPayload(p *api.ForkPayload) (*DingtalkPayload, error) {
  59. title := fmt.Sprintf("%s is forked to %s", p.Forkee.FullName, p.Repo.FullName)
  60. return &DingtalkPayload{
  61. MsgType: "actionCard",
  62. ActionCard: dingtalk.ActionCard{
  63. Text: title,
  64. Title: title,
  65. HideAvatar: "0",
  66. SingleTitle: fmt.Sprintf("view forked repo %s", p.Repo.FullName),
  67. SingleURL: p.Repo.HTMLURL,
  68. },
  69. }, nil
  70. }
  71. func getDingtalkPushPayload(p *api.PushPayload) (*DingtalkPayload, error) {
  72. var (
  73. branchName = git.RefEndName(p.Ref)
  74. commitDesc string
  75. )
  76. var titleLink, linkText string
  77. if len(p.Commits) == 1 {
  78. commitDesc = "1 new commit"
  79. titleLink = p.Commits[0].URL
  80. linkText = fmt.Sprintf("view commit %s", p.Commits[0].ID[:7])
  81. } else {
  82. commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
  83. titleLink = p.CompareURL
  84. linkText = fmt.Sprintf("view commit %s...%s", p.Commits[0].ID[:7], p.Commits[len(p.Commits)-1].ID[:7])
  85. }
  86. if titleLink == "" {
  87. titleLink = p.Repo.HTMLURL + "/src/" + branchName
  88. }
  89. title := fmt.Sprintf("[%s:%s] %s", p.Repo.FullName, branchName, commitDesc)
  90. var text string
  91. // for each commit, generate attachment text
  92. for i, commit := range p.Commits {
  93. var authorName string
  94. if commit.Author != nil {
  95. authorName = " - " + commit.Author.Name
  96. }
  97. text += fmt.Sprintf("[%s](%s) %s", commit.ID[:7], commit.URL,
  98. strings.TrimRight(commit.Message, "\r\n")) + authorName
  99. // add linebreak to each commit but the last
  100. if i < len(p.Commits)-1 {
  101. text += "\n"
  102. }
  103. }
  104. return &DingtalkPayload{
  105. MsgType: "actionCard",
  106. ActionCard: dingtalk.ActionCard{
  107. Text: text,
  108. Title: title,
  109. HideAvatar: "0",
  110. SingleTitle: linkText,
  111. SingleURL: titleLink,
  112. },
  113. }, nil
  114. }
  115. func getDingtalkIssuesPayload(p *api.IssuePayload) (*DingtalkPayload, error) {
  116. var text, title string
  117. switch p.Action {
  118. case api.HookIssueOpened:
  119. title = fmt.Sprintf("[%s] Issue opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  120. text = p.Issue.Body
  121. case api.HookIssueClosed:
  122. title = fmt.Sprintf("[%s] Issue closed: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  123. text = p.Issue.Body
  124. case api.HookIssueReOpened:
  125. title = fmt.Sprintf("[%s] Issue re-opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  126. text = p.Issue.Body
  127. case api.HookIssueEdited:
  128. title = fmt.Sprintf("[%s] Issue edited: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  129. text = p.Issue.Body
  130. case api.HookIssueAssigned:
  131. title = fmt.Sprintf("[%s] Issue assigned to %s: #%d %s", p.Repository.FullName,
  132. p.Issue.Assignee.UserName, p.Index, p.Issue.Title)
  133. text = p.Issue.Body
  134. case api.HookIssueUnassigned:
  135. title = fmt.Sprintf("[%s] Issue unassigned: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  136. text = p.Issue.Body
  137. case api.HookIssueLabelUpdated:
  138. title = fmt.Sprintf("[%s] Issue labels updated: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  139. text = p.Issue.Body
  140. case api.HookIssueLabelCleared:
  141. title = fmt.Sprintf("[%s] Issue labels cleared: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  142. text = p.Issue.Body
  143. case api.HookIssueSynchronized:
  144. title = fmt.Sprintf("[%s] Issue synchronized: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  145. text = p.Issue.Body
  146. case api.HookIssueMilestoned:
  147. title = fmt.Sprintf("[%s] Issue milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  148. text = p.Issue.Body
  149. case api.HookIssueDemilestoned:
  150. title = fmt.Sprintf("[%s] Issue clear milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title)
  151. text = p.Issue.Body
  152. }
  153. return &DingtalkPayload{
  154. MsgType: "actionCard",
  155. ActionCard: dingtalk.ActionCard{
  156. Text: title + "\r\n\r\n" + text,
  157. //Markdown: "# " + title + "\n" + text,
  158. Title: title,
  159. HideAvatar: "0",
  160. SingleTitle: "view issue",
  161. SingleURL: p.Issue.URL,
  162. },
  163. }, nil
  164. }
  165. func getDingtalkIssueCommentPayload(p *api.IssueCommentPayload) (*DingtalkPayload, error) {
  166. title := fmt.Sprintf("#%d: %s", p.Issue.Index, p.Issue.Title)
  167. url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, models.CommentHashTag(p.Comment.ID))
  168. var content string
  169. switch p.Action {
  170. case api.HookIssueCommentCreated:
  171. if p.IsPull {
  172. title = "New comment on pull request " + title
  173. } else {
  174. title = "New comment on issue " + title
  175. }
  176. content = p.Comment.Body
  177. case api.HookIssueCommentEdited:
  178. if p.IsPull {
  179. title = "Comment edited on pull request " + title
  180. } else {
  181. title = "Comment edited on issue " + title
  182. }
  183. content = p.Comment.Body
  184. case api.HookIssueCommentDeleted:
  185. if p.IsPull {
  186. title = "Comment deleted on pull request " + title
  187. } else {
  188. title = "Comment deleted on issue " + title
  189. }
  190. url = fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
  191. content = p.Comment.Body
  192. }
  193. title = fmt.Sprintf("[%s] %s", p.Repository.FullName, title)
  194. return &DingtalkPayload{
  195. MsgType: "actionCard",
  196. ActionCard: dingtalk.ActionCard{
  197. Text: title + "\r\n\r\n" + content,
  198. Title: title,
  199. HideAvatar: "0",
  200. SingleTitle: "view issue comment",
  201. SingleURL: url,
  202. },
  203. }, nil
  204. }
  205. func getDingtalkPullRequestPayload(p *api.PullRequestPayload) (*DingtalkPayload, error) {
  206. var text, title string
  207. switch p.Action {
  208. case api.HookIssueOpened:
  209. title = fmt.Sprintf("[%s] Pull request opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  210. text = p.PullRequest.Body
  211. case api.HookIssueClosed:
  212. if p.PullRequest.HasMerged {
  213. title = fmt.Sprintf("[%s] Pull request merged: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  214. } else {
  215. title = fmt.Sprintf("[%s] Pull request closed: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  216. }
  217. text = p.PullRequest.Body
  218. case api.HookIssueReOpened:
  219. title = fmt.Sprintf("[%s] Pull request re-opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  220. text = p.PullRequest.Body
  221. case api.HookIssueEdited:
  222. title = fmt.Sprintf("[%s] Pull request edited: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  223. text = p.PullRequest.Body
  224. case api.HookIssueAssigned:
  225. list := make([]string, len(p.PullRequest.Assignees))
  226. for i, user := range p.PullRequest.Assignees {
  227. list[i] = user.UserName
  228. }
  229. title = fmt.Sprintf("[%s] Pull request assigned to %s: #%d %s", p.Repository.FullName,
  230. strings.Join(list, ", "),
  231. p.Index, p.PullRequest.Title)
  232. text = p.PullRequest.Body
  233. case api.HookIssueUnassigned:
  234. title = fmt.Sprintf("[%s] Pull request unassigned: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  235. text = p.PullRequest.Body
  236. case api.HookIssueLabelUpdated:
  237. title = fmt.Sprintf("[%s] Pull request labels updated: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  238. text = p.PullRequest.Body
  239. case api.HookIssueLabelCleared:
  240. title = fmt.Sprintf("[%s] Pull request labels cleared: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  241. text = p.PullRequest.Body
  242. case api.HookIssueSynchronized:
  243. title = fmt.Sprintf("[%s] Pull request synchronized: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  244. text = p.PullRequest.Body
  245. case api.HookIssueMilestoned:
  246. title = fmt.Sprintf("[%s] Pull request milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  247. text = p.PullRequest.Body
  248. case api.HookIssueDemilestoned:
  249. title = fmt.Sprintf("[%s] Pull request clear milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
  250. text = p.PullRequest.Body
  251. }
  252. return &DingtalkPayload{
  253. MsgType: "actionCard",
  254. ActionCard: dingtalk.ActionCard{
  255. Text: title + "\r\n\r\n" + text,
  256. //Markdown: "# " + title + "\n" + text,
  257. Title: title,
  258. HideAvatar: "0",
  259. SingleTitle: "view pull request",
  260. SingleURL: p.PullRequest.HTMLURL,
  261. },
  262. }, nil
  263. }
  264. func getDingtalkPullRequestApprovalPayload(p *api.PullRequestPayload, event models.HookEventType) (*DingtalkPayload, error) {
  265. var text, title string
  266. switch p.Action {
  267. case api.HookIssueSynchronized:
  268. action, err := parseHookPullRequestEventType(event)
  269. if err != nil {
  270. return nil, err
  271. }
  272. title = fmt.Sprintf("[%s] Pull request review %s : #%d %s", p.Repository.FullName, action, p.Index, p.PullRequest.Title)
  273. text = p.Review.Content
  274. }
  275. return &DingtalkPayload{
  276. MsgType: "actionCard",
  277. ActionCard: dingtalk.ActionCard{
  278. Text: title + "\r\n\r\n" + text,
  279. Title: title,
  280. HideAvatar: "0",
  281. SingleTitle: "view pull request",
  282. SingleURL: p.PullRequest.HTMLURL,
  283. },
  284. }, nil
  285. }
  286. func getDingtalkRepositoryPayload(p *api.RepositoryPayload) (*DingtalkPayload, error) {
  287. var title, url string
  288. switch p.Action {
  289. case api.HookRepoCreated:
  290. title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
  291. url = p.Repository.HTMLURL
  292. return &DingtalkPayload{
  293. MsgType: "actionCard",
  294. ActionCard: dingtalk.ActionCard{
  295. Text: title,
  296. Title: title,
  297. HideAvatar: "0",
  298. SingleTitle: "view repository",
  299. SingleURL: url,
  300. },
  301. }, nil
  302. case api.HookRepoDeleted:
  303. title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
  304. return &DingtalkPayload{
  305. MsgType: "text",
  306. Text: struct {
  307. Content string `json:"content"`
  308. }{
  309. Content: title,
  310. },
  311. }, nil
  312. }
  313. return nil, nil
  314. }
  315. func getDingtalkReleasePayload(p *api.ReleasePayload) (*DingtalkPayload, error) {
  316. var title, url string
  317. switch p.Action {
  318. case api.HookReleasePublished:
  319. title = fmt.Sprintf("[%s] Release created", p.Release.TagName)
  320. url = p.Release.URL
  321. return &DingtalkPayload{
  322. MsgType: "actionCard",
  323. ActionCard: dingtalk.ActionCard{
  324. Text: title,
  325. Title: title,
  326. HideAvatar: "0",
  327. SingleTitle: "view release",
  328. SingleURL: url,
  329. },
  330. }, nil
  331. case api.HookReleaseUpdated:
  332. title = fmt.Sprintf("[%s] Release updated", p.Release.TagName)
  333. url = p.Release.URL
  334. return &DingtalkPayload{
  335. MsgType: "actionCard",
  336. ActionCard: dingtalk.ActionCard{
  337. Text: title,
  338. Title: title,
  339. HideAvatar: "0",
  340. SingleTitle: "view release",
  341. SingleURL: url,
  342. },
  343. }, nil
  344. case api.HookReleaseDeleted:
  345. title = fmt.Sprintf("[%s] Release deleted", p.Release.TagName)
  346. url = p.Release.URL
  347. return &DingtalkPayload{
  348. MsgType: "actionCard",
  349. ActionCard: dingtalk.ActionCard{
  350. Text: title,
  351. Title: title,
  352. HideAvatar: "0",
  353. SingleTitle: "view release",
  354. SingleURL: url,
  355. },
  356. }, nil
  357. }
  358. return nil, nil
  359. }
  360. // GetDingtalkPayload converts a ding talk webhook into a DingtalkPayload
  361. func GetDingtalkPayload(p api.Payloader, event models.HookEventType, meta string) (*DingtalkPayload, error) {
  362. s := new(DingtalkPayload)
  363. switch event {
  364. case models.HookEventCreate:
  365. return getDingtalkCreatePayload(p.(*api.CreatePayload))
  366. case models.HookEventDelete:
  367. return getDingtalkDeletePayload(p.(*api.DeletePayload))
  368. case models.HookEventFork:
  369. return getDingtalkForkPayload(p.(*api.ForkPayload))
  370. case models.HookEventIssues:
  371. return getDingtalkIssuesPayload(p.(*api.IssuePayload))
  372. case models.HookEventIssueComment:
  373. return getDingtalkIssueCommentPayload(p.(*api.IssueCommentPayload))
  374. case models.HookEventPush:
  375. return getDingtalkPushPayload(p.(*api.PushPayload))
  376. case models.HookEventPullRequest:
  377. return getDingtalkPullRequestPayload(p.(*api.PullRequestPayload))
  378. case models.HookEventPullRequestApproved, models.HookEventPullRequestRejected, models.HookEventPullRequestComment:
  379. return getDingtalkPullRequestApprovalPayload(p.(*api.PullRequestPayload), event)
  380. case models.HookEventRepository:
  381. return getDingtalkRepositoryPayload(p.(*api.RepositoryPayload))
  382. case models.HookEventRelease:
  383. return getDingtalkReleasePayload(p.(*api.ReleasePayload))
  384. }
  385. return s, nil
  386. }