summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
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