diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2019-11-24 13:16:59 +0800 |
---|---|---|
committer | techknowlogick <techknowlogick@gitea.io> | 2019-11-24 00:16:59 -0500 |
commit | 8ab35eefc4ff5db3f2f0a62f6f0272eae9be0585 (patch) | |
tree | 5249bcee68fb96652adc8f7dd46b604cbf15cd5e /services/mirror | |
parent | e3f22ad2cca094cba057683f35f8536e3f71a582 (diff) | |
download | gitea-8ab35eefc4ff5db3f2f0a62f6f0272eae9be0585.tar.gz gitea-8ab35eefc4ff5db3f2f0a62f6f0272eae9be0585.zip |
Move mirror sync actions to notification (#9022)
* Move mirror sync actions to notification
* fix lint
Diffstat (limited to 'services/mirror')
-rw-r--r-- | services/mirror/mirror.go | 27 | ||||
-rw-r--r-- | services/mirror/sync.go | 67 |
2 files changed, 13 insertions, 81 deletions
diff --git a/services/mirror/mirror.go b/services/mirror/mirror.go index 8c8131b5c2..d35a205d00 100644 --- a/services/mirror/mirror.go +++ b/services/mirror/mirror.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/sync" @@ -336,19 +337,17 @@ func syncMirror(repoID string) { continue } + tp, _ := git.SplitRefName(result.refName) + // Create reference if result.oldCommitID == gitShortEmptySha { - if err = SyncCreateAction(m.Repo, result.refName); err != nil { - log.Error("SyncCreateAction [repo_id: %d]: %v", m.RepoID, err) - } + notification.NotifySyncCreateRef(m.Repo.MustOwner(), m.Repo, tp, result.refName) continue } // Delete reference if result.newCommitID == gitShortEmptySha { - if err = SyncDeleteAction(m.Repo, result.refName); err != nil { - log.Error("SyncDeleteAction [repo_id: %d]: %v", m.RepoID, err) - } + notification.NotifySyncDeleteRef(m.Repo.MustOwner(), m.Repo, tp, result.refName) continue } @@ -368,15 +367,15 @@ func syncMirror(repoID string) { log.Error("CommitsBetweenIDs [repo_id: %d, new_commit_id: %s, old_commit_id: %s]: %v", m.RepoID, newCommitID, oldCommitID, err) continue } - if err = SyncPushAction(m.Repo, SyncPushActionOptions{ - RefName: result.refName, - OldCommitID: oldCommitID, - NewCommitID: newCommitID, - Commits: models.ListToPushCommits(commits), - }); err != nil { - log.Error("SyncPushAction [repo_id: %d]: %v", m.RepoID, err) - continue + + theCommits := models.ListToPushCommits(commits) + if len(theCommits.Commits) > setting.UI.FeedMaxCommitNum { + theCommits.Commits = theCommits.Commits[:setting.UI.FeedMaxCommitNum] } + + theCommits.CompareURL = m.Repo.ComposeCompareURL(oldCommitID, newCommitID) + + notification.NotifySyncPushCommits(m.Repo.MustOwner(), m.Repo, result.refName, oldCommitID, newCommitID, models.ListToPushCommits(commits)) } // Get latest commit date and update to current repository updated time diff --git a/services/mirror/sync.go b/services/mirror/sync.go deleted file mode 100644 index ba9e896dd5..0000000000 --- a/services/mirror/sync.go +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright 2019 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package mirror - -import ( - "encoding/json" - "fmt" - - "code.gitea.io/gitea/models" - "code.gitea.io/gitea/modules/notification" - "code.gitea.io/gitea/modules/setting" -) - -func syncAction(opType models.ActionType, repo *models.Repository, refName string, data []byte) error { - if err := models.NotifyWatchers(&models.Action{ - ActUserID: repo.OwnerID, - ActUser: repo.MustOwner(), - OpType: opType, - RepoID: repo.ID, - Repo: repo, - IsPrivate: repo.IsPrivate, - RefName: refName, - Content: string(data), - }); err != nil { - return fmt.Errorf("notifyWatchers: %v", err) - } - - return nil -} - -// SyncPushActionOptions mirror synchronization action options. -type SyncPushActionOptions struct { - RefName string - OldCommitID string - NewCommitID string - Commits *models.PushCommits -} - -// SyncPushAction adds new action for mirror synchronization of pushed commits. -func SyncPushAction(repo *models.Repository, opts SyncPushActionOptions) error { - if len(opts.Commits.Commits) > setting.UI.FeedMaxCommitNum { - opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum] - } - - opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID) - - notification.NotifyPushCommits(repo.MustOwner(), repo, opts.RefName, opts.OldCommitID, opts.NewCommitID, opts.Commits) - - data, err := json.Marshal(opts.Commits) - if err != nil { - return err - } - - return syncAction(models.ActionMirrorSyncPush, repo, opts.RefName, data) -} - -// SyncCreateAction adds new action for mirror synchronization of new reference. -func SyncCreateAction(repo *models.Repository, refName string) error { - return syncAction(models.ActionMirrorSyncCreate, repo, refName, nil) -} - -// SyncDeleteAction adds new action for mirror synchronization of delete reference. -func SyncDeleteAction(repo *models.Repository, refName string) error { - return syncAction(models.ActionMirrorSyncDelete, repo, refName, nil) -} |