Skip to content

Commit 29dda47

Browse files
authored
Fix label count (#8267)
* fix label count * fix vendor * fix import order * update xorm to fix bug * fix tests * fix mssql bug
1 parent 7cccada commit 29dda47

38 files changed

+958
-579
lines changed

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ require (
4848
github.com/go-redis/redis v6.15.2+incompatible
4949
github.com/go-sql-driver/mysql v1.4.1
5050
github.com/go-swagger/go-swagger v0.20.1
51-
github.com/go-xorm/xorm v0.7.7-0.20190822154023-17592d96b35b
51+
github.com/go-xorm/xorm v0.7.8-0.20190924080535-59ed80ce1a67
5252
github.com/gobwas/glob v0.2.3
5353
github.com/gogits/chardet v0.0.0-20150115103509-2404f7772561
5454
github.com/gogs/cron v0.0.0-20171120032916-9f6c956d3e14
@@ -124,6 +124,6 @@ require (
124124
gopkg.in/testfixtures.v2 v2.5.0
125125
mvdan.cc/xurls/v2 v2.0.0
126126
strk.kbt.io/projects/go/libravatar v0.0.0-20160628055650-5eed7bff870a
127-
xorm.io/builder v0.3.5
127+
xorm.io/builder v0.3.6
128128
xorm.io/core v0.7.0
129129
)

go.sum

+6-2
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,11 @@ github.com/go-swagger/scan-repo-boundary v0.0.0-20180623220736-973b3573c013/go.m
250250
github.com/go-xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a h1:9wScpmSP5A3Bk8V3XHWUcJmYTh+ZnlHVyc+A4oZYS3Y=
251251
github.com/go-xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a/go.mod h1:56xuuqnHyryaerycW3BfssRdxQstACi0Epw/yC5E2xM=
252252
github.com/go-xorm/xorm v0.7.6/go.mod h1:nqz2TAsuOHWH2yk4FYWtacCGgdbrcdZ5mF1XadqEHls=
253-
github.com/go-xorm/xorm v0.7.7-0.20190822154023-17592d96b35b h1:Y0hWUheXDHpIs7BWtJcykO4d1VOsVDKg1PsP5YJwxxM=
254-
github.com/go-xorm/xorm v0.7.7-0.20190822154023-17592d96b35b/go.mod h1:nqz2TAsuOHWH2yk4FYWtacCGgdbrcdZ5mF1XadqEHls=
253+
github.com/go-xorm/xorm v0.7.8-0.20190924080535-59ed80ce1a67 h1:mB5RWONyATkQ48+iQZ1lCZNPG3tABilyaEOxDm1QWyU=
254+
github.com/go-xorm/xorm v0.7.8-0.20190924080535-59ed80ce1a67/go.mod h1:RSsmsVARCy4sayuKWFPaVNQMPYGLNRIK71YIVvgImL0=
255255
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
256256
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
257+
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
257258
github.com/gogits/chardet v0.0.0-20150115103509-2404f7772561 h1:deE7ritpK04PgtpyVOS2TYcQEld9qLCD5b5EbVNOuLA=
258259
github.com/gogits/chardet v0.0.0-20150115103509-2404f7772561/go.mod h1:YgYOrVn3Nj9Tq0EvjmFbphRytDj7JNRoWSStJZWDJTQ=
259260
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
@@ -340,6 +341,7 @@ github.com/jackc/pgx v3.3.0+incompatible h1:Wa90/+qsITBAPkAZjiByeIGHFcj3Ztu+Vzrr
340341
github.com/jackc/pgx v3.3.0+incompatible/go.mod h1:0ZGrqGqkRlliWnWB4zKnWtjbSWbGkVEFm4TeybAXq+I=
341342
github.com/jackc/pgx v3.5.0+incompatible h1:BRJ4G3UPtvml5R1ey0biqqGuYUGayMYekm3woO75orY=
342343
github.com/jackc/pgx v3.5.0+incompatible/go.mod h1:0ZGrqGqkRlliWnWB4zKnWtjbSWbGkVEFm4TeybAXq+I=
344+
github.com/jackc/pgx v3.6.0+incompatible/go.mod h1:0ZGrqGqkRlliWnWB4zKnWtjbSWbGkVEFm4TeybAXq+I=
343345
github.com/jarcoal/httpmock v0.0.0-20180424175123-9c70cfe4a1da/go.mod h1:ks+b9deReOc7jgqp+e7LuFiCBH6Rm5hL32cLcEAArb4=
344346
github.com/jaytaylor/html2text v0.0.0-20160923191438-8fb95d837f7d h1:ig/iUfDDg06RVW8OMby+GrmW6K2nPO3AFHlEIdvJSd4=
345347
github.com/jaytaylor/html2text v0.0.0-20160923191438-8fb95d837f7d/go.mod h1:CVKlgaMiht+LXvHG173ujK6JUhZXKb2u/BQtjPDIvyk=
@@ -818,5 +820,7 @@ strk.kbt.io/projects/go/libravatar v0.0.0-20160628055650-5eed7bff870a h1:8q33Shx
818820
strk.kbt.io/projects/go/libravatar v0.0.0-20160628055650-5eed7bff870a/go.mod h1:FJGmPh3vz9jSos1L/F91iAgnC/aejc0wIIrF2ZwJxdY=
819821
xorm.io/builder v0.3.5 h1:EilU39fvWDxjb1cDaELpYhsF+zziRBhew8xk4pngO+A=
820822
xorm.io/builder v0.3.5/go.mod h1:ZFbByS/KxZI1FKRjL05PyJ4YrK2bcxlUaAxdum5aTR8=
823+
xorm.io/builder v0.3.6 h1:ha28mQ2M+TFx96Hxo+iq6tQgnkC9IZkM6D8w9sKHHF8=
824+
xorm.io/builder v0.3.6/go.mod h1:LEFAPISnRzG+zxaxj2vPicRwz67BdhFreKg8yv8/TgU=
821825
xorm.io/core v0.7.0 h1:hKxuOKWZNeiFQsSuGet/KV8HZ788hclvAl+7azx3tkM=
822826
xorm.io/core v0.7.0/go.mod h1:TuOJjIVa7e3w/rN8tDcAvuLBMtwzdHPbyOzE6Gk1EUI=

models/issue.go

-5
Original file line numberDiff line numberDiff line change
@@ -760,11 +760,6 @@ func (issue *Issue) changeStatus(e *xorm.Session, doer *User, isClosed bool) (er
760760
return err
761761
}
762762
for idx := range issue.Labels {
763-
if issue.IsClosed {
764-
issue.Labels[idx].NumClosedIssues++
765-
} else {
766-
issue.Labels[idx].NumClosedIssues--
767-
}
768763
if err = updateLabel(e, issue.Labels[idx]); err != nil {
769764
return err
770765
}

models/issue_label.go

+15-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
api "code.gitea.io/gitea/modules/structs"
1515

1616
"github.com/go-xorm/xorm"
17+
"xorm.io/builder"
1718
)
1819

1920
var labelColorPattern = regexp.MustCompile("#([a-fA-F0-9]{6})")
@@ -294,7 +295,20 @@ func GetLabelsByIssueID(issueID int64) ([]*Label, error) {
294295
}
295296

296297
func updateLabel(e Engine, l *Label) error {
297-
_, err := e.ID(l.ID).AllCols().Update(l)
298+
_, err := e.ID(l.ID).
299+
SetExpr("num_issues",
300+
builder.Select("count(*)").From("issue_label").
301+
Where(builder.Eq{"label_id": l.ID}),
302+
).
303+
SetExpr("num_closed_issues",
304+
builder.Select("count(*)").From("issue_label").
305+
InnerJoin("issue", "issue_label.issue_id = issue.id").
306+
Where(builder.Eq{
307+
"issue_label.label_id": l.ID,
308+
"issue.is_closed": true,
309+
}),
310+
).
311+
AllCols().Update(l)
298312
return err
299313
}
300314

@@ -375,10 +389,6 @@ func newIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (err
375389
return err
376390
}
377391

378-
label.NumIssues++
379-
if issue.IsClosed {
380-
label.NumClosedIssues++
381-
}
382392
return updateLabel(e, label)
383393
}
384394

@@ -448,10 +458,6 @@ func deleteIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (
448458
return err
449459
}
450460

451-
label.NumIssues--
452-
if issue.IsClosed {
453-
label.NumClosedIssues--
454-
}
455461
return updateLabel(e, label)
456462
}
457463

models/issue_label_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ func TestNewIssueLabel(t *testing.T) {
205205
LabelID: label.ID,
206206
Content: "1",
207207
})
208+
label = AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label)
208209
assert.EqualValues(t, prevNumIssues+1, label.NumIssues)
209210

210211
// re-add existing IssueLabel

0 commit comments

Comments
 (0)