Skip to content

Commit 3c5116e

Browse files
committed
Add doctor for wrong label and issue_label data
1 parent b9d611e commit 3c5116e

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

models/issue_label.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,3 +779,36 @@ func deleteLabelsByRepoID(sess Engine, repoID int64) error {
779779
_, err := sess.Delete(&Label{RepoID: repoID})
780780
return err
781781
}
782+
783+
// CountWrongLabels counts wrong data in issue_label and label
784+
func CountWrongLabels() (int64, int64, error) {
785+
wrongLabels, err := x.Where("repo_id > 0 and repo_id not in (select id from repository)").Count(new(Label))
786+
if err != nil {
787+
return 0, 0, err
788+
}
789+
790+
wrongIssueLabels, err := x.Where("label_id IN (select id from label where repo_id > 0 and repo_id not in (select id from repository))").Count(new(IssueLabel))
791+
if err != nil {
792+
return 0, 0, err
793+
}
794+
795+
return wrongLabels, wrongIssueLabels, nil
796+
}
797+
798+
// FixWrongLabels fix wrong data in label
799+
func FixWrongLabels() (int64, error) {
800+
res, err := x.Exec("DELETE FROM label WHERE repo_id > 0 and repo_id not in (select id from repository)")
801+
if err != nil {
802+
return 0, err
803+
}
804+
return res.RowsAffected()
805+
}
806+
807+
// FixWrongIssueLables fix wrong label data
808+
func FixWrongIssueLables() (int64, error) {
809+
res, err := x.Exec("DELETE FROM issue_label WHERE label_id in (select id FROM label WHERE repo_id > 0 and repo_id not in (select id from repository))")
810+
if err != nil {
811+
return 0, err
812+
}
813+
return res.RowsAffected()
814+
}

modules/doctor/label.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package doctor
6+
7+
import (
8+
"code.gitea.io/gitea/models"
9+
"code.gitea.io/gitea/modules/log"
10+
)
11+
12+
func checkLabels(logger log.Logger, autofix bool) error {
13+
lablesCnt, issueLabelsCnt, err := models.CountWrongLabels()
14+
if err != nil {
15+
logger.Critical("Error: %v whilst counting wrong labels and issue labels")
16+
return err
17+
}
18+
var count int64
19+
if issueLabelsCnt > 0 { // We should check issue_label before label
20+
if autofix {
21+
if count, err = models.FixWrongIssueLables(); err != nil {
22+
logger.Critical("Error: %v whilst fixing wrong issue labels")
23+
return err
24+
}
25+
logger.Info("%d issue labels missing repositories fixed", count)
26+
} else {
27+
logger.Warn("%d issue labels missing repositories exist", issueLabelsCnt)
28+
}
29+
}
30+
if lablesCnt > 0 {
31+
if autofix {
32+
if count, err = models.FixWrongLabels(); err != nil {
33+
logger.Critical("Error: %v whilst fixing wrong labels")
34+
return err
35+
}
36+
logger.Info("%d labels missing repositories fixed", count)
37+
} else {
38+
logger.Warn("%d labels missing repositories exist", lablesCnt)
39+
}
40+
}
41+
return nil
42+
}
43+
44+
func init() {
45+
Register(&Check{
46+
Title: "Check wrong labels and issue_labels where repositories deleted",
47+
Name: "labels",
48+
IsDefault: true,
49+
Run: checkLabels,
50+
AbortIfFailed: true,
51+
SkipDatabaseInitialization: false,
52+
Priority: 1,
53+
})
54+
}

0 commit comments

Comments
 (0)