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

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