From 8edf3d58048fce20cdde776c3deb2ca1d94ad8e7 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 16 Oct 2019 17:56:59 +0800 Subject: [PATCH] Move sync mirror actions to mirror service package (#8518) * move sync mirror actions to mirror service * fix go.mod * fix lint * fix lint --- models/action.go | 74 --------------------------------- services/mirror/mirror.go | 12 +++--- services/mirror/sync.go | 87 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 80 deletions(-) create mode 100644 services/mirror/sync.go diff --git a/models/action.go b/models/action.go index 2d2999f880..b651c658d5 100644 --- a/models/action.go +++ b/models/action.go @@ -6,7 +6,6 @@ package models import ( - "encoding/json" "fmt" "html" "path" @@ -610,79 +609,6 @@ func MergePullRequestAction(actUser *User, repo *Repository, pull *Issue) error return mergePullRequestAction(x, actUser, repo, pull) } -func mirrorSyncAction(e Engine, opType ActionType, repo *Repository, refName string, data []byte) error { - if err := notifyWatchers(e, &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) - } - - defer func() { - go HookQueue.Add(repo.ID) - }() - - return nil -} - -// MirrorSyncPushActionOptions mirror synchronization action options. -type MirrorSyncPushActionOptions struct { - RefName string - OldCommitID string - NewCommitID string - Commits *PushCommits -} - -// MirrorSyncPushAction adds new action for mirror synchronization of pushed commits. -func MirrorSyncPushAction(repo *Repository, opts MirrorSyncPushActionOptions) error { - if len(opts.Commits.Commits) > setting.UI.FeedMaxCommitNum { - opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum] - } - - apiCommits, err := opts.Commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL()) - if err != nil { - return err - } - - opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID) - apiPusher := repo.MustOwner().APIFormat() - if err := PrepareWebhooks(repo, HookEventPush, &api.PushPayload{ - Ref: opts.RefName, - Before: opts.OldCommitID, - After: opts.NewCommitID, - CompareURL: setting.AppURL + opts.Commits.CompareURL, - Commits: apiCommits, - Repo: repo.APIFormat(AccessModeOwner), - Pusher: apiPusher, - Sender: apiPusher, - }); err != nil { - return fmt.Errorf("PrepareWebhooks: %v", err) - } - - data, err := json.Marshal(opts.Commits) - if err != nil { - return err - } - - return mirrorSyncAction(x, ActionMirrorSyncPush, repo, opts.RefName, data) -} - -// MirrorSyncCreateAction adds new action for mirror synchronization of new reference. -func MirrorSyncCreateAction(repo *Repository, refName string) error { - return mirrorSyncAction(x, ActionMirrorSyncCreate, repo, refName, nil) -} - -// MirrorSyncDeleteAction adds new action for mirror synchronization of delete reference. -func MirrorSyncDeleteAction(repo *Repository, refName string) error { - return mirrorSyncAction(x, ActionMirrorSyncDelete, repo, refName, nil) -} - // GetFeedsOptions options for retrieving feeds type GetFeedsOptions struct { RequestedUser *User diff --git a/services/mirror/mirror.go b/services/mirror/mirror.go index 7bfc5fd4da..11430c2070 100644 --- a/services/mirror/mirror.go +++ b/services/mirror/mirror.go @@ -329,16 +329,16 @@ func SyncMirrors() { // Create reference if result.oldCommitID == gitShortEmptySha { - if err = models.MirrorSyncCreateAction(m.Repo, result.refName); err != nil { - log.Error("MirrorSyncCreateAction [repo_id: %d]: %v", m.RepoID, err) + if err = SyncCreateAction(m.Repo, result.refName); err != nil { + log.Error("SyncCreateAction [repo_id: %d]: %v", m.RepoID, err) } continue } // Delete reference if result.newCommitID == gitShortEmptySha { - if err = models.MirrorSyncDeleteAction(m.Repo, result.refName); err != nil { - log.Error("MirrorSyncDeleteAction [repo_id: %d]: %v", m.RepoID, err) + if err = SyncDeleteAction(m.Repo, result.refName); err != nil { + log.Error("SyncDeleteAction [repo_id: %d]: %v", m.RepoID, err) } continue } @@ -359,13 +359,13 @@ func SyncMirrors() { log.Error("CommitsBetweenIDs [repo_id: %d, new_commit_id: %s, old_commit_id: %s]: %v", m.RepoID, newCommitID, oldCommitID, err) continue } - if err = models.MirrorSyncPushAction(m.Repo, models.MirrorSyncPushActionOptions{ + if err = SyncPushAction(m.Repo, SyncPushActionOptions{ RefName: result.refName, OldCommitID: oldCommitID, NewCommitID: newCommitID, Commits: models.ListToPushCommits(commits), }); err != nil { - log.Error("MirrorSyncPushAction [repo_id: %d]: %v", m.RepoID, err) + log.Error("SyncPushAction [repo_id: %d]: %v", m.RepoID, err) continue } } diff --git a/services/mirror/sync.go b/services/mirror/sync.go new file mode 100644 index 0000000000..4bc153b47f --- /dev/null +++ b/services/mirror/sync.go @@ -0,0 +1,87 @@ +// 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/setting" + api "code.gitea.io/gitea/modules/structs" +) + +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) + } + + defer func() { + go models.HookQueue.Add(repo.ID) + }() + + 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] + } + + apiCommits, err := opts.Commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL()) + if err != nil { + return err + } + + opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID) + apiPusher := repo.MustOwner().APIFormat() + if err := models.PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{ + Ref: opts.RefName, + Before: opts.OldCommitID, + After: opts.NewCommitID, + CompareURL: setting.AppURL + opts.Commits.CompareURL, + Commits: apiCommits, + Repo: repo.APIFormat(models.AccessModeOwner), + Pusher: apiPusher, + Sender: apiPusher, + }); err != nil { + return fmt.Errorf("PrepareWebhooks: %v", err) + } + + 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) +}