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.

general.go 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2019 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. "fmt"
  7. "html"
  8. "strings"
  9. "code.gitea.io/gitea/modules/setting"
  10. api "code.gitea.io/gitea/modules/structs"
  11. )
  12. type linkFormatter = func(string, string) string
  13. // noneLinkFormatter does not create a link but just returns the text
  14. func noneLinkFormatter(url string, text string) string {
  15. return text
  16. }
  17. // htmlLinkFormatter creates a HTML link
  18. func htmlLinkFormatter(url string, text string) string {
  19. return fmt.Sprintf(`<a href="%s">%s</a>`, url, html.EscapeString(text))
  20. }
  21. func getIssuesPayloadInfo(p *api.IssuePayload, linkFormatter linkFormatter) (string, string, string, int) {
  22. senderLink := linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName)
  23. repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
  24. issueTitle := fmt.Sprintf("#%d %s", p.Index, p.Issue.Title)
  25. titleLink := linkFormatter(fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Index), issueTitle)
  26. var text string
  27. color := yellowColor
  28. switch p.Action {
  29. case api.HookIssueOpened:
  30. text = fmt.Sprintf("[%s] Issue opened: %s by %s", repoLink, titleLink, senderLink)
  31. color = orangeColor
  32. case api.HookIssueClosed:
  33. text = fmt.Sprintf("[%s] Issue closed: %s by %s", repoLink, titleLink, senderLink)
  34. color = redColor
  35. case api.HookIssueReOpened:
  36. text = fmt.Sprintf("[%s] Issue re-opened: %s by %s", repoLink, titleLink, senderLink)
  37. case api.HookIssueEdited:
  38. text = fmt.Sprintf("[%s] Issue edited: %s by %s", repoLink, titleLink, senderLink)
  39. case api.HookIssueAssigned:
  40. text = fmt.Sprintf("[%s] Issue assigned to %s: %s by %s", repoLink,
  41. linkFormatter(setting.AppURL+p.Issue.Assignee.UserName, p.Issue.Assignee.UserName),
  42. titleLink, senderLink)
  43. color = greenColor
  44. case api.HookIssueUnassigned:
  45. text = fmt.Sprintf("[%s] Issue unassigned: %s by %s", repoLink, titleLink, senderLink)
  46. case api.HookIssueLabelUpdated:
  47. text = fmt.Sprintf("[%s] Issue labels updated: %s by %s", repoLink, titleLink, senderLink)
  48. case api.HookIssueLabelCleared:
  49. text = fmt.Sprintf("[%s] Issue labels cleared: %s by %s", repoLink, titleLink, senderLink)
  50. case api.HookIssueSynchronized:
  51. text = fmt.Sprintf("[%s] Issue synchronized: %s by %s", repoLink, titleLink, senderLink)
  52. case api.HookIssueMilestoned:
  53. mileStoneLink := fmt.Sprintf("%s/milestone/%d", p.Repository.HTMLURL, p.Issue.Milestone.ID)
  54. text = fmt.Sprintf("[%s] Issue milestoned to %s: %s by %s", repoLink,
  55. linkFormatter(mileStoneLink, p.Issue.Milestone.Title), titleLink, senderLink)
  56. case api.HookIssueDemilestoned:
  57. text = fmt.Sprintf("[%s] Issue milestone cleared: %s by %s", repoLink, titleLink, senderLink)
  58. }
  59. var attachmentText string
  60. if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited {
  61. attachmentText = p.Issue.Body
  62. }
  63. return text, issueTitle, attachmentText, color
  64. }
  65. func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkFormatter) (string, string, string, int) {
  66. senderLink := linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName)
  67. repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
  68. issueTitle := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title)
  69. titleLink := linkFormatter(p.PullRequest.URL, issueTitle)
  70. var text string
  71. color := yellowColor
  72. switch p.Action {
  73. case api.HookIssueOpened:
  74. text = fmt.Sprintf("[%s] Pull request %s opened by %s", repoLink, titleLink, senderLink)
  75. color = greenColor
  76. case api.HookIssueClosed:
  77. if p.PullRequest.HasMerged {
  78. text = fmt.Sprintf("[%s] Pull request %s merged by %s", repoLink, titleLink, senderLink)
  79. color = purpleColor
  80. } else {
  81. text = fmt.Sprintf("[%s] Pull request %s closed by %s", repoLink, titleLink, senderLink)
  82. color = redColor
  83. }
  84. case api.HookIssueReOpened:
  85. text = fmt.Sprintf("[%s] Pull request %s re-opened by %s", repoLink, titleLink, senderLink)
  86. case api.HookIssueEdited:
  87. text = fmt.Sprintf("[%s] Pull request %s edited by %s", repoLink, titleLink, senderLink)
  88. case api.HookIssueAssigned:
  89. list := make([]string, len(p.PullRequest.Assignees))
  90. for i, user := range p.PullRequest.Assignees {
  91. list[i] = linkFormatter(setting.AppURL+user.UserName, user.UserName)
  92. }
  93. text = fmt.Sprintf("[%s] Pull request %s assigned to %s by %s", repoLink,
  94. strings.Join(list, ", "),
  95. titleLink, senderLink)
  96. color = greenColor
  97. case api.HookIssueUnassigned:
  98. text = fmt.Sprintf("[%s] Pull request %s unassigned by %s", repoLink, titleLink, senderLink)
  99. case api.HookIssueLabelUpdated:
  100. text = fmt.Sprintf("[%s] Pull request %s labels updated by %s", repoLink, titleLink, senderLink)
  101. case api.HookIssueLabelCleared:
  102. text = fmt.Sprintf("[%s] Pull request %s labels cleared by %s", repoLink, titleLink, senderLink)
  103. case api.HookIssueSynchronized:
  104. text = fmt.Sprintf("[%s] Pull request %s synchronized by %s", repoLink, titleLink, senderLink)
  105. case api.HookIssueMilestoned:
  106. mileStoneLink := fmt.Sprintf("%s/milestone/%d", p.Repository.HTMLURL, p.PullRequest.Milestone.ID)
  107. text = fmt.Sprintf("[%s] Pull request %s milestoned to %s by %s", repoLink,
  108. linkFormatter(mileStoneLink, p.PullRequest.Milestone.Title), titleLink, senderLink)
  109. case api.HookIssueDemilestoned:
  110. text = fmt.Sprintf("[%s] Pull request %s milestone cleared by %s", repoLink, titleLink, senderLink)
  111. }
  112. var attachmentText string
  113. if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited {
  114. attachmentText = p.PullRequest.Body
  115. }
  116. return text, issueTitle, attachmentText, color
  117. }
  118. func getReleasePayloadInfo(p *api.ReleasePayload, linkFormatter linkFormatter) (text string, color int) {
  119. senderLink := linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName)
  120. repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
  121. refLink := linkFormatter(p.Repository.HTMLURL+"/src/"+p.Release.TagName, p.Release.TagName)
  122. switch p.Action {
  123. case api.HookReleasePublished:
  124. text = fmt.Sprintf("[%s] Release %s created by %s", repoLink, refLink, senderLink)
  125. color = greenColor
  126. case api.HookReleaseUpdated:
  127. text = fmt.Sprintf("[%s] Release %s updated by %s", repoLink, refLink, senderLink)
  128. color = yellowColor
  129. case api.HookReleaseDeleted:
  130. text = fmt.Sprintf("[%s] Release %s deleted by %s", repoLink, refLink, senderLink)
  131. color = redColor
  132. }
  133. return text, color
  134. }
  135. func getIssueCommentPayloadInfo(p *api.IssueCommentPayload, linkFormatter linkFormatter) (string, string, int) {
  136. senderLink := linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName)
  137. repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
  138. issueTitle := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
  139. var text, typ, titleLink string
  140. color := yellowColor
  141. if p.IsPull {
  142. typ = "pull request"
  143. titleLink = linkFormatter(p.Comment.PRURL, issueTitle)
  144. } else {
  145. typ = "issue"
  146. titleLink = linkFormatter(p.Comment.IssueURL, issueTitle)
  147. }
  148. switch p.Action {
  149. case api.HookIssueCommentCreated:
  150. text = fmt.Sprintf("[%s] New comment on %s %s by %s", repoLink, typ, titleLink, senderLink)
  151. if p.IsPull {
  152. color = greenColorLight
  153. } else {
  154. color = orangeColorLight
  155. }
  156. case api.HookIssueCommentEdited:
  157. text = fmt.Sprintf("[%s] Comment on %s %s edited by %s", repoLink, typ, titleLink, senderLink)
  158. case api.HookIssueCommentDeleted:
  159. text = fmt.Sprintf("[%s] Comment on %s %s deleted by %s", repoLink, typ, titleLink, senderLink)
  160. color = redColor
  161. }
  162. return text, issueTitle, color
  163. }