Skip to content

Commit 70e4134

Browse files
authored
Delete Labels & IssueLabels on Repo Delete too (#15039) (#15051)
* Doctor: find IssueLabels without existing label * Repo Delete: delete labels & issue_labels too
1 parent 909f2be commit 70e4134

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

cmd/doctor.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,22 @@ func runDoctorCheckDBConsistency(ctx *cli.Context) ([]string, error) {
606606
}
607607
}
608608

609+
// find IssueLabels without existing label
610+
count, err = models.CountOrphanedIssueLabels()
611+
if err != nil {
612+
return nil, err
613+
}
614+
if count > 0 {
615+
if ctx.Bool("fix") {
616+
if err = models.DeleteOrphanedIssueLabels(); err != nil {
617+
return nil, err
618+
}
619+
results = append(results, fmt.Sprintf("%d issue_labels without existing label deleted", count))
620+
} else {
621+
results = append(results, fmt.Sprintf("%d issue_labels without existing label", count))
622+
}
623+
}
624+
609625
//find issues without existing repository
610626
count, err = models.CountOrphanedIssues()
611627
if err != nil {

models/consistency.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,24 @@ func DeleteOrphanedLabels() error {
224224
return nil
225225
}
226226

227+
// CountOrphanedIssueLabels return count of IssueLabels witch have no label behind anymore
228+
func CountOrphanedIssueLabels() (int64, error) {
229+
return x.Table("issue_label").
230+
Join("LEFT", "label", "issue_label.label_id = label.id").
231+
Where(builder.IsNull{"label.id"}).Count()
232+
}
233+
234+
// DeleteOrphanedIssueLabels delete IssueLabels witch have no label behind anymore
235+
func DeleteOrphanedIssueLabels() error {
236+
237+
_, err := x.In("id", builder.Select("issue_label.id").From("issue_label").
238+
Join("LEFT", "label", "issue_label.label_id = label.id").
239+
Where(builder.IsNull{"label.id"})).
240+
Delete(IssueLabel{})
241+
242+
return err
243+
}
244+
227245
// CountOrphanedIssues count issues without a repo
228246
func CountOrphanedIssues() (int64, error) {
229247
return x.Table("issue").

models/issue_label.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,3 +764,15 @@ func DeleteIssueLabel(issue *Issue, label *Label, doer *User) (err error) {
764764

765765
return sess.Commit()
766766
}
767+
768+
func deleteLabelsByRepoID(sess Engine, repoID int64) error {
769+
deleteCond := builder.Select("id").From("label").Where(builder.Eq{"label.repo_id": repoID})
770+
771+
if _, err := sess.In("label_id", deleteCond).
772+
Delete(&IssueLabel{}); err != nil {
773+
return err
774+
}
775+
776+
_, err := sess.Delete(&Label{RepoID: repoID})
777+
return err
778+
}

models/repo.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,10 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
17291729
return fmt.Errorf("deleteBeans: %v", err)
17301730
}
17311731

1732+
if err := deleteLabelsByRepoID(sess, repoID); err != nil {
1733+
return err
1734+
}
1735+
17321736
// Delete Issues and related objects
17331737
var attachmentPaths []string
17341738
if attachmentPaths, err = deleteIssuesByRepoID(sess, repoID); err != nil {

0 commit comments

Comments
 (0)