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.3KB

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