Skip to content

Commit 43ff92e

Browse files
petergardfjalllunnytechknowlogick
authored
An attempt to sync a non-mirror repo must give 400 (Bad Request) (#19300)
* An attempt to sync a non-mirror repo must give 400 (Bad Request) * add missing return statement Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent 4f27c28 commit 43ff92e

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

integrations/api_repo_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,27 @@ func testAPIRepoMigrateConflict(t *testing.T, u *url.URL) {
405405
})
406406
}
407407

408+
// mirror-sync must fail with "400 (Bad Request)" when an attempt is made to
409+
// sync a non-mirror repository.
410+
func TestAPIMirrorSyncNonMirrorRepo(t *testing.T) {
411+
defer prepareTestEnv(t)()
412+
413+
session := loginUser(t, "user2")
414+
token := getTokenForLoggedInUser(t, session)
415+
416+
var repo api.Repository
417+
req := NewRequest(t, "GET", "/api/v1/repos/user2/repo1")
418+
resp := MakeRequest(t, req, http.StatusOK)
419+
DecodeJSON(t, resp, &repo)
420+
assert.EqualValues(t, false, repo.Mirror)
421+
422+
req = NewRequestf(t, "POST", "/api/v1/repos/user2/repo1/mirror-sync?token=%s", token)
423+
resp = session.MakeRequest(t, req, http.StatusBadRequest)
424+
errRespJSON := map[string]string{}
425+
DecodeJSON(t, resp, &errRespJSON)
426+
assert.Equal(t, "Repository is not a mirror", errRespJSON["message"])
427+
}
428+
408429
func TestAPIOrgRepoCreate(t *testing.T) {
409430
testCases := []struct {
410431
ctxUserID int64

routers/api/v1/repo/mirror.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
package repo
66

77
import (
8+
"errors"
89
"net/http"
910

11+
repo_model "code.gitea.io/gitea/models/repo"
1012
"code.gitea.io/gitea/models/unit"
1113
"code.gitea.io/gitea/modules/context"
1214
"code.gitea.io/gitea/modules/setting"
@@ -48,6 +50,15 @@ func MirrorSync(ctx *context.APIContext) {
4850
return
4951
}
5052

53+
if _, err := repo_model.GetMirrorByRepoID(repo.ID); err != nil {
54+
if errors.Is(err, repo_model.ErrMirrorNotExist) {
55+
ctx.Error(http.StatusBadRequest, "MirrorSync", "Repository is not a mirror")
56+
return
57+
}
58+
ctx.Error(http.StatusInternalServerError, "MirrorSync", err)
59+
return
60+
}
61+
5162
mirror_service.StartToMirror(repo.ID)
5263

5364
ctx.Status(http.StatusOK)

0 commit comments

Comments
 (0)