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.

87 lines
2.5 KiB

  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 mirror
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/setting"
  10. api "code.gitea.io/gitea/modules/structs"
  11. )
  12. func syncAction(opType models.ActionType, repo *models.Repository, refName string, data []byte) error {
  13. if err := models.NotifyWatchers(&models.Action{
  14. ActUserID: repo.OwnerID,
  15. ActUser: repo.MustOwner(),
  16. OpType: opType,
  17. RepoID: repo.ID,
  18. Repo: repo,
  19. IsPrivate: repo.IsPrivate,
  20. RefName: refName,
  21. Content: string(data),
  22. }); err != nil {
  23. return fmt.Errorf("notifyWatchers: %v", err)
  24. }
  25. defer func() {
  26. go models.HookQueue.Add(repo.ID)
  27. }()
  28. return nil
  29. }
  30. // SyncPushActionOptions mirror synchronization action options.
  31. type SyncPushActionOptions struct {
  32. RefName string
  33. OldCommitID string
  34. NewCommitID string
  35. Commits *models.PushCommits
  36. }
  37. // SyncPushAction adds new action for mirror synchronization of pushed commits.
  38. func SyncPushAction(repo *models.Repository, opts SyncPushActionOptions) error {
  39. if len(opts.Commits.Commits) > setting.UI.FeedMaxCommitNum {
  40. opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum]
  41. }
  42. apiCommits, err := opts.Commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
  43. if err != nil {
  44. return err
  45. }
  46. opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
  47. apiPusher := repo.MustOwner().APIFormat()
  48. if err := models.PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{
  49. Ref: opts.RefName,
  50. Before: opts.OldCommitID,
  51. After: opts.NewCommitID,
  52. CompareURL: setting.AppURL + opts.Commits.CompareURL,
  53. Commits: apiCommits,
  54. Repo: repo.APIFormat(models.AccessModeOwner),
  55. Pusher: apiPusher,
  56. Sender: apiPusher,
  57. }); err != nil {
  58. return fmt.Errorf("PrepareWebhooks: %v", err)
  59. }
  60. data, err := json.Marshal(opts.Commits)
  61. if err != nil {
  62. return err
  63. }
  64. return syncAction(models.ActionMirrorSyncPush, repo, opts.RefName, data)
  65. }
  66. // SyncCreateAction adds new action for mirror synchronization of new reference.
  67. func SyncCreateAction(repo *models.Repository, refName string) error {
  68. return syncAction(models.ActionMirrorSyncCreate, repo, refName, nil)
  69. }
  70. // SyncDeleteAction adds new action for mirror synchronization of delete reference.
  71. func SyncDeleteAction(repo *models.Repository, refName string) error {
  72. return syncAction(models.ActionMirrorSyncDelete, repo, refName, nil)
  73. }