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.

notifier.go 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package actions
  4. import (
  5. "context"
  6. "code.gitea.io/gitea/models/db"
  7. issues_model "code.gitea.io/gitea/models/issues"
  8. packages_model "code.gitea.io/gitea/models/packages"
  9. perm_model "code.gitea.io/gitea/models/perm"
  10. access_model "code.gitea.io/gitea/models/perm/access"
  11. repo_model "code.gitea.io/gitea/models/repo"
  12. user_model "code.gitea.io/gitea/models/user"
  13. "code.gitea.io/gitea/modules/git"
  14. "code.gitea.io/gitea/modules/log"
  15. "code.gitea.io/gitea/modules/notification/base"
  16. "code.gitea.io/gitea/modules/repository"
  17. "code.gitea.io/gitea/modules/setting"
  18. api "code.gitea.io/gitea/modules/structs"
  19. webhook_module "code.gitea.io/gitea/modules/webhook"
  20. "code.gitea.io/gitea/services/convert"
  21. )
  22. type actionsNotifier struct {
  23. base.NullNotifier
  24. }
  25. var _ base.Notifier = &actionsNotifier{}
  26. // NewNotifier create a new actionsNotifier notifier
  27. func NewNotifier() base.Notifier {
  28. return &actionsNotifier{}
  29. }
  30. // NotifyNewIssue notifies issue created event
  31. func (n *actionsNotifier) NotifyNewIssue(ctx context.Context, issue *issues_model.Issue, _ []*user_model.User) {
  32. ctx = withMethod(ctx, "NotifyNewIssue")
  33. if err := issue.LoadRepo(ctx); err != nil {
  34. log.Error("issue.LoadRepo: %v", err)
  35. return
  36. }
  37. if err := issue.LoadPoster(ctx); err != nil {
  38. log.Error("issue.LoadPoster: %v", err)
  39. return
  40. }
  41. permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster)
  42. newNotifyInputFromIssue(issue, webhook_module.HookEventIssues).WithPayload(&api.IssuePayload{
  43. Action: api.HookIssueOpened,
  44. Index: issue.Index,
  45. Issue: convert.ToAPIIssue(ctx, issue),
  46. Repository: convert.ToRepo(ctx, issue.Repo, permission),
  47. Sender: convert.ToUser(ctx, issue.Poster, nil),
  48. }).Notify(withMethod(ctx, "NotifyNewIssue"))
  49. }
  50. // NotifyIssueChangeStatus notifies close or reopen issue to notifiers
  51. func (n *actionsNotifier) NotifyIssueChangeStatus(ctx context.Context, doer *user_model.User, commitID string, issue *issues_model.Issue, _ *issues_model.Comment, isClosed bool) {
  52. ctx = withMethod(ctx, "NotifyIssueChangeStatus")
  53. permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster)
  54. if issue.IsPull {
  55. if err := issue.LoadPullRequest(ctx); err != nil {
  56. log.Error("LoadPullRequest: %v", err)
  57. return
  58. }
  59. // Merge pull request calls issue.changeStatus so we need to handle separately.
  60. apiPullRequest := &api.PullRequestPayload{
  61. Index: issue.Index,
  62. PullRequest: convert.ToAPIPullRequest(db.DefaultContext, issue.PullRequest, nil),
  63. Repository: convert.ToRepo(ctx, issue.Repo, permission),
  64. Sender: convert.ToUser(ctx, doer, nil),
  65. CommitID: commitID,
  66. }
  67. if isClosed {
  68. apiPullRequest.Action = api.HookIssueClosed
  69. } else {
  70. apiPullRequest.Action = api.HookIssueReOpened
  71. }
  72. newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequest).
  73. WithDoer(doer).
  74. WithPayload(apiPullRequest).
  75. WithPullRequest(issue.PullRequest).
  76. Notify(ctx)
  77. return
  78. }
  79. apiIssue := &api.IssuePayload{
  80. Index: issue.Index,
  81. Issue: convert.ToAPIIssue(ctx, issue),
  82. Repository: convert.ToRepo(ctx, issue.Repo, permission),
  83. Sender: convert.ToUser(ctx, doer, nil),
  84. }
  85. if isClosed {
  86. apiIssue.Action = api.HookIssueClosed
  87. } else {
  88. apiIssue.Action = api.HookIssueReOpened
  89. }
  90. newNotifyInputFromIssue(issue, webhook_module.HookEventIssues).
  91. WithDoer(doer).
  92. WithPayload(apiIssue).
  93. Notify(ctx)
  94. }
  95. func (n *actionsNotifier) NotifyIssueChangeLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue,
  96. _, _ []*issues_model.Label,
  97. ) {
  98. ctx = withMethod(ctx, "NotifyIssueChangeLabels")
  99. var err error
  100. if err = issue.LoadRepo(ctx); err != nil {
  101. log.Error("LoadRepo: %v", err)
  102. return
  103. }
  104. if err = issue.LoadPoster(ctx); err != nil {
  105. log.Error("LoadPoster: %v", err)
  106. return
  107. }
  108. permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster)
  109. if issue.IsPull {
  110. if err = issue.LoadPullRequest(ctx); err != nil {
  111. log.Error("loadPullRequest: %v", err)
  112. return
  113. }
  114. if err = issue.PullRequest.LoadIssue(ctx); err != nil {
  115. log.Error("LoadIssue: %v", err)
  116. return
  117. }
  118. newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequestLabel).
  119. WithDoer(doer).
  120. WithPayload(&api.PullRequestPayload{
  121. Action: api.HookIssueLabelUpdated,
  122. Index: issue.Index,
  123. PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
  124. Repository: convert.ToRepo(ctx, issue.Repo, access_model.Permission{AccessMode: perm_model.AccessModeNone}),
  125. Sender: convert.ToUser(ctx, doer, nil),
  126. }).
  127. WithPullRequest(issue.PullRequest).
  128. Notify(ctx)
  129. return
  130. }
  131. newNotifyInputFromIssue(issue, webhook_module.HookEventIssueLabel).
  132. WithDoer(doer).
  133. WithPayload(&api.IssuePayload{
  134. Action: api.HookIssueLabelUpdated,
  135. Index: issue.Index,
  136. Issue: convert.ToAPIIssue(ctx, issue),
  137. Repository: convert.ToRepo(ctx, issue.Repo, permission),
  138. Sender: convert.ToUser(ctx, doer, nil),
  139. }).
  140. Notify(ctx)
  141. }
  142. // NotifyCreateIssueComment notifies comment on an issue to notifiers
  143. func (n *actionsNotifier) NotifyCreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository,
  144. issue *issues_model.Issue, comment *issues_model.Comment, _ []*user_model.User,
  145. ) {
  146. ctx = withMethod(ctx, "NotifyCreateIssueComment")
  147. permission, _ := access_model.GetUserRepoPermission(ctx, repo, doer)
  148. if issue.IsPull {
  149. if err := issue.LoadPullRequest(ctx); err != nil {
  150. log.Error("LoadPullRequest: %v", err)
  151. return
  152. }
  153. newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequestComment).
  154. WithDoer(doer).
  155. WithPayload(&api.IssueCommentPayload{
  156. Action: api.HookIssueCommentCreated,
  157. Issue: convert.ToAPIIssue(ctx, issue),
  158. Comment: convert.ToAPIComment(ctx, repo, comment),
  159. Repository: convert.ToRepo(ctx, repo, permission),
  160. Sender: convert.ToUser(ctx, doer, nil),
  161. IsPull: true,
  162. }).
  163. WithPullRequest(issue.PullRequest).
  164. Notify(ctx)
  165. return
  166. }
  167. newNotifyInputFromIssue(issue, webhook_module.HookEventIssueComment).
  168. WithDoer(doer).
  169. WithPayload(&api.IssueCommentPayload{
  170. Action: api.HookIssueCommentCreated,
  171. Issue: convert.ToAPIIssue(ctx, issue),
  172. Comment: convert.ToAPIComment(ctx, repo, comment),
  173. Repository: convert.ToRepo(ctx, repo, permission),
  174. Sender: convert.ToUser(ctx, doer, nil),
  175. IsPull: false,
  176. }).
  177. Notify(ctx)
  178. }
  179. func (n *actionsNotifier) NotifyNewPullRequest(ctx context.Context, pull *issues_model.PullRequest, _ []*user_model.User) {
  180. ctx = withMethod(ctx, "NotifyNewPullRequest")
  181. if err := pull.LoadIssue(ctx); err != nil {
  182. log.Error("pull.LoadIssue: %v", err)
  183. return
  184. }
  185. if err := pull.Issue.LoadRepo(ctx); err != nil {
  186. log.Error("pull.Issue.LoadRepo: %v", err)
  187. return
  188. }
  189. if err := pull.Issue.LoadPoster(ctx); err != nil {
  190. log.Error("pull.Issue.LoadPoster: %v", err)
  191. return
  192. }
  193. permission, _ := access_model.GetUserRepoPermission(ctx, pull.Issue.Repo, pull.Issue.Poster)
  194. newNotifyInputFromIssue(pull.Issue, webhook_module.HookEventPullRequest).
  195. WithPayload(&api.PullRequestPayload{
  196. Action: api.HookIssueOpened,
  197. Index: pull.Issue.Index,
  198. PullRequest: convert.ToAPIPullRequest(ctx, pull, nil),
  199. Repository: convert.ToRepo(ctx, pull.Issue.Repo, permission),
  200. Sender: convert.ToUser(ctx, pull.Issue.Poster, nil),
  201. }).
  202. WithPullRequest(pull).
  203. Notify(ctx)
  204. }
  205. func (n *actionsNotifier) NotifyCreateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
  206. ctx = withMethod(ctx, "NotifyCreateRepository")
  207. newNotifyInput(repo, doer, webhook_module.HookEventRepository).WithPayload(&api.RepositoryPayload{
  208. Action: api.HookRepoCreated,
  209. Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),
  210. Organization: convert.ToUser(ctx, u, nil),
  211. Sender: convert.ToUser(ctx, doer, nil),
  212. }).Notify(ctx)
  213. }
  214. func (n *actionsNotifier) NotifyForkRepository(ctx context.Context, doer *user_model.User, oldRepo, repo *repo_model.Repository) {
  215. ctx = withMethod(ctx, "NotifyForkRepository")
  216. oldPermission, _ := access_model.GetUserRepoPermission(ctx, oldRepo, doer)
  217. permission, _ := access_model.GetUserRepoPermission(ctx, repo, doer)
  218. // forked webhook
  219. newNotifyInput(oldRepo, doer, webhook_module.HookEventFork).WithPayload(&api.ForkPayload{
  220. Forkee: convert.ToRepo(ctx, oldRepo, oldPermission),
  221. Repo: convert.ToRepo(ctx, repo, permission),
  222. Sender: convert.ToUser(ctx, doer, nil),
  223. }).Notify(ctx)
  224. u := repo.MustOwner(ctx)
  225. // Add to hook queue for created repo after session commit.
  226. if u.IsOrganization() {
  227. newNotifyInput(repo, doer, webhook_module.HookEventRepository).
  228. WithRef(oldRepo.DefaultBranch).
  229. WithPayload(&api.RepositoryPayload{
  230. Action: api.HookRepoCreated,
  231. Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),
  232. Organization: convert.ToUser(ctx, u, nil),
  233. Sender: convert.ToUser(ctx, doer, nil),
  234. }).Notify(ctx)
  235. }
  236. }
  237. func (n *actionsNotifier) NotifyPullRequestReview(ctx context.Context, pr *issues_model.PullRequest, review *issues_model.Review, _ *issues_model.Comment, _ []*user_model.User) {
  238. ctx = withMethod(ctx, "NotifyPullRequestReview")
  239. var reviewHookType webhook_module.HookEventType
  240. switch review.Type {
  241. case issues_model.ReviewTypeApprove:
  242. reviewHookType = webhook_module.HookEventPullRequestReviewApproved
  243. case issues_model.ReviewTypeComment:
  244. reviewHookType = webhook_module.HookEventPullRequestReviewComment
  245. case issues_model.ReviewTypeReject:
  246. reviewHookType = webhook_module.HookEventPullRequestReviewRejected
  247. default:
  248. // unsupported review webhook type here
  249. log.Error("Unsupported review webhook type")
  250. return
  251. }
  252. if err := pr.LoadIssue(ctx); err != nil {
  253. log.Error("pr.LoadIssue: %v", err)
  254. return
  255. }
  256. permission, err := access_model.GetUserRepoPermission(ctx, review.Issue.Repo, review.Issue.Poster)
  257. if err != nil {
  258. log.Error("models.GetUserRepoPermission: %v", err)
  259. return
  260. }
  261. newNotifyInput(review.Issue.Repo, review.Reviewer, reviewHookType).
  262. WithRef(review.CommitID).
  263. WithPayload(&api.PullRequestPayload{
  264. Action: api.HookIssueReviewed,
  265. Index: review.Issue.Index,
  266. PullRequest: convert.ToAPIPullRequest(db.DefaultContext, pr, nil),
  267. Repository: convert.ToRepo(ctx, review.Issue.Repo, permission),
  268. Sender: convert.ToUser(ctx, review.Reviewer, nil),
  269. Review: &api.ReviewPayload{
  270. Type: string(reviewHookType),
  271. Content: review.Content,
  272. },
  273. }).Notify(ctx)
  274. }
  275. func (*actionsNotifier) NotifyMergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
  276. ctx = withMethod(ctx, "NotifyMergePullRequest")
  277. // Reload pull request information.
  278. if err := pr.LoadAttributes(ctx); err != nil {
  279. log.Error("LoadAttributes: %v", err)
  280. return
  281. }
  282. if err := pr.LoadIssue(ctx); err != nil {
  283. log.Error("LoadAttributes: %v", err)
  284. return
  285. }
  286. if err := pr.Issue.LoadRepo(db.DefaultContext); err != nil {
  287. log.Error("pr.Issue.LoadRepo: %v", err)
  288. return
  289. }
  290. permission, err := access_model.GetUserRepoPermission(ctx, pr.Issue.Repo, doer)
  291. if err != nil {
  292. log.Error("models.GetUserRepoPermission: %v", err)
  293. return
  294. }
  295. // Merge pull request calls issue.changeStatus so we need to handle separately.
  296. apiPullRequest := &api.PullRequestPayload{
  297. Index: pr.Issue.Index,
  298. PullRequest: convert.ToAPIPullRequest(db.DefaultContext, pr, nil),
  299. Repository: convert.ToRepo(ctx, pr.Issue.Repo, permission),
  300. Sender: convert.ToUser(ctx, doer, nil),
  301. Action: api.HookIssueClosed,
  302. }
  303. newNotifyInput(pr.Issue.Repo, doer, webhook_module.HookEventPullRequest).
  304. WithRef(pr.MergedCommitID).
  305. WithPayload(apiPullRequest).
  306. WithPullRequest(pr).
  307. Notify(ctx)
  308. }
  309. func (n *actionsNotifier) NotifyPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
  310. ctx = withMethod(ctx, "NotifyPushCommits")
  311. apiPusher := convert.ToUser(ctx, pusher, nil)
  312. apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(ctx, repo.RepoPath(), repo.HTMLURL())
  313. if err != nil {
  314. log.Error("commits.ToAPIPayloadCommits failed: %v", err)
  315. return
  316. }
  317. newNotifyInput(repo, pusher, webhook_module.HookEventPush).
  318. WithRef(opts.RefFullName.String()).
  319. WithPayload(&api.PushPayload{
  320. Ref: opts.RefFullName.String(),
  321. Before: opts.OldCommitID,
  322. After: opts.NewCommitID,
  323. CompareURL: setting.AppURL + commits.CompareURL,
  324. Commits: apiCommits,
  325. HeadCommit: apiHeadCommit,
  326. Repo: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),
  327. Pusher: apiPusher,
  328. Sender: apiPusher,
  329. }).
  330. Notify(ctx)
  331. }
  332. func (n *actionsNotifier) NotifyCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName, refID string) {
  333. ctx = withMethod(ctx, "NotifyCreateRef")
  334. apiPusher := convert.ToUser(ctx, pusher, nil)
  335. apiRepo := convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeNone})
  336. newNotifyInput(repo, pusher, webhook_module.HookEventCreate).
  337. WithRef(refFullName.ShortName()). // FIXME: should we use a full ref name
  338. WithPayload(&api.CreatePayload{
  339. Ref: refFullName.ShortName(),
  340. Sha: refID,
  341. RefType: refFullName.RefType(),
  342. Repo: apiRepo,
  343. Sender: apiPusher,
  344. }).
  345. Notify(ctx)
  346. }
  347. func (n *actionsNotifier) NotifyDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName) {
  348. ctx = withMethod(ctx, "NotifyDeleteRef")
  349. apiPusher := convert.ToUser(ctx, pusher, nil)
  350. apiRepo := convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeNone})
  351. newNotifyInput(repo, pusher, webhook_module.HookEventDelete).
  352. WithRef(refFullName.ShortName()). // FIXME: should we use a full ref name
  353. WithPayload(&api.DeletePayload{
  354. Ref: refFullName.ShortName(),
  355. RefType: refFullName.RefType(),
  356. PusherType: api.PusherTypeUser,
  357. Repo: apiRepo,
  358. Sender: apiPusher,
  359. }).
  360. Notify(ctx)
  361. }
  362. func (n *actionsNotifier) NotifySyncPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
  363. ctx = withMethod(ctx, "NotifySyncPushCommits")
  364. apiPusher := convert.ToUser(ctx, pusher, nil)
  365. apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(db.DefaultContext, repo.RepoPath(), repo.HTMLURL())
  366. if err != nil {
  367. log.Error("commits.ToAPIPayloadCommits failed: %v", err)
  368. return
  369. }
  370. newNotifyInput(repo, pusher, webhook_module.HookEventPush).
  371. WithRef(opts.RefFullName.String()).
  372. WithPayload(&api.PushPayload{
  373. Ref: opts.RefFullName.String(),
  374. Before: opts.OldCommitID,
  375. After: opts.NewCommitID,
  376. CompareURL: setting.AppURL + commits.CompareURL,
  377. Commits: apiCommits,
  378. TotalCommits: commits.Len,
  379. HeadCommit: apiHeadCommit,
  380. Repo: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),
  381. Pusher: apiPusher,
  382. Sender: apiPusher,
  383. }).
  384. Notify(ctx)
  385. }
  386. func (n *actionsNotifier) NotifySyncCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName, refID string) {
  387. ctx = withMethod(ctx, "NotifySyncCreateRef")
  388. n.NotifyCreateRef(ctx, pusher, repo, refFullName, refID)
  389. }
  390. func (n *actionsNotifier) NotifySyncDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName) {
  391. ctx = withMethod(ctx, "NotifySyncDeleteRef")
  392. n.NotifyDeleteRef(ctx, pusher, repo, refFullName)
  393. }
  394. func (n *actionsNotifier) NotifyNewRelease(ctx context.Context, rel *repo_model.Release) {
  395. ctx = withMethod(ctx, "NotifyNewRelease")
  396. notifyRelease(ctx, rel.Publisher, rel, api.HookReleasePublished)
  397. }
  398. func (n *actionsNotifier) NotifyUpdateRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) {
  399. ctx = withMethod(ctx, "NotifyUpdateRelease")
  400. notifyRelease(ctx, doer, rel, api.HookReleaseUpdated)
  401. }
  402. func (n *actionsNotifier) NotifyDeleteRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) {
  403. ctx = withMethod(ctx, "NotifyDeleteRelease")
  404. notifyRelease(ctx, doer, rel, api.HookReleaseDeleted)
  405. }
  406. func (n *actionsNotifier) NotifyPackageCreate(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) {
  407. ctx = withMethod(ctx, "NotifyPackageCreate")
  408. notifyPackage(ctx, doer, pd, api.HookPackageCreated)
  409. }
  410. func (n *actionsNotifier) NotifyPackageDelete(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) {
  411. ctx = withMethod(ctx, "NotifyPackageDelete")
  412. notifyPackage(ctx, doer, pd, api.HookPackageDeleted)
  413. }
  414. func (n *actionsNotifier) NotifyAutoMergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
  415. ctx = withMethod(ctx, "NotifyAutoMergePullRequest")
  416. n.NotifyMergePullRequest(ctx, doer, pr)
  417. }
  418. func (n *actionsNotifier) NotifyPullRequestSynchronized(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
  419. ctx = withMethod(ctx, "NotifyPullRequestSynchronized")
  420. if err := pr.LoadIssue(ctx); err != nil {
  421. log.Error("LoadAttributes: %v", err)
  422. return
  423. }
  424. if err := pr.Issue.LoadRepo(db.DefaultContext); err != nil {
  425. log.Error("pr.Issue.LoadRepo: %v", err)
  426. return
  427. }
  428. newNotifyInput(pr.Issue.Repo, doer, webhook_module.HookEventPullRequestSync).
  429. WithPayload(&api.PullRequestPayload{
  430. Action: api.HookIssueSynchronized,
  431. Index: pr.Issue.Index,
  432. PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
  433. Repository: convert.ToRepo(ctx, pr.Issue.Repo, access_model.Permission{AccessMode: perm_model.AccessModeNone}),
  434. Sender: convert.ToUser(ctx, doer, nil),
  435. }).
  436. WithPullRequest(pr).
  437. Notify(ctx)
  438. }
  439. func (n *actionsNotifier) NotifyPullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch string) {
  440. ctx = withMethod(ctx, "NotifyPullRequestChangeTargetBranch")
  441. if err := pr.LoadIssue(ctx); err != nil {
  442. log.Error("LoadAttributes: %v", err)
  443. return
  444. }
  445. if err := pr.Issue.LoadRepo(db.DefaultContext); err != nil {
  446. log.Error("pr.Issue.LoadRepo: %v", err)
  447. return
  448. }
  449. permission, _ := access_model.GetUserRepoPermission(ctx, pr.Issue.Repo, pr.Issue.Poster)
  450. newNotifyInput(pr.Issue.Repo, doer, webhook_module.HookEventPullRequest).
  451. WithPayload(&api.PullRequestPayload{
  452. Action: api.HookIssueEdited,
  453. Index: pr.Issue.Index,
  454. Changes: &api.ChangesPayload{
  455. Ref: &api.ChangesFromPayload{
  456. From: oldBranch,
  457. },
  458. },
  459. PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
  460. Repository: convert.ToRepo(ctx, pr.Issue.Repo, permission),
  461. Sender: convert.ToUser(ctx, doer, nil),
  462. }).
  463. WithPullRequest(pr).
  464. Notify(ctx)
  465. }
  466. func (n *actionsNotifier) NotifyNewWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) {
  467. ctx = withMethod(ctx, "NotifyNewWikiPage")
  468. newNotifyInput(repo, doer, webhook_module.HookEventWiki).WithPayload(&api.WikiPayload{
  469. Action: api.HookWikiCreated,
  470. Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),
  471. Sender: convert.ToUser(ctx, doer, nil),
  472. Page: page,
  473. Comment: comment,
  474. }).Notify(ctx)
  475. }
  476. func (n *actionsNotifier) NotifyEditWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) {
  477. ctx = withMethod(ctx, "NotifyEditWikiPage")
  478. newNotifyInput(repo, doer, webhook_module.HookEventWiki).WithPayload(&api.WikiPayload{
  479. Action: api.HookWikiEdited,
  480. Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),
  481. Sender: convert.ToUser(ctx, doer, nil),
  482. Page: page,
  483. Comment: comment,
  484. }).Notify(ctx)
  485. }
  486. func (n *actionsNotifier) NotifyDeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page string) {
  487. ctx = withMethod(ctx, "NotifyDeleteWikiPage")
  488. newNotifyInput(repo, doer, webhook_module.HookEventWiki).WithPayload(&api.WikiPayload{
  489. Action: api.HookWikiDeleted,
  490. Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),
  491. Sender: convert.ToUser(ctx, doer, nil),
  492. Page: page,
  493. }).Notify(ctx)
  494. }