From 260c7309da6f43af40dedf927c17e495872e4e3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Gardfj=C3=A4ll?= Date: Fri, 1 Apr 2022 16:06:02 +0200 Subject: [PATCH 1/2] An attempt to sync a non-mirror repo must give 400 (Bad Request) --- integrations/api_repo_test.go | 21 +++++++++++++++++++++ routers/api/v1/repo/mirror.go | 10 ++++++++++ 2 files changed, 31 insertions(+) diff --git a/integrations/api_repo_test.go b/integrations/api_repo_test.go index ce1ecb1d43d8a..b585ad15e3a1e 100644 --- a/integrations/api_repo_test.go +++ b/integrations/api_repo_test.go @@ -405,6 +405,27 @@ func testAPIRepoMigrateConflict(t *testing.T, u *url.URL) { }) } +// mirror-sync must fail with "400 (Bad Request)" when an attempt is made to +// sync a non-mirror repository. +func TestAPIMirrorSyncNonMirrorRepo(t *testing.T) { + defer prepareTestEnv(t)() + + session := loginUser(t, "user2") + token := getTokenForLoggedInUser(t, session) + + var repo api.Repository + req := NewRequest(t, "GET", "/api/v1/repos/user2/repo1") + resp := MakeRequest(t, req, http.StatusOK) + DecodeJSON(t, resp, &repo) + assert.EqualValues(t, false, repo.Mirror) + + req = NewRequestf(t, "POST", "/api/v1/repos/user2/repo1/mirror-sync?token=%s", token) + resp = session.MakeRequest(t, req, http.StatusBadRequest) + errRespJSON := map[string]string{} + DecodeJSON(t, resp, &errRespJSON) + assert.Equal(t, "Repository is not a mirror", errRespJSON["message"]) +} + func TestAPIOrgRepoCreate(t *testing.T) { testCases := []struct { ctxUserID int64 diff --git a/routers/api/v1/repo/mirror.go b/routers/api/v1/repo/mirror.go index c9ac3e829230e..e4cae0e3c9308 100644 --- a/routers/api/v1/repo/mirror.go +++ b/routers/api/v1/repo/mirror.go @@ -5,8 +5,10 @@ package repo import ( + "errors" "net/http" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/setting" @@ -48,6 +50,14 @@ func MirrorSync(ctx *context.APIContext) { return } + if _, err := repo_model.GetMirrorByRepoID(repo.ID); err != nil { + if errors.Is(err, repo_model.ErrMirrorNotExist) { + ctx.Error(http.StatusBadRequest, "MirrorSync", "Repository is not a mirror") + return + } + ctx.Error(http.StatusInternalServerError, "MirrorSync", err) + } + mirror_service.StartToMirror(repo.ID) ctx.Status(http.StatusOK) From 72c05e5659101ce2e5a8399c7cdfb345bd486ead Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Gardfj=C3=A4ll?= Date: Fri, 1 Apr 2022 16:14:44 +0200 Subject: [PATCH 2/2] add missing return statement --- routers/api/v1/repo/mirror.go | 1 + 1 file changed, 1 insertion(+) diff --git a/routers/api/v1/repo/mirror.go b/routers/api/v1/repo/mirror.go index e4cae0e3c9308..d7facd24d96cc 100644 --- a/routers/api/v1/repo/mirror.go +++ b/routers/api/v1/repo/mirror.go @@ -56,6 +56,7 @@ func MirrorSync(ctx *context.APIContext) { return } ctx.Error(http.StatusInternalServerError, "MirrorSync", err) + return } mirror_service.StartToMirror(repo.ID)