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

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