Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

general.go 7.2KB

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