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.

67 lines
2.0 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/notification"
  10. "code.gitea.io/gitea/modules/setting"
  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. return nil
  26. }
  27. // SyncPushActionOptions mirror synchronization action options.
  28. type SyncPushActionOptions struct {
  29. RefName string
  30. OldCommitID string
  31. NewCommitID string
  32. Commits *models.PushCommits
  33. }
  34. // SyncPushAction adds new action for mirror synchronization of pushed commits.
  35. func SyncPushAction(repo *models.Repository, opts SyncPushActionOptions) error {
  36. if len(opts.Commits.Commits) > setting.UI.FeedMaxCommitNum {
  37. opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum]
  38. }
  39. opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
  40. notification.NotifyPushCommits(repo.MustOwner(), repo, opts.RefName, opts.OldCommitID, opts.NewCommitID, opts.Commits)
  41. data, err := json.Marshal(opts.Commits)
  42. if err != nil {
  43. return err
  44. }
  45. return syncAction(models.ActionMirrorSyncPush, repo, opts.RefName, data)
  46. }
  47. // SyncCreateAction adds new action for mirror synchronization of new reference.
  48. func SyncCreateAction(repo *models.Repository, refName string) error {
  49. return syncAction(models.ActionMirrorSyncCreate, repo, refName, nil)
  50. }
  51. // SyncDeleteAction adds new action for mirror synchronization of delete reference.
  52. func SyncDeleteAction(repo *models.Repository, refName string) error {
  53. return syncAction(models.ActionMirrorSyncDelete, repo, refName, nil)
  54. }