diff options
author | Jaime merino <cobak78@gmail.com> | 2025-01-14 12:17:42 +0100 |
---|---|---|
committer | Earl Warren <earl-warren@noreply.codeberg.org> | 2025-01-14 12:17:42 +0100 |
commit | 9f842f0dec6a20edcaae47d91b78028e52ad24f3 (patch) | |
tree | eea35a92387c585adb3e4f99ee59891850871a27 /routers/api/v1 | |
parent | Update https://data.forgejo.org/infrastructure/issue-action action to v1.3.0 ... (diff) | |
download | forgejo-9f842f0dec6a20edcaae47d91b78028e52ad24f3.tar.xz forgejo-9f842f0dec6a20edcaae47d91b78028e52ad24f3.zip |
Add search action jobs for API routes, repo, org and global level (#6300)
This PR wants to improve information of the tasks waiting to be executed on a global, organization, user and repository leve.
The main motivation is explained here https://codeberg.org/forgejo/discussions/issues/241
## 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...
- [x] 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/<pull request number>.md` to be be used for the release notes instead of the title.
<!--start release-notes-assistant-->
## Release notes
<!--URL:https://codeberg.org/forgejo/forgejo-->
- Features
- [PR](https://codeberg.org/forgejo/forgejo/pulls/6300): <!--number 6300 --><!--line 0 --><!--description QWRkIHNlYXJjaCBhY3Rpb24gam9icyBmb3IgQVBJIHJvdXRlcywgcmVwbywgb3JnIGFuZCBnbG9iYWwgbGV2ZWw=-->Add search action jobs for API routes, repo, org and global level<!--description-->
<!--end release-notes-assistant-->
Co-authored-by: jaime merino <jaime.merino_mora@mail.schwarzª>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6300
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Jaime merino <cobak78@gmail.com>
Co-committed-by: Jaime merino <cobak78@gmail.com>
Diffstat (limited to 'routers/api/v1')
-rw-r--r-- | routers/api/v1/admin/runners.go | 20 | ||||
-rw-r--r-- | routers/api/v1/api.go | 3 | ||||
-rw-r--r-- | routers/api/v1/org/action.go | 25 | ||||
-rw-r--r-- | routers/api/v1/repo/action.go | 30 | ||||
-rw-r--r-- | routers/api/v1/shared/runners.go | 48 | ||||
-rw-r--r-- | routers/api/v1/user/runners.go | 22 |
6 files changed, 148 insertions, 0 deletions
diff --git a/routers/api/v1/admin/runners.go b/routers/api/v1/admin/runners.go index 329242d9f6..d0cfef8e48 100644 --- a/routers/api/v1/admin/runners.go +++ b/routers/api/v1/admin/runners.go @@ -24,3 +24,23 @@ func GetRegistrationToken(ctx *context.APIContext) { shared.GetRegistrationToken(ctx, 0, 0) } + +// SearchActionRunJobs return a list of actions jobs filtered by the provided parameters +func SearchActionRunJobs(ctx *context.APIContext) { + // swagger:operation GET /admin/runners/jobs admin adminSearchRunJobs + // --- + // summary: Search action jobs according filter conditions + // produces: + // - application/json + // parameters: + // - name: labels + // in: query + // description: a comma separated list of run job labels to search for + // type: string + // responses: + // "200": + // "$ref": "#/responses/RunJobList" + // "403": + // "$ref": "#/responses/forbidden" + shared.GetActionRunJobs(ctx, 0, 0) +} diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 4928c9ff58..89338d6977 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -822,6 +822,7 @@ func Routes() *web.Route { m.Group("/runners", func() { m.Get("/registration-token", reqToken(), reqChecker, act.GetRegistrationToken) + m.Get("/jobs", reqToken(), reqChecker, act.SearchActionRunJobs) }) }) } @@ -975,6 +976,7 @@ func Routes() *web.Route { m.Group("/runners", func() { m.Get("/registration-token", reqToken(), user.GetRegistrationToken) + m.Get("/jobs", reqToken(), user.SearchActionRunJobs) }) }) @@ -1631,6 +1633,7 @@ func Routes() *web.Route { }) m.Group("/runners", func() { m.Get("/registration-token", admin.GetRegistrationToken) + m.Get("/jobs", admin.SearchActionRunJobs) }) if setting.Quota.Enabled { m.Group("/quota", func() { diff --git a/routers/api/v1/org/action.go b/routers/api/v1/org/action.go index 03a1fa8ccc..8cd2e00e00 100644 --- a/routers/api/v1/org/action.go +++ b/routers/api/v1/org/action.go @@ -189,6 +189,31 @@ func (Action) GetRegistrationToken(ctx *context.APIContext) { shared.GetRegistrationToken(ctx, ctx.Org.Organization.ID, 0) } +// SearchActionRunJobs return a list of actions jobs filtered by the provided parameters +func (Action) SearchActionRunJobs(ctx *context.APIContext) { + // swagger:operation GET /orgs/{org}/actions/runners/jobs organization orgSearchRunJobs + // --- + // summary: Search for organization's action jobs according filter conditions + // produces: + // - application/json + // parameters: + // - name: org + // in: path + // description: name of the organization + // type: string + // required: true + // - name: labels + // in: query + // description: a comma separated list of run job labels to search for + // type: string + // responses: + // "200": + // "$ref": "#/responses/RunJobList" + // "403": + // "$ref": "#/responses/forbidden" + shared.GetActionRunJobs(ctx, ctx.Org.Organization.ID, 0) +} + // ListVariables list org-level variables func (Action) ListVariables(ctx *context.APIContext) { // swagger:operation GET /orgs/{org}/actions/variables organization getOrgVariablesList diff --git a/routers/api/v1/repo/action.go b/routers/api/v1/repo/action.go index 0c7506b13b..2ff52c3744 100644 --- a/routers/api/v1/repo/action.go +++ b/routers/api/v1/repo/action.go @@ -507,6 +507,36 @@ func (Action) GetRegistrationToken(ctx *context.APIContext) { shared.GetRegistrationToken(ctx, 0, ctx.Repo.Repository.ID) } +// SearchActionRunJobs return a list of actions jobs filtered by the provided parameters +func (Action) SearchActionRunJobs(ctx *context.APIContext) { + // swagger:operation GET /repos/{owner}/{repo}/actions/runners/jobs repository repoSearchRunJobs + // --- + // summary: Search for repository's action jobs according filter conditions + // produces: + // - application/json + // parameters: + // - name: owner + // in: path + // description: owner of the repo + // type: string + // required: true + // - name: repo + // in: path + // description: name of the repo + // type: string + // required: true + // - name: labels + // in: query + // description: a comma separated list of run job labels to search for + // type: string + // responses: + // "200": + // "$ref": "#/responses/RunJobList" + // "403": + // "$ref": "#/responses/forbidden" + shared.GetActionRunJobs(ctx, 0, ctx.Repo.Repository.ID) +} + var _ actions_service.API = new(Action) // Action implements actions_service.API diff --git a/routers/api/v1/shared/runners.go b/routers/api/v1/shared/runners.go index f184786d7d..53761a07e9 100644 --- a/routers/api/v1/shared/runners.go +++ b/routers/api/v1/shared/runners.go @@ -6,8 +6,11 @@ package shared import ( "errors" "net/http" + "strings" actions_model "code.gitea.io/gitea/models/actions" + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/services/context" ) @@ -30,3 +33,48 @@ func GetRegistrationToken(ctx *context.APIContext, ownerID, repoID int64) { ctx.JSON(http.StatusOK, RegistrationToken{Token: token.Token}) } + +// RunJobList is a list of action run jobs +// swagger:response RunJobList +type RunJobList struct { + // in:body + Body []*structs.ActionRunJob `json:"body"` +} + +func GetActionRunJobs(ctx *context.APIContext, ownerID, repoID int64) { + labels := strings.Split(ctx.FormTrim("labels"), ",") + + total, err := db.Find[actions_model.ActionRunJob](ctx, &actions_model.FindTaskOptions{ + Status: []actions_model.Status{actions_model.StatusWaiting, actions_model.StatusRunning}, + OwnerID: ownerID, + RepoID: repoID, + }) + if err != nil { + ctx.Error(http.StatusInternalServerError, "CountWaitingActionRunJobs", err) + return + } + + res := new(RunJobList) + res.Body = fromRunJobModelToResponse(total, labels) + + ctx.JSON(http.StatusOK, res) +} + +func fromRunJobModelToResponse(job []*actions_model.ActionRunJob, labels []string) []*structs.ActionRunJob { + var res []*structs.ActionRunJob + for i := range job { + if job[i].ItRunsOn(labels) { + res = append(res, &structs.ActionRunJob{ + ID: job[i].ID, + RepoID: job[i].RepoID, + OwnerID: job[i].OwnerID, + Name: job[i].Name, + Needs: job[i].Needs, + RunsOn: job[i].RunsOn, + TaskID: job[i].TaskID, + Status: job[i].Status.String(), + }) + } + } + return res +} diff --git a/routers/api/v1/user/runners.go b/routers/api/v1/user/runners.go index dc4c187ffe..5e8cdbeb58 100644 --- a/routers/api/v1/user/runners.go +++ b/routers/api/v1/user/runners.go @@ -28,3 +28,25 @@ func GetRegistrationToken(ctx *context.APIContext) { shared.GetRegistrationToken(ctx, ctx.Doer.ID, 0) } + +// SearchActionRunJobs return a list of actions jobs filtered by the provided parameters +func SearchActionRunJobs(ctx *context.APIContext) { + // swagger:operation GET /user/actions/runners/jobs user userSearchRunJobs + // --- + // summary: Search for user's action jobs according filter conditions + // produces: + // - application/json + // parameters: + // - name: labels + // in: query + // description: a comma separated list of run job labels to search for + // type: string + // responses: + // "200": + // "$ref": "#/responses/RunJobList" + // "401": + // "$ref": "#/responses/unauthorized" + // "403": + // "$ref": "#/responses/forbidden" + shared.GetActionRunJobs(ctx, ctx.Doer.ID, 0) +} |