forgejo/services/doctor/push_mirror_consistency.go
Gergely Nagy ff00749191
[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 commit 9038e07ef35978336612588d68c1315179a45c73)
(cherry picked from commit b15bafcbc7d9033b0cc7b0fd888915b117e08d42)
(cherry picked from commit 93ba05a2dd9fdec46f337542cd5f22c8960ac55f)
(cherry picked from commit e418ea80822361e387b460c583592bbd83d4a39e)
(cherry picked from commit 321790a91ec8553d1b3668f606ebec762865dd17)
(cherry picked from commit f4e19d332392cb455b3b4e32e271f3e42302bbc8)
(cherry picked from commit 4d9923dee851a4046050761d3dd352f2f343f4fc)
(cherry picked from commit 049df69eda1ceb47f6e74c9a67e9ce5041e65c3b)

Conflicts:
	services/doctor/push_mirror_consistency.go
	https://codeberg.org/forgejo/forgejo/pulls/2214
(cherry picked from commit c79cba8d556320be0da7ca8324b39cd8930465bf)
(cherry picked from commit f3a3969c02cade7261a5f25c9e342800ccdf9111)
2024-02-05 16:09:41 +01:00

92 lines
2.4 KiB
Go

// Copyright 2023 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
// SPDX-License-Identifier: MIT
package doctor
import (
"context"
"strings"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/log"
"xorm.io/builder"
)
func FixPushMirrorsWithoutGitRemote(ctx context.Context, logger log.Logger, autofix bool) error {
var missingMirrors []*repo_model.PushMirror
err := db.Iterate(ctx, builder.Gt{"id": 0}, func(ctx context.Context, repo *repo_model.Repository) error {
pushMirrors, _, err := repo_model.GetPushMirrorsByRepoID(ctx, repo.ID, db.ListOptions{})
if err != nil {
return err
}
for i := 0; i < len(pushMirrors); i++ {
_, err = repo_model.GetPushMirrorRemoteAddress(repo.OwnerName, repo.Name, pushMirrors[i].RemoteName)
if err != nil {
if strings.Contains(err.Error(), "No such remote") {
missingMirrors = append(missingMirrors, pushMirrors[i])
} else if logger != nil {
logger.Warn("Unable to retrieve the remote address of a mirror: %s", err)
}
}
}
return nil
})
if err != nil {
if logger != nil {
logger.Critical("Unable to iterate across repounits to fix push mirrors without a git remote: Error %v", err)
}
return err
}
count := len(missingMirrors)
if !autofix {
if logger != nil {
if count == 0 {
logger.Info("Found no push mirrors with missing git remotes")
} else {
logger.Warn("Found %d push mirrors with missing git remotes", count)
}
}
return nil
}
for i := 0; i < len(missingMirrors); i++ {
if logger != nil {
logger.Info("Removing push mirror #%d (remote: %s), for repo: %s/%s",
missingMirrors[i].ID,
missingMirrors[i].RemoteName,
missingMirrors[i].GetRepository(ctx).OwnerName,
missingMirrors[i].GetRepository(ctx).Name)
}
err = repo_model.DeletePushMirrors(ctx, repo_model.PushMirrorOptions{
ID: missingMirrors[i].ID,
RepoID: missingMirrors[i].RepoID,
RemoteName: missingMirrors[i].RemoteName,
})
if err != nil {
if logger != nil {
logger.Critical("Error removing a push mirror (repo_id: %d, push_mirror: %d): %s", missingMirrors[i].Repo.ID, missingMirrors[i].ID, err)
}
return err
}
}
return nil
}
func init() {
Register(&Check{
Title: "Check for push mirrors without a git remote configured",
Name: "fix-push-mirrors-without-git-remote",
IsDefault: false,
Run: FixPushMirrorsWithoutGitRemote,
Priority: 7,
})
}