From 8b79625feaf95df71c0d0e80f678a2b602da3a97 Mon Sep 17 00:00:00 2001 From: Gwyneth Morgan Date: Thu, 11 Jan 2024 03:07:15 +0000 Subject: [PATCH] Display both open and closed in issue list when state=all When URL parameter "state" is set to "all", list both open and closed issues on the same page. The "Open" and "Closed" filter buttons do not show as active in this case. This is not currently exposed in the frontend without manually editing the URL. --- models/issues/tracked_time.go | 11 ++++++---- models/issues/tracked_time_test.go | 9 ++++++-- routers/web/repo/issue.go | 33 ++++++++++++++++++++--------- templates/repo/issue/openclose.tmpl | 4 ++-- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/models/issues/tracked_time.go b/models/issues/tracked_time.go index 795bddeb347b8..c03c1f8fbf284 100644 --- a/models/issues/tracked_time.go +++ b/models/issues/tracked_time.go @@ -328,7 +328,7 @@ func GetTrackedTimeByID(ctx context.Context, id int64) (*TrackedTime, error) { } // GetIssueTotalTrackedTime returns the total tracked time for issues by given conditions. -func GetIssueTotalTrackedTime(ctx context.Context, opts *IssuesOptions, isClosed bool) (int64, error) { +func GetIssueTotalTrackedTime(ctx context.Context, opts *IssuesOptions, isClosed util.OptionalBool) (int64, error) { if len(opts.IssueIDs) <= MaxQueryParameters { return getIssueTotalTrackedTimeChunk(ctx, opts, isClosed, opts.IssueIDs) } @@ -351,7 +351,7 @@ func GetIssueTotalTrackedTime(ctx context.Context, opts *IssuesOptions, isClosed return accum, nil } -func getIssueTotalTrackedTimeChunk(ctx context.Context, opts *IssuesOptions, isClosed bool, issueIDs []int64) (int64, error) { +func getIssueTotalTrackedTimeChunk(ctx context.Context, opts *IssuesOptions, isClosed util.OptionalBool, issueIDs []int64) (int64, error) { sumSession := func(opts *IssuesOptions, issueIDs []int64) *xorm.Session { sess := db.GetEngine(ctx). Table("tracked_time"). @@ -365,7 +365,10 @@ func getIssueTotalTrackedTimeChunk(ctx context.Context, opts *IssuesOptions, isC Time int64 } - return sumSession(opts, issueIDs). - And("issue.is_closed = ?", isClosed). + session := sumSession(opts, issueIDs) + if !isClosed.IsNone() { + session = session.And("issue.is_closed = ?", isClosed.IsTrue()) + } + return session. SumInt(new(trackedTime), "tracked_time.time") } diff --git a/models/issues/tracked_time_test.go b/models/issues/tracked_time_test.go index 2774234e7bd33..9beb862ffbca2 100644 --- a/models/issues/tracked_time_test.go +++ b/models/issues/tracked_time_test.go @@ -11,6 +11,7 @@ import ( issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/util" "github.com/stretchr/testify/assert" ) @@ -119,11 +120,15 @@ func TestTotalTimesForEachUser(t *testing.T) { func TestGetIssueTotalTrackedTime(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - ttt, err := issues_model.GetIssueTotalTrackedTime(db.DefaultContext, &issues_model.IssuesOptions{MilestoneIDs: []int64{1}}, false) + ttt, err := issues_model.GetIssueTotalTrackedTime(db.DefaultContext, &issues_model.IssuesOptions{MilestoneIDs: []int64{1}}, util.OptionalBoolFalse) assert.NoError(t, err) assert.EqualValues(t, 3682, ttt) - ttt, err = issues_model.GetIssueTotalTrackedTime(db.DefaultContext, &issues_model.IssuesOptions{MilestoneIDs: []int64{1}}, true) + ttt, err = issues_model.GetIssueTotalTrackedTime(db.DefaultContext, &issues_model.IssuesOptions{MilestoneIDs: []int64{1}}, util.OptionalBoolTrue) assert.NoError(t, err) assert.EqualValues(t, 0, ttt) + + ttt, err = issues_model.GetIssueTotalTrackedTime(db.DefaultContext, &issues_model.IssuesOptions{MilestoneIDs: []int64{1}}, util.OptionalBoolNone) + assert.NoError(t, err) + assert.EqualValues(t, 3682, ttt) } diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 14781be822da0..95c13340ba6f3 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -237,10 +237,18 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption uti } } - isShowClosed := ctx.FormString("state") == "closed" - // if open issues are zero and close don't, use closed as default + var isShowClosed util.OptionalBool + switch ctx.FormString("state") { + case "closed": + isShowClosed = util.OptionalBoolTrue + case "all": + isShowClosed = util.OptionalBoolNone + default: + isShowClosed = util.OptionalBoolFalse + } + // if there are closed issues and no open issues, default to showing closed if len(ctx.FormString("state")) == 0 && issueStats.OpenCount == 0 && issueStats.ClosedCount != 0 { - isShowClosed = true + isShowClosed = util.OptionalBoolTrue } if repo.IsTimetrackerEnabled(ctx) { @@ -260,10 +268,13 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption uti } var total int - if !isShowClosed { - total = int(issueStats.OpenCount) - } else { + switch isShowClosed { + case util.OptionalBoolTrue: total = int(issueStats.ClosedCount) + case util.OptionalBoolNone: + total = int(issueStats.OpenCount + issueStats.ClosedCount) + default: + total = int(issueStats.OpenCount) } pager := context.NewPagination(total, setting.UI.IssuePagingNum, page, 5) @@ -282,7 +293,7 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption uti ReviewedID: reviewedID, MilestoneIDs: mileIDs, ProjectID: projectID, - IsClosed: util.OptionalBoolOf(isShowClosed), + IsClosed: isShowClosed, IsPull: isPullOption, LabelIDs: labelIDs, SortType: sortType, @@ -442,11 +453,13 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption uti ctx.Data["ProjectID"] = projectID ctx.Data["AssigneeID"] = assigneeID ctx.Data["PosterID"] = posterID - ctx.Data["IsShowClosed"] = isShowClosed ctx.Data["Keyword"] = keyword - if isShowClosed { + switch isShowClosed { + case util.OptionalBoolTrue: ctx.Data["State"] = "closed" - } else { + case util.OptionalBoolNone: + ctx.Data["State"] = "all" + default: ctx.Data["State"] = "open" } ctx.Data["ShowArchivedLabels"] = archived diff --git a/templates/repo/issue/openclose.tmpl b/templates/repo/issue/openclose.tmpl index ff5ec3c5a5424..cc10086c049e3 100644 --- a/templates/repo/issue/openclose.tmpl +++ b/templates/repo/issue/openclose.tmpl @@ -1,5 +1,5 @@