Skip to content

Commit

Permalink
Delete protected branch if repository gets removed (#15658)
Browse files Browse the repository at this point in the history
* Added missing error parameters.

* Delete protected branch if repository gets removed.

* Added doctor fix.
  • Loading branch information
KN4CK3R authored Apr 29, 2021
1 parent d576126 commit 2e85165
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
34 changes: 26 additions & 8 deletions modules/doctor/dbconsistency.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down

0 comments on commit 2e85165

Please sign in to comment.