Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doctor dbconsistency fix to delete repos with no owner #27290

Merged
merged 20 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions modules/doctor/dbconsistency.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ func checkDBConsistency(ctx context.Context, logger log.Logger, autofix bool) er
Fixer: activities_model.FixActionCreatedUnixString,
FixedMessage: "Set to zero",
},
{
Name: "Repos with no existing owner",
Counter: CountOrphanedRepos,
Fixer: DeleteOrphanedRepos,
FixedMessage: "Deleted all related stuff of orphaned repos",
6543 marked this conversation as resolved.
Show resolved Hide resolved
},
}

// TODO: function to recalc all counters
Expand Down
49 changes: 49 additions & 0 deletions modules/doctor/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package doctor

import (
"context"

"code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user"
repo_service "code.gitea.io/gitea/services/repository"

"xorm.io/builder"
)

// CountOrphanedRepos count repository where user of owner_id do not exist
func CountOrphanedRepos(ctx context.Context) (int64, error) {
return db.CountOrphanedObjects(ctx, "repository", "user", "repository.owner_id=user.id")
}

// DeleteOrphanedRepos delete repository where user of owner_id do not exist
func DeleteOrphanedRepos(ctx context.Context) (int64, error) {
batchSize := db.MaxBatchInsertSize("repository")
e := db.GetEngine(ctx)
var deleted int64
adminUser := &user_model.User{IsAdmin: true}

for {
var ids []int64
if err := e.Table("`repository`").
Join("LEFT", "`user`", "repository.owner_id=user.id").
Where(builder.IsNull{"`user`.id"}).
Select("`repository`.id").Limit(batchSize).Find(&ids); err != nil {
return deleted, err
}

// if we don't get ids we have deleted them all
if len(ids) == 0 {
return deleted, nil
}

for _, id := range ids {
if err := repo_service.DeleteRepositoryDirectly(ctx, adminUser, 0, id, true); err != nil {
6543 marked this conversation as resolved.
Show resolved Hide resolved
return deleted, err
}
deleted++
}
}
}
15 changes: 9 additions & 6 deletions services/repository/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (

// DeleteRepository deletes a repository for a user or organization.
// make sure if you call this func to close open sessions (sqlite will otherwise get a deadlock)
func DeleteRepositoryDirectly(ctx context.Context, doer *user_model.User, uid, repoID int64) error {
func DeleteRepositoryDirectly(ctx context.Context, doer *user_model.User, uid, repoID int64, ignoreOrgTeams ...bool) error {
ctx, committer, err := db.TxContext(ctx)
if err != nil {
return err
Expand All @@ -53,10 +53,13 @@ func DeleteRepositoryDirectly(ctx context.Context, doer *user_model.User, uid, r
return fmt.Errorf("list actions artifacts of repo %v: %w", repoID, err)
}

// In case is a organization.
org, err := user_model.GetUserByID(ctx, uid)
if err != nil {
return err
// In case owner is a organization, we have to change repo specific teams
// if ignoreOrgTeams is not true
var org *user_model.User
if len(ignoreOrgTeams) == 0 || !ignoreOrgTeams[0] {
if org, err = user_model.GetUserByID(ctx, uid); err != nil {
return err
}
}

repo := &repo_model.Repository{OwnerID: uid}
Expand Down Expand Up @@ -95,7 +98,7 @@ func DeleteRepositoryDirectly(ctx context.Context, doer *user_model.User, uid, r
}
}

if org.IsOrganization() {
if org != nil && org.IsOrganization() {
teams, err := organization.FindOrgTeams(ctx, org.ID)
if err != nil {
return err
Expand Down