From 2e8516534974ad23ca417a64edfb90d157989dab Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Fri, 30 Apr 2021 00:48:52 +0200 Subject: [PATCH] Delete protected branch if repository gets removed (#15658) * Added missing error parameters. * Delete protected branch if repository gets removed. * Added doctor fix. --- models/repo.go | 1 + modules/doctor/dbconsistency.go | 34 +++++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/models/repo.go b/models/repo.go index cea874769..0a0f725f0 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1472,6 +1472,7 @@ func DeleteRepository(doer *User, uid, repoID int64) error { &LanguageStat{RepoID: repoID}, &Comment{RefRepoID: repoID}, &Task{RepoID: repoID}, + &ProtectedBranch{RepoID: repoID}, ); err != nil { return fmt.Errorf("deleteBeans: %v", err) } diff --git a/modules/doctor/dbconsistency.go b/modules/doctor/dbconsistency.go index 8c960cb4a..417a7d4b6 100644 --- a/modules/doctor/dbconsistency.go +++ b/modules/doctor/dbconsistency.go @@ -29,7 +29,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error { if count > 0 { if autofix { if err = models.DeleteOrphanedLabels(); err != nil { - logger.Critical("Error: %v whilst deleting orphaned labels") + logger.Critical("Error: %v whilst deleting orphaned labels", err) return err } logger.Info("%d labels without existing repository/organisation deleted", count) @@ -47,7 +47,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error { if count > 0 { if autofix { if err = models.DeleteOrphanedIssueLabels(); err != nil { - logger.Critical("Error: %v whilst deleting orphaned issue_labels") + logger.Critical("Error: %v whilst deleting orphaned issue_labels", err) return err } logger.Info("%d issue_labels without existing label deleted", count) @@ -65,7 +65,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error { if count > 0 { if autofix { if err = models.DeleteOrphanedIssues(); err != nil { - logger.Critical("Error: %v whilst deleting orphaned issues") + logger.Critical("Error: %v whilst deleting orphaned issues", err) return err } logger.Info("%d issues without existing repository deleted", count) @@ -83,7 +83,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error { if count > 0 { if autofix { if err = models.DeleteOrphanedObjects("pull_request", "issue", "pull_request.issue_id=issue.id"); err != nil { - logger.Critical("Error: %v whilst deleting orphaned objects") + logger.Critical("Error: %v whilst deleting orphaned objects", err) return err } logger.Info("%d pull requests without existing issue deleted", count) @@ -101,7 +101,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error { if count > 0 { if autofix { if err = models.DeleteOrphanedObjects("tracked_time", "issue", "tracked_time.issue_id=issue.id"); err != nil { - logger.Critical("Error: %v whilst deleting orphaned objects") + logger.Critical("Error: %v whilst deleting orphaned objects", err) return err } logger.Info("%d tracked times without existing issue deleted", count) @@ -120,7 +120,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error { if autofix { updatedCount, err := models.FixNullArchivedRepository() if err != nil { - logger.Critical("Error: %v whilst fixing null archived repositories") + logger.Critical("Error: %v whilst fixing null archived repositories", err) return err } logger.Info("%d repositories with null is_archived updated", updatedCount) @@ -139,7 +139,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error { if autofix { updatedCount, err := models.FixCommentTypeLabelWithEmptyLabel() if err != nil { - logger.Critical("Error: %v whilst removing label comments with empty labels") + logger.Critical("Error: %v whilst removing label comments with empty labels", err) return err } logger.Info("%d label comments with empty labels removed", updatedCount) @@ -197,7 +197,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error { if autofix { err := models.FixBadSequences() if err != nil { - logger.Critical("Error: %v whilst attempting to fix sequences") + logger.Critical("Error: %v whilst attempting to fix sequences", err) return err } logger.Info("%d sequences updated", count) @@ -207,6 +207,24 @@ func checkDBConsistency(logger log.Logger, autofix bool) error { } } + // find protected branches without existing repository + count, err = models.CountOrphanedObjects("protected_branch", "repository", "protected_branch.repo_id=repository.id") + if err != nil { + logger.Critical("Error: %v whilst counting orphaned objects") + return err + } + if count > 0 { + if autofix { + if err = models.DeleteOrphanedObjects("protected_branch", "repository", "protected_branch.repo_id=repository.id"); err != nil { + logger.Critical("Error: %v whilst deleting orphaned objects", err) + return err + } + logger.Info("%d protected branches without existing repository deleted", count) + } else { + logger.Warn("%d protected branches without existing repository", count) + } + } + return nil }