Skip to content

Commit 8c27049

Browse files
committed
fix
1 parent 9786720 commit 8c27049

File tree

4 files changed

+58
-112
lines changed

4 files changed

+58
-112
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: 8 additions & 27 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
}
@@ -346,18 +345,18 @@ func SettingsPost(ctx *context.Context) {
346345
// as an error on the UI for this action
347346
ctx.Data["Err_RepoName"] = nil
348347

349-
m, err := selectPushMirrorByForm(ctx, form, repo)
350-
if err != nil {
348+
m, _, _ := repo_model.GetPushMirrorByIDAndRepoID(ctx, form.PushMirrorID, repo.ID)
349+
if m == nil {
351350
ctx.NotFound("", nil)
352351
return
353352
}
354353

355-
if err = mirror_service.RemovePushMirrorRemote(ctx, m); err != nil {
354+
if err := mirror_service.RemovePushMirrorRemote(ctx, m); err != nil {
356355
ctx.ServerError("RemovePushMirrorRemote", err)
357356
return
358357
}
359358

360-
if err = repo_model.DeletePushMirrors(ctx, repo_model.PushMirrorOptions{ID: m.ID, RepoID: m.RepoID}); err != nil {
359+
if err := repo_model.DeletePushMirrors(ctx, repo_model.PushMirrorOptions{ID: m.ID, RepoID: m.RepoID}); err != nil {
361360
ctx.ServerError("DeletePushMirrorByID", err)
362361
return
363362
}
@@ -993,21 +992,3 @@ func handleSettingRemoteAddrError(ctx *context.Context, err error, form *forms.R
993992
}
994993
ctx.RenderWithErr(ctx.Tr("repo.mirror_address_url_invalid"), tplSettingsOptions, form)
995994
}
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

tests/integration/mirror_push_test.go

Lines changed: 45 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"code.gitea.io/gitea/modules/git"
2020
"code.gitea.io/gitea/modules/gitrepo"
2121
"code.gitea.io/gitea/modules/setting"
22-
"code.gitea.io/gitea/modules/test"
2322
gitea_context "code.gitea.io/gitea/services/context"
2423
"code.gitea.io/gitea/services/migrations"
2524
mirror_service "code.gitea.io/gitea/services/mirror"
@@ -124,78 +123,59 @@ func doRemovePushMirror(ctx APITestContext, address, username, password string,
124123

125124
func TestRepoSettingPushMirror(t *testing.T) {
126125
defer tests.PrepareTestEnv(t)()
126+
setting.Migrations.AllowLocalNetworks = true
127+
assert.NoError(t, migrations.Init())
127128

128129
session := loginUser(t, "user2")
129-
130-
repoPrefix := "/user2/repo2"
131130
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
132131

133-
defer func() {
134-
migrations.Init()
135-
}()
136-
defer test.MockVariableValue(&setting.Migrations.AllowedDomains, "127.0.0.1")()
137-
assert.NoError(t, migrations.Init())
138-
139-
// visit repository setting page
140-
req := NewRequest(t, "GET", repoPrefix+"/settings")
141-
resp := session.MakeRequest(t, req, http.StatusOK)
142-
htmlDoc := NewHTMLParser(t, resp.Body)
143-
144-
onGiteaRun(t, func(t *testing.T, u *url.URL) {
145-
t.Run("Push Mirror Add", func(t *testing.T) {
146-
req = NewRequestWithValues(t, "POST", repoPrefix+"/settings", map[string]string{
147-
"_csrf": htmlDoc.GetCSRF(),
148-
"action": "push-mirror-add",
149-
"push_mirror_address": u.String() + "/user1/repo1.git",
150-
"push_mirror_interval": "0",
151-
})
152-
session.MakeRequest(t, req, http.StatusSeeOther)
153-
154-
flashCookie := session.GetCookie(gitea_context.CookieNameFlash)
155-
assert.NotNil(t, flashCookie)
156-
assert.Contains(t, flashCookie.Value, "success")
157-
158-
mirrors, cnt, err := repo_model.GetPushMirrorsByRepoID(db.DefaultContext, repo2.ID, db.ListOptions{})
159-
assert.NoError(t, err)
160-
assert.Len(t, mirrors, 1)
161-
assert.EqualValues(t, 1, cnt)
162-
assert.EqualValues(t, 0, mirrors[0].Interval)
132+
t.Run("Push Mirror Add", func(t *testing.T) {
133+
req := NewRequestWithValues(t, "POST", "/user2/repo2/settings", map[string]string{
134+
"_csrf": GetUserCSRFToken(t, session),
135+
"action": "push-mirror-add",
136+
"push_mirror_address": "https://127.0.0.1/user1/repo1.git",
137+
"push_mirror_interval": "24h",
163138
})
139+
session.MakeRequest(t, req, http.StatusSeeOther)
164140

165-
mirrors, _, _ := repo_model.GetPushMirrorsByRepoID(db.DefaultContext, repo2.ID, db.ListOptions{})
166-
167-
t.Run("Push Mirror Update", func(t *testing.T) {
168-
req := NewRequestWithValues(t, "POST", repoPrefix+"/settings", map[string]string{
169-
"_csrf": htmlDoc.GetCSRF(),
170-
"action": "push-mirror-update",
171-
"push_mirror_id": strconv.FormatInt(mirrors[0].ID, 10),
172-
"push_mirror_interval": "10m0s",
173-
})
174-
session.MakeRequest(t, req, http.StatusSeeOther)
175-
176-
mirror, err := repo_model.GetPushMirrorByID(db.DefaultContext, mirrors[0].ID)
177-
assert.NoError(t, err)
178-
assert.EqualValues(t, 10*time.Minute, mirror.Interval)
179-
180-
req = NewRequestWithValues(t, "POST", repoPrefix+"/settings", map[string]string{
181-
"_csrf": htmlDoc.GetCSRF(),
182-
"action": "push-mirror-update",
183-
"push_mirror_id": strconv.FormatInt(9999, 10), // 1 is an mirror ID which is not exist
184-
"push_mirror_interval": "10m0s",
185-
})
186-
session.MakeRequest(t, req, http.StatusNotFound)
187-
})
141+
flashCookie := session.GetCookie(gitea_context.CookieNameFlash)
142+
assert.NotNil(t, flashCookie)
143+
assert.Contains(t, flashCookie.Value, "success")
188144

189-
t.Run("Push Mirror Remove", func(t *testing.T) {
190-
req := NewRequestWithValues(t, "POST", repoPrefix+"/settings", map[string]string{
191-
"_csrf": htmlDoc.GetCSRF(),
192-
"action": "push-mirror-remove",
193-
"push_mirror_id": strconv.FormatInt(mirrors[0].ID, 10),
194-
})
195-
session.MakeRequest(t, req, http.StatusSeeOther)
145+
pushMirrors, cnt, err := repo_model.GetPushMirrorsByRepoID(db.DefaultContext, repo2.ID, db.ListOptions{})
146+
assert.NoError(t, err)
147+
assert.Len(t, pushMirrors, 1)
148+
assert.EqualValues(t, 1, cnt)
149+
assert.EqualValues(t, 24*time.Hour, pushMirrors[0].Interval)
150+
repo2PushMirrorID := pushMirrors[0].ID
151+
152+
// update repo2 push mirror
153+
req = NewRequestWithValues(t, "POST", "/user2/repo2/settings", map[string]string{
154+
"_csrf": GetUserCSRFToken(t, session),
155+
"action": "push-mirror-update",
156+
"push_mirror_id": strconv.FormatInt(repo2PushMirrorID, 10),
157+
"push_mirror_interval": "10m0s",
158+
})
159+
session.MakeRequest(t, req, http.StatusSeeOther)
160+
mirror := unittest.AssertExistsAndLoadBean(t, &repo_model.PushMirror{ID: repo2PushMirrorID})
161+
assert.EqualValues(t, 10*time.Minute, mirror.Interval)
162+
163+
// avoid updating repo1 push mirror
164+
req = NewRequestWithValues(t, "POST", "/user2/repo1/settings", map[string]string{
165+
"_csrf": GetUserCSRFToken(t, session),
166+
"action": "push-mirror-update",
167+
"push_mirror_id": strconv.FormatInt(repo2PushMirrorID, 10),
168+
"push_mirror_interval": "20m0s",
169+
})
170+
session.MakeRequest(t, req, http.StatusNotFound)
196171

197-
_, err := repo_model.GetPushMirrorByID(db.DefaultContext, mirrors[0].ID)
198-
assert.Error(t, err)
172+
// delete repo2 push mirror
173+
req = NewRequestWithValues(t, "POST", "/user2/repo2/settings", map[string]string{
174+
"_csrf": GetUserCSRFToken(t, session),
175+
"action": "push-mirror-remove",
176+
"push_mirror_id": strconv.FormatInt(repo2PushMirrorID, 10),
199177
})
178+
session.MakeRequest(t, req, http.StatusSeeOther)
179+
unittest.AssertNotExistsBean(t, &repo_model.PushMirror{ID: repo2PushMirrorID})
200180
})
201181
}

0 commit comments

Comments
 (0)