Skip to content

Commit 49b0039

Browse files
committed
Fix doctor deleting orphaned issues attachments
1 parent 8c9d2bd commit 49b0039

File tree

3 files changed

+39
-33
lines changed

3 files changed

+39
-33
lines changed

models/issues/issue_update.go

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
access_model "code.gitea.io/gitea/models/perm/access"
1616
project_model "code.gitea.io/gitea/models/project"
1717
repo_model "code.gitea.io/gitea/models/repo"
18-
system_model "code.gitea.io/gitea/models/system"
1918
"code.gitea.io/gitea/models/unit"
2019
user_model "code.gitea.io/gitea/models/user"
2120
"code.gitea.io/gitea/modules/git"
@@ -817,36 +816,13 @@ func DeleteIssuesByRepoID(ctx context.Context, repoID int64) (attachmentPaths []
817816
return attachmentPaths, err
818817
}
819818

820-
// DeleteOrphanedIssues delete issues without a repo
821-
func DeleteOrphanedIssues(ctx context.Context) error {
822-
var attachmentPaths []string
823-
err := db.WithTx(ctx, func(ctx context.Context) error {
824-
var ids []int64
825-
826-
if err := db.GetEngine(ctx).Table("issue").Distinct("issue.repo_id").
827-
Join("LEFT", "repository", "issue.repo_id=repository.id").
828-
Where(builder.IsNull{"repository.id"}).GroupBy("issue.repo_id").
829-
Find(&ids); err != nil {
830-
return err
831-
}
832-
833-
for i := range ids {
834-
paths, err := DeleteIssuesByRepoID(ctx, ids[i])
835-
if err != nil {
836-
return err
837-
}
838-
attachmentPaths = append(attachmentPaths, paths...)
839-
}
840-
841-
return nil
842-
})
843-
if err != nil {
844-
return err
845-
}
846-
847-
// Remove issue attachment files.
848-
for i := range attachmentPaths {
849-
system_model.RemoveAllWithNotice(ctx, "Delete issue attachment", attachmentPaths[i])
819+
func GetOrphanedIssueRepoIDs(ctx context.Context) ([]int64, error) {
820+
var repoIDs []int64
821+
if err := db.GetEngine(ctx).Table("issue").Distinct("issue.repo_id").
822+
Join("LEFT", "repository", "issue.repo_id=repository.id").
823+
Where(builder.IsNull{"repository.id"}).GroupBy("issue.repo_id").
824+
Find(&repoIDs); err != nil {
825+
return nil, err
850826
}
851-
return nil
827+
return repoIDs, nil
852828
}

services/doctor/dbconsistency.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
secret_model "code.gitea.io/gitea/models/secret"
1616
"code.gitea.io/gitea/modules/log"
1717
"code.gitea.io/gitea/modules/setting"
18+
issue_service "code.gitea.io/gitea/services/issue"
1819
)
1920

2021
type consistencyCheck struct {
@@ -93,7 +94,7 @@ func prepareDBConsistencyChecks() []consistencyCheck {
9394
// find issues without existing repository
9495
Name: "Orphaned Issues without existing repository",
9596
Counter: issues_model.CountOrphanedIssues,
96-
Fixer: asFixer(issues_model.DeleteOrphanedIssues),
97+
Fixer: asFixer(issue_service.DeleteOrphanedIssues),
9798
},
9899
// find releases without existing repository
99100
genericOrphanCheck("Orphaned Releases without existing repository",

services/issue/issue.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,32 @@ func deleteIssue(ctx context.Context, issue *issues_model.Issue) error {
323323

324324
return committer.Commit()
325325
}
326+
327+
// DeleteOrphanedIssues delete issues without a repo
328+
func DeleteOrphanedIssues(ctx context.Context) error {
329+
var attachmentPaths []string
330+
err := db.WithTx(ctx, func(ctx context.Context) error {
331+
repoIDs, err := issues_model.GetOrphanedIssueRepoIDs(ctx)
332+
if err != nil {
333+
return err
334+
}
335+
for i := range repoIDs {
336+
paths, err := issues_model.DeleteIssuesByRepoID(ctx, repoIDs[i])
337+
if err != nil {
338+
return err
339+
}
340+
attachmentPaths = append(attachmentPaths, paths...)
341+
}
342+
343+
return nil
344+
})
345+
if err != nil {
346+
return err
347+
}
348+
349+
// Remove issue attachment files.
350+
for i := range attachmentPaths {
351+
system_model.RemoveStorageWithNotice(ctx, storage.Attachments, "Delete issue attachment", attachmentPaths[i])
352+
}
353+
return nil
354+
}

0 commit comments

Comments
 (0)