Skip to content

Commit 5410305

Browse files
committed
fix
1 parent 9786720 commit 5410305

File tree

6 files changed

+95
-173
lines changed

6 files changed

+95
-173
lines changed

models/repo/pushmirror.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ import (
1616
"xorm.io/builder"
1717
)
1818

19-
// ErrPushMirrorNotExist mirror does not exist error
20-
var ErrPushMirrorNotExist = util.NewNotExistErrorf("PushMirror does not exist")
21-
2219
// PushMirror represents mirror information of a repository.
2320
type PushMirror struct {
2421
ID int64 `xorm:"pk autoincr"`
@@ -122,25 +119,13 @@ func GetPushMirrorsByRepoID(ctx context.Context, repoID int64, listOptions db.Li
122119
})
123120
}
124121

125-
func GetPushMirrorByIDAndRepoID(ctx context.Context, id, repoID int64) (*PushMirror, error) {
122+
func GetPushMirrorByIDAndRepoID(ctx context.Context, id, repoID int64) (*PushMirror, bool, error) {
126123
var pushMirror PushMirror
127124
has, err := db.GetEngine(ctx).Where("id = ?", id).And("repo_id = ?", repoID).Get(&pushMirror)
128-
if err != nil {
129-
return nil, err
130-
} else if !has {
131-
return nil, ErrPushMirrorNotExist
132-
}
133-
return &pushMirror, nil
134-
}
135-
136-
func GetPushMirrorByID(ctx context.Context, id int64) (*PushMirror, error) {
137-
mirror, has, err := db.GetByID[PushMirror](ctx, id)
138-
if err != nil {
139-
return nil, err
140-
} else if !has {
141-
return nil, ErrPushMirrorNotExist
125+
if !has || err != nil {
126+
return nil, has, err
142127
}
143-
return mirror, nil
128+
return &pushMirror, true, nil
144129
}
145130

146131
// GetPushMirrorsSyncedOnCommit returns push-mirrors for this repo that should be updated by new commits

routers/web/repo/setting/setting.go

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99
"fmt"
1010
"net/http"
11-
"strconv"
1211
"strings"
1312
"time"
1413

@@ -290,8 +289,8 @@ func SettingsPost(ctx *context.Context) {
290289
return
291290
}
292291

293-
m, err := selectPushMirrorByForm(ctx, form, repo)
294-
if err != nil {
292+
m, _, _ := repo_model.GetPushMirrorByIDAndRepoID(ctx, form.PushMirrorID, repo.ID)
293+
if m == nil {
295294
ctx.NotFound("", nil)
296295
return
297296
}
@@ -317,8 +316,8 @@ func SettingsPost(ctx *context.Context) {
317316
return
318317
}
319318

320-
m, err := selectPushMirrorByForm(ctx, form, repo)
321-
if err != nil {
319+
m, _, _ := repo_model.GetPushMirrorByIDAndRepoID(ctx, form.PushMirrorID, repo.ID)
320+
if m == nil {
322321
ctx.NotFound("", nil)
323322
return
324323
}
@@ -332,7 +331,10 @@ func SettingsPost(ctx *context.Context) {
332331
// If we observed its implementation in the context of `push-mirror-sync` where it
333332
// is evident that pushing to the queue is necessary for updates.
334333
// So, there are updates within the given interval, it is necessary to update the queue accordingly.
335-
mirror_service.AddPushMirrorToQueue(m.ID)
334+
if !ctx.FormBool("push_mirror_defer_sync") {
335+
// push_mirror_defer_sync is mainly for testing purpose, we do not really want to sync the push mirror immediately
336+
mirror_service.AddPushMirrorToQueue(m.ID)
337+
}
336338
ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
337339
ctx.Redirect(repo.Link() + "/settings")
338340

@@ -346,18 +348,18 @@ func SettingsPost(ctx *context.Context) {
346348
// as an error on the UI for this action
347349
ctx.Data["Err_RepoName"] = nil
348350

349-
m, err := selectPushMirrorByForm(ctx, form, repo)
350-
if err != nil {
351+
m, _, _ := repo_model.GetPushMirrorByIDAndRepoID(ctx, form.PushMirrorID, repo.ID)
352+
if m == nil {
351353
ctx.NotFound("", nil)
352354
return
353355
}
354356

355-
if err = mirror_service.RemovePushMirrorRemote(ctx, m); err != nil {
357+
if err := mirror_service.RemovePushMirrorRemote(ctx, m); err != nil {
356358
ctx.ServerError("RemovePushMirrorRemote", err)
357359
return
358360
}
359361

360-
if err = repo_model.DeletePushMirrors(ctx, repo_model.PushMirrorOptions{ID: m.ID, RepoID: m.RepoID}); err != nil {
362+
if err := repo_model.DeletePushMirrors(ctx, repo_model.PushMirrorOptions{ID: m.ID, RepoID: m.RepoID}); err != nil {
361363
ctx.ServerError("DeletePushMirrorByID", err)
362364
return
363365
}
@@ -993,21 +995,3 @@ func handleSettingRemoteAddrError(ctx *context.Context, err error, form *forms.R
993995
}
994996
ctx.RenderWithErr(ctx.Tr("repo.mirror_address_url_invalid"), tplSettingsOptions, form)
995997
}
996-
997-
func selectPushMirrorByForm(ctx *context.Context, form *forms.RepoSettingForm, repo *repo_model.Repository) (*repo_model.PushMirror, error) {
998-
id, err := strconv.ParseInt(form.PushMirrorID, 10, 64)
999-
if err != nil {
1000-
return nil, err
1001-
}
1002-
1003-
pushMirror, err := repo_model.GetPushMirrorByIDAndRepoID(ctx, id, repo.ID)
1004-
if err != nil {
1005-
if err == repo_model.ErrPushMirrorNotExist {
1006-
return nil, fmt.Errorf("PushMirror[%v] not associated to repository %v", id, repo)
1007-
}
1008-
return nil, err
1009-
}
1010-
1011-
pushMirror.Repo = repo
1012-
return pushMirror, nil
1013-
}

services/forms/repo_form.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ type RepoSettingForm struct {
122122
MirrorPassword string
123123
LFS bool `form:"mirror_lfs"`
124124
LFSEndpoint string `form:"mirror_lfs_endpoint"`
125-
PushMirrorID string
125+
PushMirrorID int64
126126
PushMirrorAddress string
127127
PushMirrorUsername string
128128
PushMirrorPassword string

services/mirror/mirror.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99

1010
repo_model "code.gitea.io/gitea/models/repo"
11-
"code.gitea.io/gitea/modules/graceful"
1211
"code.gitea.io/gitea/modules/log"
1312
"code.gitea.io/gitea/modules/queue"
1413
"code.gitea.io/gitea/modules/setting"
@@ -119,14 +118,7 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error {
119118
return nil
120119
}
121120

122-
func queueHandler(items ...*SyncRequest) []*SyncRequest {
123-
for _, req := range items {
124-
doMirrorSync(graceful.GetManager().ShutdownContext(), req)
125-
}
126-
return nil
127-
}
128-
129121
// InitSyncMirrors initializes a go routine to sync the mirrors
130122
func InitSyncMirrors() {
131-
StartSyncMirrors(queueHandler)
123+
StartSyncMirrors()
132124
}

services/mirror/queue.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,19 @@ type SyncRequest struct {
2828
ReferenceID int64 // RepoID for pull mirror, MirrorID for push mirror
2929
}
3030

31+
func queueHandler(items ...*SyncRequest) []*SyncRequest {
32+
for _, req := range items {
33+
doMirrorSync(graceful.GetManager().ShutdownContext(), req)
34+
}
35+
return nil
36+
}
37+
3138
// StartSyncMirrors starts a go routine to sync the mirrors
32-
func StartSyncMirrors(queueHandle func(data ...*SyncRequest) []*SyncRequest) {
39+
func StartSyncMirrors() {
3340
if !setting.Mirror.Enabled {
3441
return
3542
}
36-
mirrorQueue = queue.CreateUniqueQueue(graceful.GetManager().ShutdownContext(), "mirror", queueHandle)
43+
mirrorQueue = queue.CreateUniqueQueue(graceful.GetManager().ShutdownContext(), "mirror", queueHandler)
3744
if mirrorQueue == nil {
3845
log.Fatal("Unable to create mirror queue")
3946
}

0 commit comments

Comments
 (0)