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 10 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>
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/notification/action"
  8. "code.gitea.io/gitea/modules/notification/base"
  9. "code.gitea.io/gitea/modules/notification/indexer"
  10. "code.gitea.io/gitea/modules/notification/mail"
  11. "code.gitea.io/gitea/modules/notification/ui"
  12. "code.gitea.io/gitea/modules/notification/webhook"
  13. wechatNotifier "code.gitea.io/gitea/modules/notification/wechat"
  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. RegisterNotifier(wechatNotifier.NewNotifier())
  35. }
  36. // NotifyUploadAttachment notifies attachment upload message to notifiers
  37. func NotifyOtherTask(doer *models.User, repo *models.Repository, id string, name string, optype models.ActionType) {
  38. for _, notifier := range notifiers {
  39. notifier.NotifyOtherTask(doer, repo, id, name, optype)
  40. }
  41. }
  42. // NotifyCreateIssueComment notifies issue comment related message to notifiers
  43. func NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
  44. issue *models.Issue, comment *models.Comment) {
  45. for _, notifier := range notifiers {
  46. notifier.NotifyCreateIssueComment(doer, repo, issue, comment)
  47. }
  48. }
  49. // NotifyNewIssue notifies new issue to notifiers
  50. func NotifyNewIssue(issue *models.Issue) {
  51. for _, notifier := range notifiers {
  52. notifier.NotifyNewIssue(issue)
  53. }
  54. }
  55. // NotifyIssueChangeStatus notifies close or reopen issue to notifiers
  56. func NotifyIssueChangeStatus(doer *models.User, issue *models.Issue, actionComment *models.Comment, closeOrReopen bool) {
  57. for _, notifier := range notifiers {
  58. notifier.NotifyIssueChangeStatus(doer, issue, actionComment, closeOrReopen)
  59. }
  60. }
  61. // NotifyMergePullRequest notifies merge pull request to notifiers
  62. func NotifyMergePullRequest(pr *models.PullRequest, doer *models.User) {
  63. for _, notifier := range notifiers {
  64. notifier.NotifyMergePullRequest(pr, doer)
  65. }
  66. }
  67. // NotifyNewPullRequest notifies new pull request to notifiers
  68. func NotifyNewPullRequest(pr *models.PullRequest) {
  69. for _, notifier := range notifiers {
  70. notifier.NotifyNewPullRequest(pr)
  71. }
  72. }
  73. // NotifyPullRequestSynchronized notifies Synchronized pull request
  74. func NotifyPullRequestSynchronized(doer *models.User, pr *models.PullRequest) {
  75. for _, notifier := range notifiers {
  76. notifier.NotifyPullRequestSynchronized(doer, pr)
  77. }
  78. }
  79. // NotifyPullRequestReview notifies new pull request review
  80. func NotifyPullRequestReview(pr *models.PullRequest, review *models.Review, comment *models.Comment) {
  81. for _, notifier := range notifiers {
  82. notifier.NotifyPullRequestReview(pr, review, comment)
  83. }
  84. }
  85. // NotifyPullRequestChangeTargetBranch notifies when a pull request's target branch was changed
  86. func NotifyPullRequestChangeTargetBranch(doer *models.User, pr *models.PullRequest, oldBranch string) {
  87. for _, notifier := range notifiers {
  88. notifier.NotifyPullRequestChangeTargetBranch(doer, pr, oldBranch)
  89. }
  90. }
  91. // NotifyUpdateComment notifies update comment to notifiers
  92. func NotifyUpdateComment(doer *models.User, c *models.Comment, oldContent string) {
  93. for _, notifier := range notifiers {
  94. notifier.NotifyUpdateComment(doer, c, oldContent)
  95. }
  96. }
  97. // NotifyDeleteComment notifies delete comment to notifiers
  98. func NotifyDeleteComment(doer *models.User, c *models.Comment) {
  99. for _, notifier := range notifiers {
  100. notifier.NotifyDeleteComment(doer, c)
  101. }
  102. }
  103. // NotifyNewRelease notifies new release to notifiers
  104. func NotifyNewRelease(rel *models.Release) {
  105. for _, notifier := range notifiers {
  106. notifier.NotifyNewRelease(rel)
  107. }
  108. }
  109. // NotifyUpdateRelease notifies update release to notifiers
  110. func NotifyUpdateRelease(doer *models.User, rel *models.Release) {
  111. for _, notifier := range notifiers {
  112. notifier.NotifyUpdateRelease(doer, rel)
  113. }
  114. }
  115. // NotifyDeleteRelease notifies delete release to notifiers
  116. func NotifyDeleteRelease(doer *models.User, rel *models.Release) {
  117. for _, notifier := range notifiers {
  118. notifier.NotifyDeleteRelease(doer, rel)
  119. }
  120. }
  121. // NotifyIssueChangeMilestone notifies change milestone to notifiers
  122. func NotifyIssueChangeMilestone(doer *models.User, issue *models.Issue, oldMilestoneID int64) {
  123. for _, notifier := range notifiers {
  124. notifier.NotifyIssueChangeMilestone(doer, issue, oldMilestoneID)
  125. }
  126. }
  127. // NotifyIssueChangeContent notifies change content to notifiers
  128. func NotifyIssueChangeContent(doer *models.User, issue *models.Issue, oldContent string) {
  129. for _, notifier := range notifiers {
  130. notifier.NotifyIssueChangeContent(doer, issue, oldContent)
  131. }
  132. }
  133. // NotifyIssueChangeAssignee notifies change content to notifiers
  134. func NotifyIssueChangeAssignee(doer *models.User, issue *models.Issue, assignee *models.User, removed bool, comment *models.Comment) {
  135. for _, notifier := range notifiers {
  136. notifier.NotifyIssueChangeAssignee(doer, issue, assignee, removed, comment)
  137. }
  138. }
  139. // NotifyPullReviewRequest notifies Request Review change
  140. func NotifyPullReviewRequest(doer *models.User, issue *models.Issue, reviewer *models.User, isRequest bool, comment *models.Comment) {
  141. for _, notifier := range notifiers {
  142. notifier.NotifyPullReviewRequest(doer, issue, reviewer, isRequest, comment)
  143. }
  144. }
  145. // NotifyIssueClearLabels notifies clear labels to notifiers
  146. func NotifyIssueClearLabels(doer *models.User, issue *models.Issue) {
  147. for _, notifier := range notifiers {
  148. notifier.NotifyIssueClearLabels(doer, issue)
  149. }
  150. }
  151. // NotifyIssueChangeTitle notifies change title to notifiers
  152. func NotifyIssueChangeTitle(doer *models.User, issue *models.Issue, oldTitle string) {
  153. for _, notifier := range notifiers {
  154. notifier.NotifyIssueChangeTitle(doer, issue, oldTitle)
  155. }
  156. }
  157. // NotifyIssueChangeLabels notifies change labels to notifiers
  158. func NotifyIssueChangeLabels(doer *models.User, issue *models.Issue,
  159. addedLabels []*models.Label, removedLabels []*models.Label) {
  160. for _, notifier := range notifiers {
  161. notifier.NotifyIssueChangeLabels(doer, issue, addedLabels, removedLabels)
  162. }
  163. }
  164. // NotifyCreateRepository notifies create repository to notifiers
  165. func NotifyCreateRepository(doer *models.User, u *models.User, repo *models.Repository) {
  166. for _, notifier := range notifiers {
  167. notifier.NotifyCreateRepository(doer, u, repo)
  168. }
  169. }
  170. // NotifyMigrateRepository notifies create repository to notifiers
  171. func NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Repository) {
  172. for _, notifier := range notifiers {
  173. notifier.NotifyMigrateRepository(doer, u, repo)
  174. }
  175. }
  176. // NotifyTransferRepository notifies create repository to notifiers
  177. func NotifyTransferRepository(doer *models.User, repo *models.Repository, newOwnerName string) {
  178. for _, notifier := range notifiers {
  179. notifier.NotifyTransferRepository(doer, repo, newOwnerName)
  180. }
  181. }
  182. // NotifyDeleteRepository notifies delete repository to notifiers
  183. func NotifyDeleteRepository(doer *models.User, repo *models.Repository) {
  184. for _, notifier := range notifiers {
  185. notifier.NotifyDeleteRepository(doer, repo)
  186. }
  187. }
  188. // NotifyForkRepository notifies fork repository to notifiers
  189. func NotifyForkRepository(doer *models.User, oldRepo, repo *models.Repository) {
  190. for _, notifier := range notifiers {
  191. notifier.NotifyForkRepository(doer, oldRepo, repo)
  192. }
  193. }
  194. // NotifyRenameRepository notifies repository renamed
  195. func NotifyRenameRepository(doer *models.User, repo *models.Repository, oldName string) {
  196. for _, notifier := range notifiers {
  197. notifier.NotifyRenameRepository(doer, repo, oldName)
  198. }
  199. }
  200. // NotifyPushCommits notifies commits pushed to notifiers
  201. func NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits) {
  202. for _, notifier := range notifiers {
  203. notifier.NotifyPushCommits(pusher, repo, refName, oldCommitID, newCommitID, commits)
  204. }
  205. }
  206. // NotifyCreateRef notifies branch or tag creation to notifiers
  207. func NotifyCreateRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
  208. for _, notifier := range notifiers {
  209. notifier.NotifyCreateRef(pusher, repo, refType, refFullName)
  210. }
  211. }
  212. // NotifyDeleteRef notifies branch or tag deletion to notifiers
  213. func NotifyDeleteRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
  214. for _, notifier := range notifiers {
  215. notifier.NotifyDeleteRef(pusher, repo, refType, refFullName)
  216. }
  217. }
  218. // NotifySyncPushCommits notifies commits pushed to notifiers
  219. func NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits) {
  220. for _, notifier := range notifiers {
  221. notifier.NotifySyncPushCommits(pusher, repo, refName, oldCommitID, newCommitID, commits)
  222. }
  223. }
  224. // NotifySyncCreateRef notifies branch or tag creation to notifiers
  225. func NotifySyncCreateRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
  226. for _, notifier := range notifiers {
  227. notifier.NotifySyncCreateRef(pusher, repo, refType, refFullName)
  228. }
  229. }
  230. // NotifySyncDeleteRef notifies branch or tag deletion to notifiers
  231. func NotifySyncDeleteRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
  232. for _, notifier := range notifiers {
  233. notifier.NotifySyncDeleteRef(pusher, repo, refType, refFullName)
  234. }
  235. }
  236. // NotifyChangeCloudbrainStatus
  237. func NotifyChangeCloudbrainStatus(cloudbrain *models.Cloudbrain, oldStatus string) {
  238. for _, notifier := range notifiers {
  239. notifier.NotifyChangeCloudbrainStatus(cloudbrain, oldStatus)
  240. }
  241. }