aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChongyi Zheng <git@zcy.dev>2023-08-26 22:24:45 -0400
committerGitHub <noreply@github.com>2023-08-27 10:24:45 +0800
commit43652746f2929aeb53ec1e575c3691050755a0b5 (patch)
tree8f2f05ca05fc6ad1b5c557c77a95f17c0f2381bc
parent37b3ba22a1dcf88cf4bfa4a7861703c8a5701f5c (diff)
downloadgitea-43652746f2929aeb53ec1e575c3691050755a0b5.tar.gz
gitea-43652746f2929aeb53ec1e575c3691050755a0b5.zip
Move `modules/mirror` to `services` (#26737)
To solve the cyclic imports in a better way Closes #20261
-rw-r--r--models/repo/pushmirror.go2
-rw-r--r--modules/notification/notification.go2
-rw-r--r--routers/api/v1/repo/mirror.go3
-rw-r--r--routers/web/repo/setting/setting.go7
-rw-r--r--services/mirror/mirror.go21
-rw-r--r--services/mirror/mirror_push.go12
-rw-r--r--services/mirror/notifier.go (renamed from modules/notification/mirror/mirror.go)24
-rw-r--r--services/mirror/queue.go (renamed from modules/mirror/mirror.go)0
8 files changed, 32 insertions, 39 deletions
diff --git a/models/repo/pushmirror.go b/models/repo/pushmirror.go
index dad9a9d388..3edbcceb9b 100644
--- a/models/repo/pushmirror.go
+++ b/models/repo/pushmirror.go
@@ -128,7 +128,7 @@ func GetPushMirrorsByRepoID(ctx context.Context, repoID int64, listOptions db.Li
func GetPushMirrorsSyncedOnCommit(ctx context.Context, repoID int64) ([]*PushMirror, error) {
mirrors := make([]*PushMirror, 0, 10)
return mirrors, db.GetEngine(ctx).
- Where("repo_id=? AND sync_on_commit=?", repoID, true).
+ Where("repo_id = ? AND sync_on_commit = ?", repoID, true).
Find(&mirrors)
}
diff --git a/modules/notification/notification.go b/modules/notification/notification.go
index 99e1a06ebd..50b2694d9c 100644
--- a/modules/notification/notification.go
+++ b/modules/notification/notification.go
@@ -16,7 +16,6 @@ import (
"code.gitea.io/gitea/modules/notification/base"
"code.gitea.io/gitea/modules/notification/indexer"
"code.gitea.io/gitea/modules/notification/mail"
- "code.gitea.io/gitea/modules/notification/mirror"
"code.gitea.io/gitea/modules/notification/ui"
"code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
@@ -38,7 +37,6 @@ func NewContext() {
}
RegisterNotifier(indexer.NewNotifier())
RegisterNotifier(action.NewNotifier())
- RegisterNotifier(mirror.NewNotifier())
}
// NotifyNewWikiPage notifies creating new wiki pages to notifiers
diff --git a/routers/api/v1/repo/mirror.go b/routers/api/v1/repo/mirror.go
index 9d8497927e..60f1bfe0d3 100644
--- a/routers/api/v1/repo/mirror.go
+++ b/routers/api/v1/repo/mirror.go
@@ -14,7 +14,6 @@ import (
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/context"
- mirror_module "code.gitea.io/gitea/modules/mirror"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
@@ -70,7 +69,7 @@ func MirrorSync(ctx *context.APIContext) {
return
}
- mirror_module.AddPullMirrorToQueue(repo.ID)
+ mirror_service.AddPullMirrorToQueue(repo.ID)
ctx.Status(http.StatusOK)
}
diff --git a/routers/web/repo/setting/setting.go b/routers/web/repo/setting/setting.go
index 71c1939f29..af09e240d5 100644
--- a/routers/web/repo/setting/setting.go
+++ b/routers/web/repo/setting/setting.go
@@ -24,7 +24,6 @@ import (
"code.gitea.io/gitea/modules/indexer/stats"
"code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/log"
- mirror_module "code.gitea.io/gitea/modules/mirror"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
@@ -277,7 +276,7 @@ func SettingsPost(ctx *context.Context) {
return
}
- mirror_module.AddPullMirrorToQueue(repo.ID)
+ mirror_service.AddPullMirrorToQueue(repo.ID)
ctx.Flash.Info(ctx.Tr("repo.settings.mirror_sync_in_progress"))
ctx.Redirect(repo.Link() + "/settings")
@@ -294,7 +293,7 @@ func SettingsPost(ctx *context.Context) {
return
}
- mirror_module.AddPushMirrorToQueue(m.ID)
+ mirror_service.AddPushMirrorToQueue(m.ID)
ctx.Flash.Info(ctx.Tr("repo.settings.mirror_sync_in_progress"))
ctx.Redirect(repo.Link() + "/settings")
@@ -332,7 +331,7 @@ func SettingsPost(ctx *context.Context) {
// If we observed its implementation in the context of `push-mirror-sync` where it
// is evident that pushing to the queue is necessary for updates.
// So, there are updates within the given interval, it is necessary to update the queue accordingly.
- mirror_module.AddPushMirrorToQueue(m.ID)
+ mirror_service.AddPushMirrorToQueue(m.ID)
ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
ctx.Redirect(repo.Link() + "/settings")
diff --git a/services/mirror/mirror.go b/services/mirror/mirror.go
index abce1d3c2d..0fc871b214 100644
--- a/services/mirror/mirror.go
+++ b/services/mirror/mirror.go
@@ -10,21 +10,20 @@ import (
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log"
- mirror_module "code.gitea.io/gitea/modules/mirror"
"code.gitea.io/gitea/modules/queue"
"code.gitea.io/gitea/modules/setting"
)
// doMirrorSync causes this request to mirror itself
-func doMirrorSync(ctx context.Context, req *mirror_module.SyncRequest) {
+func doMirrorSync(ctx context.Context, req *SyncRequest) {
if req.ReferenceID == 0 {
log.Warn("Skipping mirror sync request, no mirror ID was specified")
return
}
switch req.Type {
- case mirror_module.PushMirrorType:
+ case PushMirrorType:
_ = SyncPushMirror(ctx, req.ReferenceID)
- case mirror_module.PullMirrorType:
+ case PullMirrorType:
_ = SyncPullMirror(ctx, req.ReferenceID)
default:
log.Error("Unknown Request type in queue: %v for MirrorID[%d]", req.Type, req.ReferenceID)
@@ -43,7 +42,7 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error {
handler := func(idx int, bean any) error {
var repo *repo_model.Repository
- var mirrorType mirror_module.SyncType
+ var mirrorType SyncType
var referenceID int64
if m, ok := bean.(*repo_model.Mirror); ok {
@@ -52,7 +51,7 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error {
return nil
}
repo = m.Repo
- mirrorType = mirror_module.PullMirrorType
+ mirrorType = PullMirrorType
referenceID = m.RepoID
} else if m, ok := bean.(*repo_model.PushMirror); ok {
if m.GetRepository() == nil {
@@ -60,7 +59,7 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error {
return nil
}
repo = m.Repo
- mirrorType = mirror_module.PushMirrorType
+ mirrorType = PushMirrorType
referenceID = m.ID
} else {
log.Error("Unknown bean: %v", bean)
@@ -75,9 +74,9 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error {
}
// Push to the Queue
- if err := mirror_module.PushToQueue(mirrorType, referenceID); err != nil {
+ if err := PushToQueue(mirrorType, referenceID); err != nil {
if err == queue.ErrAlreadyInQueue {
- if mirrorType == mirror_module.PushMirrorType {
+ if mirrorType == PushMirrorType {
log.Trace("PushMirrors for %-v already queued for sync", repo)
} else {
log.Trace("PullMirrors for %-v already queued for sync", repo)
@@ -120,7 +119,7 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error {
return nil
}
-func queueHandler(items ...*mirror_module.SyncRequest) []*mirror_module.SyncRequest {
+func queueHandler(items ...*SyncRequest) []*SyncRequest {
for _, req := range items {
doMirrorSync(graceful.GetManager().ShutdownContext(), req)
}
@@ -129,5 +128,5 @@ func queueHandler(items ...*mirror_module.SyncRequest) []*mirror_module.SyncRequ
// InitSyncMirrors initializes a go routine to sync the mirrors
func InitSyncMirrors() {
- mirror_module.StartSyncMirrors(queueHandler)
+ StartSyncMirrors(queueHandler)
}
diff --git a/services/mirror/mirror_push.go b/services/mirror/mirror_push.go
index 2c1b00b60c..594d31df89 100644
--- a/services/mirror/mirror_push.go
+++ b/services/mirror/mirror_push.go
@@ -253,3 +253,15 @@ func pushAllLFSObjects(ctx context.Context, gitRepo *git.Repository, lfsClient l
return nil
}
+
+func syncPushMirrorWithSyncOnCommit(ctx context.Context, repoID int64) {
+ pushMirrors, err := repo_model.GetPushMirrorsSyncedOnCommit(ctx, repoID)
+ if err != nil {
+ log.Error("repo_model.GetPushMirrorsSyncedOnCommit failed: %v", err)
+ return
+ }
+
+ for _, mirror := range pushMirrors {
+ AddPushMirrorToQueue(mirror.ID)
+ }
+}
diff --git a/modules/notification/mirror/mirror.go b/services/mirror/notifier.go
index 426e36ee58..e0e1b443e0 100644
--- a/modules/notification/mirror/mirror.go
+++ b/services/mirror/notifier.go
@@ -8,23 +8,21 @@ import (
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
- "code.gitea.io/gitea/modules/log"
- mirror_module "code.gitea.io/gitea/modules/mirror"
+ "code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/notification/base"
"code.gitea.io/gitea/modules/repository"
)
+func init() {
+ notification.RegisterNotifier(&mirrorNotifier{})
+}
+
type mirrorNotifier struct {
base.NullNotifier
}
var _ base.Notifier = &mirrorNotifier{}
-// NewNotifier create a new mirrorNotifier notifier
-func NewNotifier() base.Notifier {
- return &mirrorNotifier{}
-}
-
func (m *mirrorNotifier) NotifyPushCommits(ctx context.Context, _ *user_model.User, repo *repo_model.Repository, _ *repository.PushUpdateOptions, _ *repository.PushCommits) {
syncPushMirrorWithSyncOnCommit(ctx, repo.ID)
}
@@ -32,15 +30,3 @@ func (m *mirrorNotifier) NotifyPushCommits(ctx context.Context, _ *user_model.Us
func (m *mirrorNotifier) NotifySyncPushCommits(ctx context.Context, _ *user_model.User, repo *repo_model.Repository, _ *repository.PushUpdateOptions, _ *repository.PushCommits) {
syncPushMirrorWithSyncOnCommit(ctx, repo.ID)
}
-
-func syncPushMirrorWithSyncOnCommit(ctx context.Context, repoID int64) {
- pushMirrors, err := repo_model.GetPushMirrorsSyncedOnCommit(ctx, repoID)
- if err != nil {
- log.Error("repo_model.GetPushMirrorsSyncedOnCommit failed: %v", err)
- return
- }
-
- for _, mirror := range pushMirrors {
- mirror_module.AddPushMirrorToQueue(mirror.ID)
- }
-}
diff --git a/modules/mirror/mirror.go b/services/mirror/queue.go
index 0d9a624730..0d9a624730 100644
--- a/modules/mirror/mirror.go
+++ b/services/mirror/queue.go