diff --git a/modelmigration/migrations.go b/modelmigration/migrations.go index 711454ffdae..73a2631d374 100644 --- a/modelmigration/migrations.go +++ b/modelmigration/migrations.go @@ -451,6 +451,7 @@ func prepareMigrationTasks() []*migration { newMigration(372, "Add workflow_ref to action_run", v29.AddWorkflowRefToActionRun), newMigration(373, "Repair Debian and RPM signing keys rejected by Sequoia", v29.RepairPackageSigningKeys), newMigration(374, "Add pull_merge_queue_entry table", v29.AddPullMergeQueueEntryTable), + newMigration(375, "Update OwnerID as zero for repository level runner tokens", v29.UpdateOwnerIDOfRepoLevelRunnerTokens), } return preparedMigrations } diff --git a/modelmigration/v29/v375.go b/modelmigration/v29/v375.go new file mode 100644 index 00000000000..7e33ee530e7 --- /dev/null +++ b/modelmigration/v29/v375.go @@ -0,0 +1,16 @@ +// Copyright 2026 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v29 + +import ( + "context" + + "gitea.dev/modelmigration/base" +) + +func UpdateOwnerIDOfRepoLevelRunnerTokens(_ctx context.Context, x base.EngineMigration) error { + // inactive rows can't register, a newer one moved into scope would become the latest token and trigger an auto-reset + _, err := x.Exec("UPDATE `action_runner_token` SET `owner_id` = 0 WHERE `repo_id` > 0 AND `owner_id` > 0 AND `is_active` = ?", true) + return err +} diff --git a/modelmigration/v29/v375_test.go b/modelmigration/v29/v375_test.go new file mode 100644 index 00000000000..17877a445b7 --- /dev/null +++ b/modelmigration/v29/v375_test.go @@ -0,0 +1,44 @@ +// 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 TestUpdateOwnerIDOfRepoLevelRunnerTokens(t *testing.T) { + type ActionRunnerToken struct { + ID int64 `xorm:"pk autoincr"` + OwnerID int64 + RepoID int64 + IsActive bool + } + + x, deferrable := migrationtest.PrepareTestEnv(t, 0, new(ActionRunnerToken)) + defer deferrable() + if x == nil || t.Failed() { + return + } + + _, err := x.Insert( + &ActionRunnerToken{OwnerID: 2, RepoID: 1, IsActive: true}, + &ActionRunnerToken{OwnerID: 2, RepoID: 1}, + &ActionRunnerToken{OwnerID: 2, IsActive: true}, + ) + require.NoError(t, err) + require.NoError(t, UpdateOwnerIDOfRepoLevelRunnerTokens(t.Context(), x)) + + var tokens []ActionRunnerToken + require.NoError(t, x.Asc("id").Find(&tokens)) + assert.Equal(t, []ActionRunnerToken{ + {ID: 1, OwnerID: 0, RepoID: 1, IsActive: true}, + {ID: 2, OwnerID: 2, RepoID: 1}, + {ID: 3, OwnerID: 2, IsActive: true}, + }, tokens) +} diff --git a/models/actions/runner_token.go b/models/actions/runner_token.go index 3bb16e9d194..52df9f9838d 100644 --- a/models/actions/runner_token.go +++ b/models/actions/runner_token.go @@ -57,18 +57,6 @@ func GetRunnerToken(ctx context.Context, token string) (*ActionRunnerToken, erro return &runnerToken, nil } -// UpdateRunnerToken updates runner token information. -func UpdateRunnerToken(ctx context.Context, r *ActionRunnerToken, cols ...string) (err error) { - e := db.GetEngine(ctx) - - if len(cols) == 0 { - _, err = e.ID(r.ID).AllCols().Update(r) - } else { - _, err = e.ID(r.ID).Cols(cols...).Update(r) - } - return err -} - // NewRunnerTokenWithValue creates a new active runner token and invalidate all old tokens // ownerID will be ignored and treated as 0 if repoID is non-zero. func NewRunnerTokenWithValue(ctx context.Context, ownerID, repoID int64, token string) (*ActionRunnerToken, error) { diff --git a/models/actions/runner_token_test.go b/models/actions/runner_token_test.go index d95a530f101..f5863077a8b 100644 --- a/models/actions/runner_token_test.go +++ b/models/actions/runner_token_test.go @@ -27,13 +27,3 @@ func TestNewRunnerToken(t *testing.T) { assert.NoError(t, err) assert.Equal(t, expectedToken, token) } - -func TestUpdateRunnerToken(t *testing.T) { - assert.NoError(t, unittest.PrepareTestDatabase()) - token := unittest.AssertExistsAndLoadBean(t, &ActionRunnerToken{ID: 3}) - token.IsActive = true - assert.NoError(t, UpdateRunnerToken(t.Context(), token)) - expectedToken, err := GetLatestRunnerToken(t.Context(), 1, 0) - assert.NoError(t, err) - assert.Equal(t, expectedToken, token) -} diff --git a/routers/api/actions/runner/runner.go b/routers/api/actions/runner/runner.go index 12cc14c9ff3..fb3c4d16739 100644 --- a/routers/api/actions/runner/runner.go +++ b/routers/api/actions/runner/runner.go @@ -93,12 +93,6 @@ func (s *Service) Register( return nil, errors.New("can't create new runner") } - // update token status - runnerToken.IsActive = true - if err := actions_model.UpdateRunnerToken(ctx, runnerToken, "is_active"); err != nil { - return nil, errors.New("can't update runner token status") - } - res := &runnerv1.RegisterResponse{ Runner: &runnerv1.Runner{ Id: runner.ID,