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.

notification.go 9.8 kB

Change target branch for pull request (#6488) * Adds functionality to change target branch of created pull requests Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Use const instead of var in JavaScript additions Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Check if branches are equal and if PR already exists before changing target branch Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Make sure to check all commits Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Print error messages for user as error flash message Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Disallow changing target branch of closed or merged pull requests Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Resolve conflicts after merge of upstream/master Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Change order of branch select fields Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes duplicate check Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Use ctx.Tr for translations Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Recompile JS Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Use correct translation namespace Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Remove redundant if condition Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Moves most change branch logic into pull service Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Completes comment Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Add Ref to ChangesPayload for logging changed target branches instead of creating a new struct Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Revert changes to go.mod Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Directly use createComment method Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Return 404 if pull request is not found. Move written check up Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Remove variable declaration Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Return client errors on change pull request target errors Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Return error in commit.HasPreviousCommit Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adds blank line Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Test patch before persisting new target branch Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Update patch before testing (not working) Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes patch calls when changeing pull request target Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes unneeded check for base name Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Moves ChangeTargetBranch completely to pull service. Update patch status. Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Set webhook mode after errors were validated Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Update PR in one transaction Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Move logic for check if head is equal with branch to pull model Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adds missing comment and simplify return Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adjust CreateComment method call Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com>
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Copyright 2018 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 notification
  5. import (
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/log"
  8. "code.gitea.io/gitea/modules/notification/action"
  9. "code.gitea.io/gitea/modules/notification/base"
  10. "code.gitea.io/gitea/modules/notification/indexer"
  11. "code.gitea.io/gitea/modules/notification/mail"
  12. "code.gitea.io/gitea/modules/notification/ui"
  13. "code.gitea.io/gitea/modules/notification/webhook"
  14. "code.gitea.io/gitea/modules/repository"
  15. "code.gitea.io/gitea/modules/setting"
  16. )
  17. var (
  18. notifiers []base.Notifier
  19. )
  20. // RegisterNotifier providers method to receive notify messages
  21. func RegisterNotifier(notifier base.Notifier) {
  22. go notifier.Run()
  23. notifiers = append(notifiers, notifier)
  24. }
  25. // NewContext registers notification handlers
  26. func NewContext() {
  27. RegisterNotifier(ui.NewNotifier())
  28. if setting.Service.EnableNotifyMail {
  29. RegisterNotifier(mail.NewNotifier())
  30. }
  31. RegisterNotifier(indexer.NewNotifier())
  32. RegisterNotifier(webhook.NewNotifier())
  33. RegisterNotifier(action.NewNotifier())
  34. }
  35. // NotifyUploadAttachment notifies attachment upload message to notifiers
  36. func NotifyUploadAttachment(doer *models.User, repo *models.Repository, attachment *models.Attachment) {
  37. log.Info("send attachment to db.")
  38. for _, notifier := range notifiers {
  39. log.Info("send attachment to notifiers.")
  40. notifier.NotifyUploadAttachment(doer, repo, attachment)
  41. }
  42. }
  43. // NotifyCreateIssueComment notifies issue comment related message to notifiers
  44. func NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
  45. issue *models.Issue, comment *models.Comment) {
  46. for _, notifier := range notifiers {
  47. notifier.NotifyCreateIssueComment(doer, repo, issue, comment)
  48. }
  49. }
  50. // NotifyNewIssue notifies new issue to notifiers
  51. func NotifyNewIssue(issue *models.Issue) {
  52. for _, notifier := range notifiers {
  53. notifier.NotifyNewIssue(issue)
  54. }
  55. }
  56. // NotifyIssueChangeStatus notifies close or reopen issue to notifiers
  57. func NotifyIssueChangeStatus(doer *models.User, issue *models.Issue, actionComment *models.Comment, closeOrReopen bool) {
  58. for _, notifier := range notifiers {
  59. notifier.NotifyIssueChangeStatus(doer, issue, actionComment, closeOrReopen)
  60. }
  61. }
  62. // NotifyMergePullRequest notifies merge pull request to notifiers
  63. func NotifyMergePullRequest(pr *models.PullRequest, doer *models.User) {
  64. for _, notifier := range notifiers {
  65. notifier.NotifyMergePullRequest(pr, doer)
  66. }
  67. }
  68. // NotifyNewPullRequest notifies new pull request to notifiers
  69. func NotifyNewPullRequest(pr *models.PullRequest) {
  70. for _, notifier := range notifiers {
  71. notifier.NotifyNewPullRequest(pr)
  72. }
  73. }
  74. // NotifyPullRequestSynchronized notifies Synchronized pull request
  75. func NotifyPullRequestSynchronized(doer *models.User, pr *models.PullRequest) {
  76. for _, notifier := range notifiers {
  77. notifier.NotifyPullRequestSynchronized(doer, pr)
  78. }
  79. }
  80. // NotifyPullRequestReview notifies new pull request review
  81. func NotifyPullRequestReview(pr *models.PullRequest, review *models.Review, comment *models.Comment) {
  82. for _, notifier := range notifiers {
  83. notifier.NotifyPullRequestReview(pr, review, comment)
  84. }
  85. }
  86. // NotifyPullRequestChangeTargetBranch notifies when a pull request's target branch was changed
  87. func NotifyPullRequestChangeTargetBranch(doer *models.User, pr *models.PullRequest, oldBranch string) {
  88. for _, notifier := range notifiers {
  89. notifier.NotifyPullRequestChangeTargetBranch(doer, pr, oldBranch)
  90. }
  91. }
  92. // NotifyUpdateComment notifies update comment to notifiers
  93. func NotifyUpdateComment(doer *models.User, c *models.Comment, oldContent string) {
  94. for _, notifier := range notifiers {
  95. notifier.NotifyUpdateComment(doer, c, oldContent)
  96. }
  97. }
  98. // NotifyDeleteComment notifies delete comment to notifiers
  99. func NotifyDeleteComment(doer *models.User, c *models.Comment) {
  100. for _, notifier := range notifiers {
  101. notifier.NotifyDeleteComment(doer, c)
  102. }
  103. }
  104. // NotifyNewRelease notifies new release to notifiers
  105. func NotifyNewRelease(rel *models.Release) {
  106. for _, notifier := range notifiers {
  107. notifier.NotifyNewRelease(rel)
  108. }
  109. }
  110. // NotifyUpdateRelease notifies update release to notifiers
  111. func NotifyUpdateRelease(doer *models.User, rel *models.Release) {
  112. for _, notifier := range notifiers {
  113. notifier.NotifyUpdateRelease(doer, rel)
  114. }
  115. }
  116. // NotifyDeleteRelease notifies delete release to notifiers
  117. func NotifyDeleteRelease(doer *models.User, rel *models.Release) {
  118. for _, notifier := range notifiers {
  119. notifier.NotifyDeleteRelease(doer, rel)
  120. }
  121. }
  122. // NotifyIssueChangeMilestone notifies change milestone to notifiers
  123. func NotifyIssueChangeMilestone(doer *models.User, issue *models.Issue, oldMilestoneID int64) {
  124. for _, notifier := range notifiers {
  125. notifier.NotifyIssueChangeMilestone(doer, issue, oldMilestoneID)
  126. }
  127. }
  128. // NotifyIssueChangeContent notifies change content to notifiers
  129. func NotifyIssueChangeContent(doer *models.User, issue *models.Issue, oldContent string) {
  130. for _, notifier := range notifiers {
  131. notifier.NotifyIssueChangeContent(doer, issue, oldContent)
  132. }
  133. }
  134. // NotifyIssueChangeAssignee notifies change content to notifiers
  135. func NotifyIssueChangeAssignee(doer *models.User, issue *models.Issue, assignee *models.User, removed bool, comment *models.Comment) {
  136. for _, notifier := range notifiers {
  137. notifier.NotifyIssueChangeAssignee(doer, issue, assignee, removed, comment)
  138. }
  139. }
  140. // NotifyPullReviewRequest notifies Request Review change
  141. func NotifyPullReviewRequest(doer *models.User, issue *models.Issue, reviewer *models.User, isRequest bool, comment *models.Comment) {
  142. for _, notifier := range notifiers {
  143. notifier.NotifyPullReviewRequest(doer, issue, reviewer, isRequest, comment)
  144. }
  145. }
  146. // NotifyIssueClearLabels notifies clear labels to notifiers
  147. func NotifyIssueClearLabels(doer *models.User, issue *models.Issue) {
  148. for _, notifier := range notifiers {
  149. notifier.NotifyIssueClearLabels(doer, issue)
  150. }
  151. }
  152. // NotifyIssueChangeTitle notifies change title to notifiers
  153. func NotifyIssueChangeTitle(doer *models.User, issue *models.Issue, oldTitle string) {
  154. for _, notifier := range notifiers {
  155. notifier.NotifyIssueChangeTitle(doer, issue, oldTitle)
  156. }
  157. }
  158. // NotifyIssueChangeLabels notifies change labels to notifiers
  159. func NotifyIssueChangeLabels(doer *models.User, issue *models.Issue,
  160. addedLabels []*models.Label, removedLabels []*models.Label) {
  161. for _, notifier := range notifiers {
  162. notifier.NotifyIssueChangeLabels(doer, issue, addedLabels, removedLabels)
  163. }
  164. }
  165. // NotifyCreateRepository notifies create repository to notifiers
  166. func NotifyCreateRepository(doer *models.User, u *models.User, repo *models.Repository) {
  167. for _, notifier := range notifiers {
  168. notifier.NotifyCreateRepository(doer, u, repo)
  169. }
  170. }
  171. // NotifyMigrateRepository notifies create repository to notifiers
  172. func NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Repository) {
  173. for _, notifier := range notifiers {
  174. notifier.NotifyMigrateRepository(doer, u, repo)
  175. }
  176. }
  177. // NotifyTransferRepository notifies create repository to notifiers
  178. func NotifyTransferRepository(doer *models.User, repo *models.Repository, newOwnerName string) {
  179. for _, notifier := range notifiers {
  180. notifier.NotifyTransferRepository(doer, repo, newOwnerName)
  181. }
  182. }
  183. // NotifyDeleteRepository notifies delete repository to notifiers
  184. func NotifyDeleteRepository(doer *models.User, repo *models.Repository) {
  185. for _, notifier := range notifiers {
  186. notifier.NotifyDeleteRepository(doer, repo)
  187. }
  188. }
  189. // NotifyForkRepository notifies fork repository to notifiers
  190. func NotifyForkRepository(doer *models.User, oldRepo, repo *models.Repository) {
  191. for _, notifier := range notifiers {
  192. notifier.NotifyForkRepository(doer, oldRepo, repo)
  193. }
  194. }
  195. // NotifyRenameRepository notifies repository renamed
  196. func NotifyRenameRepository(doer *models.User, repo *models.Repository, oldName string) {
  197. for _, notifier := range notifiers {
  198. notifier.NotifyRenameRepository(doer, repo, oldName)
  199. }
  200. }
  201. // NotifyPushCommits notifies commits pushed to notifiers
  202. func NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits) {
  203. for _, notifier := range notifiers {
  204. notifier.NotifyPushCommits(pusher, repo, refName, oldCommitID, newCommitID, commits)
  205. }
  206. }
  207. // NotifyCreateRef notifies branch or tag creation to notifiers
  208. func NotifyCreateRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
  209. for _, notifier := range notifiers {
  210. notifier.NotifyCreateRef(pusher, repo, refType, refFullName)
  211. }
  212. }
  213. // NotifyDeleteRef notifies branch or tag deletion to notifiers
  214. func NotifyDeleteRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
  215. for _, notifier := range notifiers {
  216. notifier.NotifyDeleteRef(pusher, repo, refType, refFullName)
  217. }
  218. }
  219. // NotifySyncPushCommits notifies commits pushed to notifiers
  220. func NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits) {
  221. for _, notifier := range notifiers {
  222. notifier.NotifySyncPushCommits(pusher, repo, refName, oldCommitID, newCommitID, commits)
  223. }
  224. }
  225. // NotifySyncCreateRef notifies branch or tag creation to notifiers
  226. func NotifySyncCreateRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
  227. for _, notifier := range notifiers {
  228. notifier.NotifySyncCreateRef(pusher, repo, refType, refFullName)
  229. }
  230. }
  231. // NotifySyncDeleteRef notifies branch or tag deletion to notifiers
  232. func NotifySyncDeleteRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
  233. for _, notifier := range notifiers {
  234. notifier.NotifySyncDeleteRef(pusher, repo, refType, refFullName)
  235. }
  236. }