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.

action.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 action
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/notification/base"
  10. )
  11. type actionNotifier struct {
  12. base.NullNotifier
  13. }
  14. var (
  15. _ base.Notifier = &actionNotifier{}
  16. )
  17. // NewNotifier create a new actionNotifier notifier
  18. func NewNotifier() base.Notifier {
  19. return &actionNotifier{}
  20. }
  21. func (a *actionNotifier) NotifyNewIssue(issue *models.Issue) {
  22. if err := issue.LoadPoster(); err != nil {
  23. log.Error("issue.LoadPoster: %v", err)
  24. return
  25. }
  26. if err := issue.LoadRepo(); err != nil {
  27. log.Error("issue.LoadRepo: %v", err)
  28. return
  29. }
  30. repo := issue.Repo
  31. if err := models.NotifyWatchers(&models.Action{
  32. ActUserID: issue.Poster.ID,
  33. ActUser: issue.Poster,
  34. OpType: models.ActionCreateIssue,
  35. Content: fmt.Sprintf("%d|%s", issue.Index, issue.Title),
  36. RepoID: repo.ID,
  37. Repo: repo,
  38. IsPrivate: repo.IsPrivate,
  39. }); err != nil {
  40. log.Error("NotifyWatchers: %v", err)
  41. }
  42. }
  43. func (a *actionNotifier) NotifyNewPullRequest(pull *models.PullRequest) {
  44. if err := pull.LoadIssue(); err != nil {
  45. log.Error("pull.LoadIssue: %v", err)
  46. return
  47. }
  48. if err := pull.Issue.LoadRepo(); err != nil {
  49. log.Error("pull.Issue.LoadRepo: %v", err)
  50. return
  51. }
  52. if err := pull.Issue.LoadPoster(); err != nil {
  53. log.Error("pull.Issue.LoadPoster: %v", err)
  54. return
  55. }
  56. if err := models.NotifyWatchers(&models.Action{
  57. ActUserID: pull.Issue.Poster.ID,
  58. ActUser: pull.Issue.Poster,
  59. OpType: models.ActionCreatePullRequest,
  60. Content: fmt.Sprintf("%d|%s", pull.Issue.Index, pull.Issue.Title),
  61. RepoID: pull.Issue.Repo.ID,
  62. Repo: pull.Issue.Repo,
  63. IsPrivate: pull.Issue.Repo.IsPrivate,
  64. }); err != nil {
  65. log.Error("NotifyWatchers: %v", err)
  66. }
  67. }
  68. func (a *actionNotifier) NotifyRenameRepository(doer *models.User, repo *models.Repository, oldName string) {
  69. if err := models.NotifyWatchers(&models.Action{
  70. ActUserID: doer.ID,
  71. ActUser: doer,
  72. OpType: models.ActionRenameRepo,
  73. RepoID: repo.ID,
  74. Repo: repo,
  75. IsPrivate: repo.IsPrivate,
  76. Content: oldName,
  77. }); err != nil {
  78. log.Error("notify watchers: %v", err)
  79. } else {
  80. log.Trace("action.renameRepoAction: %s/%s", doer.Name, repo.Name)
  81. }
  82. }
  83. func (a *actionNotifier) NotifyCreateRepository(doer *models.User, u *models.User, repo *models.Repository) {
  84. if err := models.NotifyWatchers(&models.Action{
  85. ActUserID: doer.ID,
  86. ActUser: doer,
  87. OpType: models.ActionCreateRepo,
  88. RepoID: repo.ID,
  89. Repo: repo,
  90. IsPrivate: repo.IsPrivate,
  91. }); err != nil {
  92. log.Error("notify watchers '%d/%d': %v", doer.ID, repo.ID, err)
  93. }
  94. }
  95. func (a *actionNotifier) NotifyForkRepository(doer *models.User, oldRepo, repo *models.Repository) {
  96. if err := models.NotifyWatchers(&models.Action{
  97. ActUserID: doer.ID,
  98. ActUser: doer,
  99. OpType: models.ActionCreateRepo,
  100. RepoID: repo.ID,
  101. Repo: repo,
  102. IsPrivate: repo.IsPrivate,
  103. }); err != nil {
  104. log.Error("notify watchers '%d/%d': %v", doer.ID, repo.ID, err)
  105. }
  106. }