forked from forgejo/forgejo
[GITEA] new doctor check: fix-push-mirrors-without-git-remote (#1853)
This adds a new `doctor` check: `fix-push-mirrors-without-git-remote`. The new check looks for push mirrors that do not have their remotes configured in git. If automatic fixing is enabled, it will remove these push mirrors from the database. The check is not run by default, and thus, must be invoked manually. It should be usable in a half-migrated state, too, and as such, fixes #1800. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/1853 Co-authored-by: Gergely Nagy <forgejo@gergo.csillger.hu> Co-committed-by: Gergely Nagy <forgejo@gergo.csillger.hu> (cherry picked from commit9038e07ef3
) (cherry picked from commitb15bafcbc7
)
This commit is contained in:
parent
0eae22d429
commit
93ba05a2dd
4 changed files with 145 additions and 27 deletions
|
@ -16,6 +16,7 @@ import (
|
|||
"code.gitea.io/gitea/models/unittest"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
gitea_context "code.gitea.io/gitea/modules/context"
|
||||
doctor "code.gitea.io/gitea/modules/doctor"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/services/migrations"
|
||||
|
@ -47,10 +48,11 @@ func testMirrorPush(t *testing.T, u *url.URL) {
|
|||
ctx := NewAPITestContext(t, user.LowerName, srcRepo.Name)
|
||||
|
||||
doCreatePushMirror(ctx, fmt.Sprintf("%s%s/%s", u.String(), url.PathEscape(ctx.Username), url.PathEscape(mirrorRepo.Name)), user.LowerName, userPassword)(t)
|
||||
doCreatePushMirror(ctx, fmt.Sprintf("%s%s/%s", u.String(), url.PathEscape(ctx.Username), url.PathEscape("does-not-matter")), user.LowerName, userPassword)(t)
|
||||
|
||||
mirrors, _, err := repo_model.GetPushMirrorsByRepoID(db.DefaultContext, srcRepo.ID, db.ListOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, mirrors, 1)
|
||||
assert.Len(t, mirrors, 2)
|
||||
|
||||
ok := mirror_service.SyncPushMirror(context.Background(), mirrors[0].ID)
|
||||
assert.True(t, ok)
|
||||
|
@ -71,6 +73,30 @@ func testMirrorPush(t *testing.T, u *url.URL) {
|
|||
|
||||
assert.Equal(t, srcCommit.ID, mirrorCommit.ID)
|
||||
|
||||
// Test that we can "repair" push mirrors where the remote doesn't exist in git's state.
|
||||
// To do that, we artificially remove the remote...
|
||||
cmd := git.NewCommand(db.DefaultContext, "remote", "rm").AddDynamicArguments(mirrors[0].RemoteName)
|
||||
_, _, err = cmd.RunStdString(&git.RunOpts{Dir: srcRepo.RepoPath()})
|
||||
assert.NoError(t, err)
|
||||
|
||||
// ...then ensure that trying to get its remote address fails
|
||||
_, err = repo_model.GetPushMirrorRemoteAddress(srcRepo.OwnerName, srcRepo.Name, mirrors[0].RemoteName)
|
||||
assert.Error(t, err)
|
||||
|
||||
// ...and that we can fix it.
|
||||
err = doctor.FixPushMirrorsWithoutGitRemote(db.DefaultContext, nil, true)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// ...and after fixing, we only have one remote
|
||||
mirrors, _, err = repo_model.GetPushMirrorsByRepoID(db.DefaultContext, srcRepo.ID, db.ListOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, mirrors, 1)
|
||||
|
||||
// ...one we can get the address of, and it's not the one we removed
|
||||
remoteAddress, err := repo_model.GetPushMirrorRemoteAddress(srcRepo.OwnerName, srcRepo.Name, mirrors[0].RemoteName)
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, remoteAddress, "does-not-matter")
|
||||
|
||||
// Cleanup
|
||||
doRemovePushMirror(ctx, fmt.Sprintf("%s%s/%s", u.String(), url.PathEscape(ctx.Username), url.PathEscape(mirrorRepo.Name)), user.LowerName, userPassword, int(mirrors[0].ID))(t)
|
||||
mirrors, _, err = repo_model.GetPushMirrorsByRepoID(db.DefaultContext, srcRepo.ID, db.ListOptions{})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue