summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorJaime merino <cobak78@gmail.com>2025-01-14 12:17:42 +0100
committerEarl Warren <earl-warren@noreply.codeberg.org>2025-01-14 12:17:42 +0100
commit9f842f0dec6a20edcaae47d91b78028e52ad24f3 (patch)
treeeea35a92387c585adb3e4f99ee59891850871a27 /models
parentUpdate https://data.forgejo.org/infrastructure/issue-action action to v1.3.0 ... (diff)
downloadforgejo-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 'models')
-rw-r--r--models/actions/run_job.go10
-rw-r--r--models/actions/run_job_test.go29
-rw-r--r--models/actions/task.go17
-rw-r--r--models/actions/task_list.go6
-rw-r--r--models/fixtures/action_run_job.yml45
5 files changed, 88 insertions, 19 deletions
diff --git a/models/actions/run_job.go b/models/actions/run_job.go
index de4b6aab66..9f8edfe4fc 100644
--- a/models/actions/run_job.go
+++ b/models/actions/run_job.go
@@ -10,6 +10,7 @@ import (
"time"
"code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
@@ -71,6 +72,15 @@ func (job *ActionRunJob) LoadAttributes(ctx context.Context) error {
return job.Run.LoadAttributes(ctx)
}
+func (job *ActionRunJob) ItRunsOn(labels []string) bool {
+ if len(labels) == 0 || len(job.RunsOn) == 0 {
+ return false
+ }
+ labelSet := make(container.Set[string])
+ labelSet.AddMultiple(labels...)
+ return labelSet.IsSubset(job.RunsOn)
+}
+
func GetRunJobByID(ctx context.Context, id int64) (*ActionRunJob, error) {
var job ActionRunJob
has, err := db.GetEngine(ctx).Where("id=?", id).Get(&job)
diff --git a/models/actions/run_job_test.go b/models/actions/run_job_test.go
new file mode 100644
index 0000000000..50a4ba10d8
--- /dev/null
+++ b/models/actions/run_job_test.go
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: MIT
+
+package actions
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestActionRunJob_ItRunsOn(t *testing.T) {
+ actionJob := ActionRunJob{RunsOn: []string{"ubuntu"}}
+ agentLabels := []string{"ubuntu", "node-20"}
+
+ assert.True(t, actionJob.ItRunsOn(agentLabels))
+ assert.False(t, actionJob.ItRunsOn([]string{}))
+
+ actionJob.RunsOn = append(actionJob.RunsOn, "node-20")
+
+ assert.True(t, actionJob.ItRunsOn(agentLabels))
+
+ agentLabels = []string{"ubuntu"}
+
+ assert.False(t, actionJob.ItRunsOn(agentLabels))
+
+ actionJob.RunsOn = []string{}
+
+ assert.False(t, actionJob.ItRunsOn(agentLabels))
+}
diff --git a/models/actions/task.go b/models/actions/task.go
index 8bd139a2d6..31655d2f1d 100644
--- a/models/actions/task.go
+++ b/models/actions/task.go
@@ -12,7 +12,6 @@ import (
auth_model "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
- "code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
@@ -245,7 +244,7 @@ func CreateTaskForRunner(ctx context.Context, runner *ActionRunner) (*ActionTask
var job *ActionRunJob
log.Trace("runner labels: %v", runner.AgentLabels)
for _, v := range jobs {
- if isSubset(runner.AgentLabels, v.RunsOn) {
+ if v.ItRunsOn(runner.AgentLabels) {
job = v
break
}
@@ -482,20 +481,6 @@ func FindOldTasksToExpire(ctx context.Context, olderThan timeutil.TimeStamp, lim
Find(&tasks)
}
-func isSubset(set, subset []string) bool {
- m := make(container.Set[string], len(set))
- for _, v := range set {
- m.Add(v)
- }
-
- for _, v := range subset {
- if !m.Contains(v) {
- return false
- }
- }
- return true
-}
-
func convertTimestamp(timestamp *timestamppb.Timestamp) timeutil.TimeStamp {
if timestamp.GetSeconds() == 0 && timestamp.GetNanos() == 0 {
return timeutil.TimeStamp(0)
diff --git a/models/actions/task_list.go b/models/actions/task_list.go
index df4b43c5ef..502d29e1a3 100644
--- a/models/actions/task_list.go
+++ b/models/actions/task_list.go
@@ -50,7 +50,7 @@ type FindTaskOptions struct {
RepoID int64
OwnerID int64
CommitSHA string
- Status Status
+ Status []Status
UpdatedBefore timeutil.TimeStamp
StartedBefore timeutil.TimeStamp
RunnerID int64
@@ -67,8 +67,8 @@ func (opts FindTaskOptions) ToConds() builder.Cond {
if opts.CommitSHA != "" {
cond = cond.And(builder.Eq{"commit_sha": opts.CommitSHA})
}
- if opts.Status > StatusUnknown {
- cond = cond.And(builder.Eq{"status": opts.Status})
+ if opts.Status != nil {
+ cond = cond.And(builder.In("status", opts.Status))
}
if opts.UpdatedBefore > 0 {
cond = cond.And(builder.Lt{"updated": opts.UpdatedBefore})
diff --git a/models/fixtures/action_run_job.yml b/models/fixtures/action_run_job.yml
index 117bb5ea05..702c6bc832 100644
--- a/models/fixtures/action_run_job.yml
+++ b/models/fixtures/action_run_job.yml
@@ -83,3 +83,48 @@
status: 1
started: 1683636528
stopped: 1683636626
+-
+ id: 393
+ run_id: 891
+ repo_id: 1
+ owner_id: 1
+ commit_sha: 985f0301dba5e7b34be866819cd15ad3d8f508ee
+ is_fork_pull_request: 0
+ name: job_2
+ attempt: 1
+ job_id: job_2
+ task_id: 47
+ status: 5
+ runs_on: '["ubuntu-latest"]'
+ started: 1683636528
+ stopped: 1683636626
+-
+ id: 394
+ run_id: 891
+ repo_id: 1
+ owner_id: 2
+ commit_sha: 985f0301dba5e7b34be866819cd15ad3d8f508ee
+ is_fork_pull_request: 0
+ name: job_2
+ attempt: 1
+ job_id: job_2
+ task_id: 47
+ status: 5
+ runs_on: '["debian-latest"]'
+ started: 1683636528
+ stopped: 1683636626
+-
+ id: 395
+ run_id: 891
+ repo_id: 1
+ owner_id: 3
+ commit_sha: 985f0301dba5e7b34be866819cd15ad3d8f508ee
+ is_fork_pull_request: 0
+ name: job_2
+ attempt: 1
+ job_id: job_2
+ task_id: 47
+ status: 5
+ runs_on: '["fedora"]'
+ started: 1683636528
+ stopped: 1683636626