Skip to content

Commit 6b65168

Browse files
committed
Some improvements
1 parent 25f7cce commit 6b65168

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

models/issues/issue.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
user_model "code.gitea.io/gitea/models/user"
1818
"code.gitea.io/gitea/modules/container"
1919
"code.gitea.io/gitea/modules/log"
20+
"code.gitea.io/gitea/modules/setting"
2021
api "code.gitea.io/gitea/modules/structs"
2122
"code.gitea.io/gitea/modules/timeutil"
2223
"code.gitea.io/gitea/modules/util"
@@ -95,8 +96,7 @@ type Issue struct {
9596
// TODO: RemoveIssueRef: see "repo/issue/branch_selector_field.tmpl"
9697
Ref string
9798

98-
PinOrder int `xorm:"-"`
99-
pinOrderLoaded bool `xorm:"-"`
99+
PinOrder int `xorm:"-"` // 0 means not loaded, -1 means loaded but not pinned
100100

101101
DeadlineUnix timeutil.TimeStamp `xorm:"INDEX"`
102102

@@ -291,16 +291,18 @@ func (issue *Issue) LoadMilestone(ctx context.Context) (err error) {
291291
}
292292

293293
func (issue *Issue) LoadPinOrder(ctx context.Context) error {
294-
if issue.pinOrderLoaded || issue.PinOrder > 0 {
294+
if issue.PinOrder != 0 {
295295
return nil
296296
}
297297
issuePin, err := GetIssuePin(ctx, issue)
298298
if err != nil && !db.IsErrNotExist(err) {
299299
return err
300300
}
301-
issue.pinOrderLoaded = true
301+
302302
if issuePin != nil {
303303
issue.PinOrder = issuePin.PinOrder
304+
} else {
305+
issue.PinOrder = -1
304306
}
305307
return nil
306308
}
@@ -362,7 +364,10 @@ func (issue *Issue) LoadAttributes(ctx context.Context) (err error) {
362364

363365
// IsPinned returns if a Issue is pinned
364366
func (issue *Issue) IsPinned() bool {
365-
return issue.PinOrder != 0
367+
if issue.PinOrder == 0 && (!setting.IsProd || setting.IsInTesting) {
368+
log.Fatal("issue's pinorder has not been loaded")
369+
}
370+
return issue.PinOrder > 0
366371
}
367372

368373
func (issue *Issue) ResetAttributesLoaded() {

models/issues/issue_list.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ func (issues IssueList) LoadPinOrder(ctx context.Context) error {
512512
}
513513

514514
issueIDs := container.FilterSlice(issues, func(issue *Issue) (int64, bool) {
515-
return issue.ID, issue.PinOrder == 0 && !issue.pinOrderLoaded
515+
return issue.ID, issue.PinOrder == 0
516516
})
517517
if len(issueIDs) == 0 {
518518
return nil
@@ -523,7 +523,7 @@ func (issues IssueList) LoadPinOrder(ctx context.Context) error {
523523
}
524524

525525
for _, issue := range issues {
526-
if issue.PinOrder > 0 || issue.pinOrderLoaded {
526+
if issue.PinOrder != 0 {
527527
continue
528528
}
529529
for _, pin := range issuePins {
@@ -532,6 +532,9 @@ func (issues IssueList) LoadPinOrder(ctx context.Context) error {
532532
break
533533
}
534534
}
535+
if issue.PinOrder == 0 {
536+
issue.PinOrder = -1
537+
}
535538
}
536539
return nil
537540
}

models/issues/issue_pin.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ func GetPinnedIssues(ctx context.Context, repoID int64, isPull bool) (IssueList,
208208
break
209209
}
210210
}
211+
if (!setting.IsProd || setting.IsInTesting) && issue.PinOrder == 0 {
212+
panic("It should not happen that a pinned Issue has no PinOrder")
213+
}
211214
}
212215
sort.Slice(issues, func(i, j int) bool {
213216
return issues[i].PinOrder < issues[j].PinOrder

routers/web/repo/issue_pin.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ func IssuePinOrUnpin(ctx *context.Context) {
4343
}
4444

4545
if err != nil {
46-
ctx.Status(http.StatusInternalServerError)
47-
log.Error(err.Error())
46+
if issues_model.IsErrIssueMaxPinReached(err) {
47+
ctx.JSONError(ctx.Tr("repo.issues.max_pinned"))
48+
} else {
49+
ctx.Status(http.StatusInternalServerError)
50+
log.Error(err.Error())
51+
}
4852
return
4953
}
5054

0 commit comments

Comments
 (0)