From 663acf102a00bf909b1eb2c2e95ec7ea698de786 Mon Sep 17 00:00:00 2001 From: Mathieu Fenniak Date: Fri, 26 Dec 2025 20:03:12 +0100 Subject: [PATCH] fix: ListTrackedTimes API has no defined record ordering (#10588) API call `GET /repos/{owner}/{repo}/issues/{index}/times` has no defined ordering implemented in it, causing PostgreSQL to have intermittent test failures on `TestAPIGetTrackedTimes` which expected records to be returned in ID order. ID order is reasonable enough, so this PR adds that ordering. Fixes #10577. ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [ ] in their respective `*_test.go` for unit tests. - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] I do not want this change to show in the release notes. - [x] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/.md` to be be used for the release notes instead of the title. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10588 Reviewed-by: Cyborus Co-authored-by: Mathieu Fenniak Co-committed-by: Mathieu Fenniak --- models/issues/tracked_time.go | 2 +- .../integration/api_issue_tracked_time_test.go | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/models/issues/tracked_time.go b/models/issues/tracked_time.go index d229d83470..e083f6e1e8 100644 --- a/models/issues/tracked_time.go +++ b/models/issues/tracked_time.go @@ -148,7 +148,7 @@ func (opts *FindTrackedTimesOptions) toSession(e db.Engine) db.Engine { // GetTrackedTimes returns all tracked times that fit to the given options. func GetTrackedTimes(ctx context.Context, options *FindTrackedTimesOptions) (trackedTimes TrackedTimeList, err error) { - err = options.toSession(db.GetEngine(ctx)).Find(&trackedTimes) + err = options.toSession(db.GetEngine(ctx)).Asc("tracked_time.id").Find(&trackedTimes) return trackedTimes, err } diff --git a/tests/integration/api_issue_tracked_time_test.go b/tests/integration/api_issue_tracked_time_test.go index a4f3d15f88..5e8b383437 100644 --- a/tests/integration/api_issue_tracked_time_test.go +++ b/tests/integration/api_issue_tracked_time_test.go @@ -61,8 +61,21 @@ func TestAPIGetTrackedTimes(t *testing.T) { var filterAPITimes api.TrackedTimeList DecodeJSON(t, resp, &filterAPITimes) assert.Len(t, filterAPITimes, 2) - assert.Equal(t, int64(3), filterAPITimes[0].ID) - assert.Equal(t, int64(6), filterAPITimes[1].ID) + assert.EqualValues(t, 3, filterAPITimes[0].ID) + assert.EqualValues(t, 6, filterAPITimes[1].ID) + + // test pagination + allIDs := []int64{} + for _, page := range []int{1, 2, 3} { + req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/%d/times?page=%d&limit=1", user2.Name, issue2.Repo.Name, issue2.Index, page). + AddTokenAuth(token) + resp = MakeRequest(t, req, http.StatusOK) + var pageAPITimes api.TrackedTimeList + DecodeJSON(t, resp, &pageAPITimes) + require.Len(t, pageAPITimes, 1) + allIDs = append(allIDs, pageAPITimes[0].ID) + } + assert.Equal(t, []int64{2, 3, 6}, allIDs) } func TestAPIDeleteTrackedTime(t *testing.T) {