fix(api): handle partial failures in push mirror synchronization gracefully (#37782)

This MR fixes an issue in the sync push mirrors endpoint.

Previously, when triggering the synchronization of all push mirrors for
a specific repository, the entire operation would stop if a single
mirror failed for any reason. As a result, the remaining mirrors were
not processed.

With this fix, failures on individual push mirrors no longer abort the
whole synchronization process.

---------

Signed-off-by: Nicolas <bircni@icloud.com>
Co-authored-by: Nicolas <bircni@icloud.com>
Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: Claude (Opus 4.7) <noreply@anthropic.com>
This commit is contained in:
Mohamed Sekour
2026-05-22 11:53:19 +02:00
committed by GitHub
parent 9d737a6400
commit bf1b54c3e3
4 changed files with 56 additions and 3 deletions

View File

@@ -6,6 +6,7 @@ package repo
import (
"errors"
"net/http"
"strings"
"time"
"code.gitea.io/gitea/models/db"
@@ -101,6 +102,8 @@ func PushMirrorSync(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"
// "404":
// "$ref": "#/responses/notFound"
// "422":
// "$ref": "#/responses/validationError"
if !setting.Mirror.Enabled {
ctx.APIError(http.StatusBadRequest, "Mirror feature is disabled")
@@ -112,14 +115,18 @@ func PushMirrorSync(ctx *context.APIContext) {
ctx.APIError(http.StatusNotFound, err)
return
}
failedPushMirrors := make([]string, 0)
for _, mirror := range pushMirrors {
ok := mirror_service.SyncPushMirror(ctx, mirror.ID)
if !ok {
ctx.APIErrorInternal(errors.New("error occurred when syncing push mirror " + mirror.RemoteName))
return
failedPushMirrors = append(failedPushMirrors, mirror.RemoteName)
}
}
if len(failedPushMirrors) != 0 {
ctx.APIError(http.StatusUnprocessableEntity, "error occurred when syncing push mirrors: "+strings.Join(failedPushMirrors, ", "))
return
}
ctx.Status(http.StatusOK)
}

View File

@@ -0,0 +1,40 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repo
import (
"net/http"
"testing"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/services/contexttest"
"github.com/stretchr/testify/assert"
)
// TestPushMirrorSync verifies the endpoint attempts every push mirror instead
// of aborting on the first failure, reporting all failed remotes with a 422.
// Each remote name is not a configured git remote, so SyncPushMirror fails fast
// without any network access.
func TestPushMirrorSync(t *testing.T) {
unittest.PrepareTestEnv(t)
defer test.MockVariableValue(&setting.Mirror.Enabled, true)()
for _, remoteName := range []string{"broken_remote_1", "broken_remote_2"} {
assert.NoError(t, db.Insert(t.Context(), &repo_model.PushMirror{RepoID: 1, RemoteName: remoteName}))
}
ctx, resp := contexttest.MockAPIContext(t, "user2/repo1")
contexttest.LoadRepo(t, ctx, 1)
PushMirrorSync(ctx)
assert.Equal(t, http.StatusUnprocessableEntity, ctx.Resp.WrittenStatus())
assert.Contains(t, resp.Body.String(), "broken_remote_1")
assert.Contains(t, resp.Body.String(), "broken_remote_2")
}

View File

@@ -15646,6 +15646,9 @@
},
"404": {
"$ref": "#/responses/notFound"
},
"422": {
"$ref": "#/responses/validationError"
}
}
}

View File

@@ -27446,6 +27446,9 @@
},
"404": {
"$ref": "#/components/responses/notFound"
},
"422": {
"$ref": "#/components/responses/validationError"
}
},
"summary": "Sync all push mirrored repository",