diff --git a/modelmigration/migrations.go b/modelmigration/migrations.go index b83084ac810..1fc1de3ba29 100644 --- a/modelmigration/migrations.go +++ b/modelmigration/migrations.go @@ -445,6 +445,7 @@ func prepareMigrationTasks() []*migration { newMigration(366, "Drop user_open_id table", v29.DropUserOpenIDTable), newMigration(367, "Add latest_release_id to repository table", v29.AddLatestReleaseIDToRepository), newMigration(368, "Add unique index for team name", v29.AddUniqueTeamNameIndex), + newMigration(369, "Add runner_name to action_task", v29.AddRunnerNameToActionTask), } return preparedMigrations } diff --git a/modelmigration/v29/v369.go b/modelmigration/v29/v369.go new file mode 100644 index 00000000000..daf0039d652 --- /dev/null +++ b/modelmigration/v29/v369.go @@ -0,0 +1,35 @@ +// Copyright 2026 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v29 + +import ( + "context" + + "gitea.dev/modelmigration/base" + + "xorm.io/xorm" +) + +func AddRunnerNameToActionTask(_ context.Context, x base.EngineMigration) error { + type ActionTask struct { + RunnerName string `xorm:"VARCHAR(255) NOT NULL DEFAULT ''"` + } + + if _, err := x.SyncWithOptions(xorm.SyncOptions{ + IgnoreDropIndices: true, + IgnoreConstrains: true, + }, new(ActionTask)); err != nil { + return err + } + + _, err := x.Exec(` +UPDATE action_task SET runner_name = ( + SELECT action_runner.name FROM action_runner WHERE action_runner.id = action_task.runner_id +) +WHERE runner_name = '' + AND EXISTS ( + SELECT 1 FROM action_runner WHERE action_runner.id = action_task.runner_id AND action_runner.name IS NOT NULL + );`) // soft-deleted runners are included to keep their names + return err +} diff --git a/modelmigration/v29/v369_test.go b/modelmigration/v29/v369_test.go new file mode 100644 index 00000000000..593887a25bd --- /dev/null +++ b/modelmigration/v29/v369_test.go @@ -0,0 +1,47 @@ +// Copyright 2026 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v29 + +import ( + "testing" + + "gitea.dev/modelmigration/migrationtest" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestAddRunnerNameToActionTask(t *testing.T) { + type ActionTask struct { + ID int64 `xorm:"pk"` + RunnerID int64 `xorm:"index"` + } + type ActionRunner struct { + ID int64 `xorm:"pk"` + Name string `xorm:"VARCHAR(255)"` + Deleted int64 + } + + x, deferrable := migrationtest.PrepareTestEnv(t, 0, new(ActionTask), new(ActionRunner)) + defer deferrable() + if x == nil || t.Failed() { + return + } + + _, err := x.Insert( + &ActionRunner{ID: 1, Name: "live-runner"}, + &ActionRunner{ID: 2, Name: "deleted-runner", Deleted: 1}, + + &ActionTask{ID: 1, RunnerID: 1}, + &ActionTask{ID: 2, RunnerID: 2}, + &ActionTask{ID: 3, RunnerID: 3}, + &ActionTask{ID: 4, RunnerID: 0}, + ) + require.NoError(t, err) + require.NoError(t, AddRunnerNameToActionTask(t.Context(), x)) + + var names []string + require.NoError(t, x.Table("action_task").Cols("runner_name").Asc("id").Find(&names)) + assert.Equal(t, []string{"live-runner", "deleted-runner", "", ""}, names) +} diff --git a/models/actions/runner.go b/models/actions/runner.go index 1494061d898..4a9a1ac0b50 100644 --- a/models/actions/runner.go +++ b/models/actions/runner.go @@ -207,14 +207,6 @@ func init() { db.RegisterModel(&ActionRunner{}) } -func GetRunnersMapByIDs(ctx context.Context, ids []int64) (map[int64]*ActionRunner, error) { - runners := make(map[int64]*ActionRunner, len(ids)) - if len(ids) == 0 { - return runners, nil - } - return runners, db.GetEngine(ctx).In("id", ids).Find(&runners) -} - // FindRunnerOptions // ownerID == 0 and repoID == 0 means any runner including global runners // repoID != 0 and WithAvailable == false means any runner for the given repo diff --git a/models/actions/task.go b/models/actions/task.go index 2a89e42352a..82a718261c9 100644 --- a/models/actions/task.go +++ b/models/actions/task.go @@ -27,15 +27,16 @@ import ( // ActionTask represents a distribution of job type ActionTask struct { - ID int64 - JobID int64 - Job *ActionRunJob `xorm:"-"` - Steps []*ActionTaskStep `xorm:"-"` - Attempt int64 - RunnerID int64 `xorm:"index"` - Status Status `xorm:"index"` - Started timeutil.TimeStamp `xorm:"index"` - Stopped timeutil.TimeStamp `xorm:"index(stopped_log_expired)"` + ID int64 + JobID int64 + Job *ActionRunJob `xorm:"-"` + Steps []*ActionTaskStep `xorm:"-"` + Attempt int64 + RunnerID int64 `xorm:"index"` + RunnerName string `xorm:"VARCHAR(255) NOT NULL DEFAULT ''"` + Status Status `xorm:"index"` + Started timeutil.TimeStamp `xorm:"index"` + Stopped timeutil.TimeStamp `xorm:"index(stopped_log_expired)"` RepoID int64 `xorm:"index"` OwnerID int64 `xorm:"index"` @@ -347,6 +348,7 @@ func claimJobForRunner(ctx context.Context, runner *ActionRunner, job *ActionRun JobID: job.ID, Attempt: job.Attempt, RunnerID: runner.ID, + RunnerName: runner.Name, Started: now, Status: StatusRunning, RepoID: job.RepoID, diff --git a/models/actions/task_test.go b/models/actions/task_test.go index d23607572d1..200eb6760cb 100644 --- a/models/actions/task_test.go +++ b/models/actions/task_test.go @@ -333,6 +333,7 @@ func TestReleaseTaskForRunner(t *testing.T) { } else { require.Equal(t, StatusRunning, claimed.Status) require.Equal(t, task.ID, claimed.TaskID) + assert.Equal(t, runner.Name, unittest.AssertExistsAndLoadBean(t, &ActionTask{ID: task.ID}).RunnerName) require.NoError(t, ReleaseTaskForRunner(t.Context(), task)) diff --git a/services/convert/action_test.go b/services/convert/action_test.go index 9faefc671d7..0a5a190bfaa 100644 --- a/services/convert/action_test.go +++ b/services/convert/action_test.go @@ -143,11 +143,12 @@ func TestToActionWorkflowJob_StepStatusAndBatchParity(t *testing.T) { require.NoError(t, db.Insert(ctx, run)) task := &actions_model.ActionTask{ - ID: 900102, - JobID: 9001, - RepoID: 2, - RunnerID: 34346, - Status: actions_model.StatusFailure, + ID: 900102, + JobID: 9001, + RepoID: 2, + RunnerID: 987654321, + RunnerName: "removed-runner", + Status: actions_model.StatusFailure, } require.NoError(t, db.Insert(ctx, task)) @@ -174,7 +175,8 @@ func TestToActionWorkflowJob_StepStatusAndBatchParity(t *testing.T) { require.NoError(t, err) require.Len(t, apiJob.Steps, 2) - assert.Equal(t, "runner_to_be_deleted-user", apiJob.RunnerName) + assert.EqualValues(t, 987654321, apiJob.RunnerID) + assert.Equal(t, "removed-runner", apiJob.RunnerName) assert.Equal(t, "completed", apiJob.Steps[0].Status, "step 0 status") assert.Equal(t, "success", apiJob.Steps[0].Conclusion, "step 0 conclusion (succeeded before the failure)") assert.Equal(t, "completed", apiJob.Steps[1].Status, "step 1 status") diff --git a/services/convert/convert.go b/services/convert/convert.go index f89b1a604c4..e0c38d8a6f1 100644 --- a/services/convert/convert.go +++ b/services/convert/convert.go @@ -491,7 +491,6 @@ func ToActionWorkflowJob(ctx context.Context, repo *repo_model.Repository, task return nil, err } - var runner *actions_model.ActionRunner if effectiveTaskID := job.EffectiveTaskID(); effectiveTaskID != 0 { if task == nil { task, _, err = db.GetByID[actions_model.ActionTask](ctx, effectiveTaskID) @@ -504,10 +503,9 @@ func ToActionWorkflowJob(ctx context.Context, repo *repo_model.Repository, task if err := (actions_model.TaskList{task}).LoadSteps(ctx); err != nil { return nil, err } - runner, _, _ = db.GetByID[actions_model.ActionRunner](ctx, task.RunnerID) } } - return toActionWorkflowJob(ctx, repo, task, runner, job), nil + return toActionWorkflowJob(ctx, repo, task, job), nil } // ToActionWorkflowJobs requires job.Run.Repo to be loaded @@ -523,26 +521,14 @@ func ToActionWorkflowJobs(ctx context.Context, jobs []*actions_model.ActionRunJo if err := taskList.LoadSteps(ctx); err != nil { return nil, err } - runners, err := actions_model.GetRunnersMapByIDs(ctx, container.FilterSlice(taskList, func(task *actions_model.ActionTask) (int64, bool) { - return task.RunnerID, task.RunnerID != 0 - })) - if err != nil { - return nil, err - } - result := make([]*api.ActionWorkflowJob, len(jobs)) for i, job := range jobs { - task := tasks[job.EffectiveTaskID()] - var runner *actions_model.ActionRunner - if task != nil { - runner = runners[task.RunnerID] - } - result[i] = toActionWorkflowJob(ctx, job.Run.Repo, task, runner, job) + result[i] = toActionWorkflowJob(ctx, job.Run.Repo, tasks[job.EffectiveTaskID()], job) } return result, nil } -func toActionWorkflowJob(ctx context.Context, repo *repo_model.Repository, task *actions_model.ActionTask, runner *actions_model.ActionRunner, job *actions_model.ActionRunJob) *api.ActionWorkflowJob { +func toActionWorkflowJob(ctx context.Context, repo *repo_model.Repository, task *actions_model.ActionTask, job *actions_model.ActionRunJob) *api.ActionWorkflowJob { status, conclusion := ToRunActionsStatus(job.Run, job.Status) var runnerID int64 var runnerName string @@ -550,9 +536,7 @@ func toActionWorkflowJob(ctx context.Context, repo *repo_model.Repository, task if task != nil && job.EffectiveTaskID() != 0 { runnerID = task.RunnerID - if runner != nil { - runnerName = runner.Name - } + runnerName = task.RunnerName for i, step := range task.Steps { stepStatus, stepConclusion := ToActionsStatus(step.Status) steps = append(steps, &api.ActionWorkflowStep{ diff --git a/tests/integration/actions_job_test.go b/tests/integration/actions_job_test.go index 206b9c07927..b21c050781d 100644 --- a/tests/integration/actions_job_test.go +++ b/tests/integration/actions_job_test.go @@ -588,6 +588,12 @@ jobs: }) assert.NoError(t, err) + unittest.AssertNotExistsBean(t, &actions_model.ActionRunner{ID: actionTask.RunnerID}) + req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/actions/jobs/%d", baseRepo.OwnerName, baseRepo.Name, actionRunJob.ID).AddTokenAuth(user2Token) + apiJob := DecodeJSON(t, MakeRequest(t, req, http.StatusOK), &api.ActionWorkflowJob{}) + assert.Equal(t, actionTask.RunnerID, apiJob.RunnerID) + assert.Equal(t, "mock-runner", apiJob.RunnerName) + resp, err = runner.client.runnerServiceClient.FetchTask(t.Context(), &runnerv1.FetchTaskRequest{ TasksVersion: 0, })