Skip to content

feat(api): add sort and order query parameters to job list endpoints#37672

Merged
silverwind merged 9 commits into
go-gitea:mainfrom
mtschoen:feat/action-jobs-sort-parameter
May 13, 2026
Merged

feat(api): add sort and order query parameters to job list endpoints#37672
silverwind merged 9 commits into
go-gitea:mainfrom
mtschoen:feat/action-jobs-sort-parameter

Conversation

@mtschoen
Copy link
Copy Markdown
Contributor

Summary

Adds sort and order query parameters to all action job list API endpoints (/admin/actions/jobs, /repos/{owner}/{repo}/actions/jobs, /repos/{owner}/{repo}/actions/runs/{run}/jobs, /user/actions/jobs), following the existing OrderByMap pattern used by repo/user search endpoints.

  • Default is id / asc (backwards compatible — matches previous DB natural order)
  • Only id sort field for now; the map is extensible for future fields
  • Returns 422 for invalid sort/order values
  • ToOrders() returns empty string when OrderBy is unset, so internal callers (webhook dispatch, concurrency checks) are unaffected

Closes #37666. Supersedes #37667, which was closed because hardcoding DESC ordering broke webhook dispatch by changing the order for all internal callers.

Test plan

  • Added JobsDefaultOrderAsc integration test — verifies default ordering is ascending by ID
  • Added JobsOrderedByIDDesc integration test — verifies sort=id&order=desc returns descending order
  • Full integration suite: 359 pass, 1 fail (TestAPIDownloadArchive — pre-existing, unrelated)
  • Zero regressions from baseline (same 359/1 split on main without changes)

Co-Authored-By: Claude Opus 4.6 (1M context) noreply@anthropic.com

Adds `sort` and `order` query parameters to all action job list API
endpoints (`/user/actions/jobs`, `/repos/{owner}/{repo}/actions/jobs`,
`/repos/{owner}/{repo}/actions/runs/{run}/jobs`, `/admin/actions/jobs`).
Follows the existing OrderByMap pattern used by repo/user search.

Only `id` is supported as a sort field initially. Default is `id` ASC
for backwards compatibility. Internal callers (webhook dispatch,
concurrency checks) are unaffected — they never set OrderBy so
ToOrders() returns empty string and DB uses natural insertion order.

Fixes go-gitea#37666
Closes go-gitea#37667

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label May 12, 2026
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8399660dd3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread routers/api/v1/shared/action.go Outdated
mtschoen and others added 4 commits May 12, 2026 00:38
The unqualified `id` in ORDER BY was ambiguous when the query joins
`repository` (user/org job endpoints), causing 500 errors on
MySQL/MSSQL/PostgreSQL.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The previous fix only qualified the column in JobOrderByMap (used when
sort/order params are provided). The default OrderBy still used the
unqualified db.SearchOrderByID, which also fails on MySQL/MSSQL/PgSQL
when the user/org endpoints join with repository.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Declare 422 response on all four job-list swagger blocks; the code
  already returns it for invalid sort/order but the spec only listed
  400 and 404.
- Drop the unreachable empty-OrderBy branch in FindRunJobOptions.ToOrders;
  string("") is "", so the conditional was a no-op.
- Add integration tests asserting 422 for sort=bogus and order=bogus on
  /api/v1/user/actions/jobs.
- Regenerate swagger templates.

Co-Authored-By: silverwind <me@silverwind.io>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…epos

The sort/order validation block was copy-pasted across three handlers
(shared/action.go, admin/user.go, repo/repo.go) with identical control
flow — each ~17 lines of nested if/else writing the same 422 responses.

Hoist it into routers/api/v1/utils.ResolveSortOrder: takes the OrderByMap
and a default, returns the resolved SearchOrderBy or writes 422 and
signals failure via ok=false. Each call site drops from ~17 to 4 lines
with identical behavior.

Co-Authored-By: silverwind <me@silverwind.io>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels May 13, 2026
The two JobsInvalidSort/JobsInvalidOrder integration tests exercised
generic ResolveSortOrder validation through a full HTTP round-trip on
one of three call sites. Drop them and add a small unit test directly
on the helper — one location covers the behavior for all callers.

Co-Authored-By: silverwind <me@silverwind.io>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds API-level sorting support (sort + order query parameters) to Actions job list endpoints by introducing a shared sort-resolution helper and plumbing an explicit ORDER BY into the job query options. It also updates Swagger specs and adds integration coverage for default/descending job ordering.

Changes:

  • Add ResolveSortOrder helper to consistently parse/validate sort and order query params and return 422 on invalid values.
  • Introduce FindRunJobOptions.OrderBy + ToOrders() and a JobOrderByMap to enable explicit ordering for Action job listings.
  • Update Swagger (v1 + OpenAPI3) and add integration tests validating default (ASC) and sort=id&order=desc behavior.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/integration/api_actions_run_test.go Adds integration tests for default ASC ordering and explicit DESC ordering on /user/actions/jobs.
templates/swagger/v1_openapi3_json.tmpl Documents sort/order query params and 422 response for job list endpoints (OpenAPI3).
templates/swagger/v1_json.tmpl Documents sort/order query params and 422 response for job list endpoints (Swagger v1).
routers/api/v1/utils/sort.go Adds shared ResolveSortOrder helper for API query sorting validation.
routers/api/v1/utils/sort_test.go Adds a unit test for invalid sort/order inputs (error path).
routers/api/v1/user/action.go Updates endpoint docs to include sort/order and 422 response.
routers/api/v1/shared/action.go Applies resolved OrderBy to job list queries via FindRunJobOptions.
routers/api/v1/repo/repo.go Refactors repo search sort/order parsing to use ResolveSortOrder.
routers/api/v1/repo/action.go Updates repo job endpoints docs to include sort/order and 422 response.
routers/api/v1/admin/user.go Refactors admin user search sort/order parsing to use ResolveSortOrder.
routers/api/v1/admin/action.go Updates admin job endpoint docs to include sort/order and 422 response.
models/actions/run_job_list.go Adds OrderBy + ToOrders() and defines JobOrderByMap for action job queries.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread routers/api/v1/utils/sort_test.go Outdated
Address review feedback: previously only the error paths were
exercised. Add assertions for default-when-absent, valid sort with
default order, and valid sort with explicit desc — with distinct
asc/desc values so a swapped lookup would be caught.

Co-Authored-By: silverwind <me@silverwind.io>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@lunny lunny added the type/enhancement An improvement of existing functionality label May 13, 2026
@lunny lunny added this to the 1.27.0 milestone May 13, 2026
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels May 13, 2026
@silverwind silverwind enabled auto-merge (squash) May 13, 2026 12:40
@silverwind silverwind added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label May 13, 2026
@silverwind silverwind merged commit a564f05 into go-gitea:main May 13, 2026
21 checks passed
@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label May 13, 2026
silverwind added a commit to silverwind/gitea that referenced this pull request May 13, 2026
* origin/main:
  chore: clean up "contrib" dir (go-gitea#37690)
  feat(api): add sort and order query parameters to job list endpoints (go-gitea#37672)
  fix: Sort action run jobs by JobID and Name with matrix examples (go-gitea#37046)
  fix: catch and fix more lint problems (go-gitea#37674)
  docs(agents): update AGENTS.md (go-gitea#37684)
  fix(actions): run `TransferLogs` on `UpdateLog{Rows:[], NoMore:true}` (go-gitea#37631)

# Conflicts:
#	build/test-env-check.sh
#	contrib/update_dependencies.sh
@mtschoen mtschoen deleted the feat/action-jobs-sort-parameter branch May 13, 2026 19:26
silverwind added a commit to bircni/gitea that referenced this pull request May 14, 2026
…thor

* origin/main:
  [skip ci] Updated translations via Crowdin
  fix: snap build (main branch) (go-gitea#37685)
  chore: introduce HTMLBuilder (go-gitea#37688)
  chore: clean up "contrib" dir (go-gitea#37690)
  feat(api): add sort and order query parameters to job list endpoints (go-gitea#37672)
  fix: Sort action run jobs by JobID and Name with matrix examples (go-gitea#37046)
  fix: catch and fix more lint problems (go-gitea#37674)
  docs(agents): update AGENTS.md (go-gitea#37684)
  fix(actions): run `TransferLogs` on `UpdateLog{Rows:[], NoMore:true}` (go-gitea#37631)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/enhancement An improvement of existing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Action job API endpoints return oldest jobs first with no sort option

5 participants