diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2019-09-24 21:22:39 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-24 21:22:39 +0800 |
commit | 29dda47cbbb6da148dbd3412463dc979deffcbfa (patch) | |
tree | fc627eb70613381641235684b342e072d1aeb0eb /models | |
parent | 7cccada51eb925e7acfc90aae26661afd847d8f4 (diff) | |
download | gitea-29dda47cbbb6da148dbd3412463dc979deffcbfa.tar.gz gitea-29dda47cbbb6da148dbd3412463dc979deffcbfa.zip |
Fix label count (#8267)
* fix label count
* fix vendor
* fix import order
* update xorm to fix bug
* fix tests
* fix mssql bug
Diffstat (limited to 'models')
-rw-r--r-- | models/issue.go | 5 | ||||
-rw-r--r-- | models/issue_label.go | 24 | ||||
-rw-r--r-- | models/issue_label_test.go | 1 |
3 files changed, 16 insertions, 14 deletions
diff --git a/models/issue.go b/models/issue.go index c4b15e9ddf..77712c0fec 100644 --- a/models/issue.go +++ b/models/issue.go @@ -760,11 +760,6 @@ func (issue *Issue) changeStatus(e *xorm.Session, doer *User, isClosed bool) (er return err } for idx := range issue.Labels { - if issue.IsClosed { - issue.Labels[idx].NumClosedIssues++ - } else { - issue.Labels[idx].NumClosedIssues-- - } if err = updateLabel(e, issue.Labels[idx]); err != nil { return err } diff --git a/models/issue_label.go b/models/issue_label.go index f378f62e65..dab5ba2827 100644 --- a/models/issue_label.go +++ b/models/issue_label.go @@ -14,6 +14,7 @@ import ( api "code.gitea.io/gitea/modules/structs" "github.com/go-xorm/xorm" + "xorm.io/builder" ) var labelColorPattern = regexp.MustCompile("#([a-fA-F0-9]{6})") @@ -294,7 +295,20 @@ func GetLabelsByIssueID(issueID int64) ([]*Label, error) { } func updateLabel(e Engine, l *Label) error { - _, err := e.ID(l.ID).AllCols().Update(l) + _, err := e.ID(l.ID). + SetExpr("num_issues", + builder.Select("count(*)").From("issue_label"). + Where(builder.Eq{"label_id": l.ID}), + ). + SetExpr("num_closed_issues", + builder.Select("count(*)").From("issue_label"). + InnerJoin("issue", "issue_label.issue_id = issue.id"). + Where(builder.Eq{ + "issue_label.label_id": l.ID, + "issue.is_closed": true, + }), + ). + AllCols().Update(l) return err } @@ -375,10 +389,6 @@ func newIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (err return err } - label.NumIssues++ - if issue.IsClosed { - label.NumClosedIssues++ - } return updateLabel(e, label) } @@ -448,10 +458,6 @@ func deleteIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) ( return err } - label.NumIssues-- - if issue.IsClosed { - label.NumClosedIssues-- - } return updateLabel(e, label) } diff --git a/models/issue_label_test.go b/models/issue_label_test.go index 5cdc059cff..3cf6cc0e57 100644 --- a/models/issue_label_test.go +++ b/models/issue_label_test.go @@ -205,6 +205,7 @@ func TestNewIssueLabel(t *testing.T) { LabelID: label.ID, Content: "1", }) + label = AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) assert.EqualValues(t, prevNumIssues+1, label.NumIssues) // re-add existing IssueLabel |