diff --git a/modelmigration/migrations.go b/modelmigration/migrations.go index 1fc1de3ba29..33704320b79 100644 --- a/modelmigration/migrations.go +++ b/modelmigration/migrations.go @@ -446,6 +446,7 @@ func prepareMigrationTasks() []*migration { 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), + newMigration(370, "Change raw_concurrency of action_run and action_run_job to TEXT on MSSQL", v29.ChangeActionsRawConcurrencyToText), } return preparedMigrations } diff --git a/modelmigration/v29/v370.go b/modelmigration/v29/v370.go new file mode 100644 index 00000000000..ba5276cbc39 --- /dev/null +++ b/modelmigration/v29/v370.go @@ -0,0 +1,24 @@ +// Copyright 2026 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v29 + +import ( + "context" + "fmt" + + "gitea.dev/modelmigration/base" + "gitea.dev/modules/setting" +) + +func ChangeActionsRawConcurrencyToText(_ context.Context, x base.EngineMigration) error { + if !setting.Database.Type.IsMSSQL() { // startup sync widens VARCHAR to TEXT on MySQL and PostgreSQL + return nil + } + for _, table := range []string{"action_run", "action_run_job"} { + if _, err := x.Exec("ALTER TABLE " + table + " ALTER COLUMN raw_concurrency NVARCHAR(MAX) NULL"); err != nil { + return fmt.Errorf("ChangeActionsRawConcurrencyToText: %s: %w", table, err) + } + } + return nil +} diff --git a/modelmigration/v29/v370_test.go b/modelmigration/v29/v370_test.go new file mode 100644 index 00000000000..2e6ded654ea --- /dev/null +++ b/modelmigration/v29/v370_test.go @@ -0,0 +1,52 @@ +// Copyright 2026 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v29 + +import ( + "strings" + "testing" + + "gitea.dev/modelmigration/migrationtest" + "gitea.dev/modules/setting" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestChangeActionsRawConcurrencyToText(t *testing.T) { + if !setting.Database.Type.IsMSSQL() { + t.Skip("Only MSSQL needs a migration to widen raw_concurrency") + } + + type ActionRun struct { + ID int64 `xorm:"pk autoincr"` + RawConcurrency string + } + type ActionRunJob struct { + ID int64 `xorm:"pk autoincr"` + RawConcurrency string + } + + x, deferrable := migrationtest.PrepareTestEnv(t, 0, new(ActionRun), new(ActionRunJob)) + defer deferrable() + if x == nil || t.Failed() { + return + } + + require.NoError(t, ChangeActionsRawConcurrencyToText(t.Context(), x)) + + long := strings.Repeat("界", 1000) + run := &ActionRun{RawConcurrency: long} + job := &ActionRunJob{RawConcurrency: long} + _, err := x.Insert(run, job) + require.NoError(t, err) + + gotRun, gotJob := &ActionRun{ID: run.ID}, &ActionRunJob{ID: job.ID} + _, err = x.Get(gotRun) + require.NoError(t, err) + _, err = x.Get(gotJob) + require.NoError(t, err) + assert.Equal(t, long, gotRun.RawConcurrency) + assert.Equal(t, long, gotJob.RawConcurrency) +} diff --git a/models/actions/run.go b/models/actions/run.go index aed2971d616..f3ae4b8db6f 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -51,7 +51,7 @@ type ActionRun struct { TriggerEvent string // the trigger event defined in the `on` configuration of the triggered workflow Status Status `xorm:"index index(repo_status)"` Version int `xorm:"version default 0"` // Status could be updated concomitantly, so an optimistic lock is needed - RawConcurrency string // raw concurrency + RawConcurrency string `xorm:"TEXT"` // raw concurrency // WorkflowRepoID/WorkflowCommitSHA record the (repo, commit) the run's workflow file content came from. // Always filled (repo-level run = the repo itself; scoped run = the source repo). diff --git a/models/actions/run_job.go b/models/actions/run_job.go index ba54ed3f5db..dd3afdbb610 100644 --- a/models/actions/run_job.go +++ b/models/actions/run_job.go @@ -56,7 +56,7 @@ type ActionRunJob struct { Status Status `xorm:"index index(pickup) index(repo_status)"` - RawConcurrency string // raw concurrency from job YAML's "concurrency" section + RawConcurrency string `xorm:"TEXT"` // raw concurrency from job YAML's "concurrency" section // IsConcurrencyEvaluated is only valid/needed when this job's RawConcurrency is not empty. // If RawConcurrency can't be evaluated (e.g. depend on other job's outputs or have errors), this field will be false. diff --git a/services/actions/concurrency.go b/services/actions/concurrency.go index 7f4dce194ea..7f496586060 100644 --- a/services/actions/concurrency.go +++ b/services/actions/concurrency.go @@ -6,10 +6,12 @@ package actions import ( "context" "fmt" + "unicode/utf8" act_model "gitea.dev/actionslib/pkg/model" actions_model "gitea.dev/models/actions" "gitea.dev/modules/actions/jobparser" + "gitea.dev/modules/base" "gitea.dev/modules/setting" "gitea.dev/modules/util" webhook_module "gitea.dev/modules/webhook" @@ -41,6 +43,7 @@ func EvaluateRunConcurrencyFillModel(ctx context.Context, run *actions_model.Act if err != nil { return fmt.Errorf("evaluate concurrency: %w", err) } + attempt.ConcurrencyGroup = boundedConcurrencyGroup(attempt.ConcurrencyGroup) return nil } @@ -83,6 +86,14 @@ func EvaluateJobConcurrencyFillModel(ctx context.Context, run *actions_model.Act if err != nil { return util.NewInvalidArgumentErrorf("%v", err) } + actionRunJob.ConcurrencyGroup = boundedConcurrencyGroup(actionRunJob.ConcurrencyGroup) actionRunJob.IsConcurrencyEvaluated = true return nil } + +func boundedConcurrencyGroup(group string) string { + if utf8.RuneCountInString(group) > 255 { + return base.EncodeSha256(group) + } + return group +} diff --git a/services/actions/context_test.go b/services/actions/context_test.go index 80aaf30187e..ce8b44495b6 100644 --- a/services/actions/context_test.go +++ b/services/actions/context_test.go @@ -5,7 +5,9 @@ package actions import ( "strconv" + "strings" "testing" + "unicode/utf8" act_model "gitea.dev/actionslib/pkg/model" actions_model "gitea.dev/models/actions" @@ -49,6 +51,19 @@ func TestEvaluateRunConcurrency_RunIDFallback(t *testing.T) { assert.Contains(t, attemptA.ConcurrencyGroup, "791") assert.Contains(t, attemptB.ConcurrencyGroup, "792") assert.NotEqual(t, attemptA.ConcurrencyGroup, attemptB.ConcurrencyGroup) + + t.Run("preserves 255 runes and bounds longer groups", func(t *testing.T) { + evaluateGroup := func(group string) string { + require.NoError(t, EvaluateRunConcurrencyFillModel(ctx, runA, attemptA, &act_model.RawConcurrency{Group: group}, nil, nil)) + return attemptA.ConcurrencyGroup + } + group := strings.Repeat("界", 255) + assert.Equal(t, group, evaluateGroup(group)) + boundedGroup := evaluateGroup(group + "a") + assert.LessOrEqual(t, utf8.RuneCountInString(boundedGroup), 255) + assert.Equal(t, boundedGroup, evaluateGroup(group+"a")) + assert.NotEqual(t, boundedGroup, evaluateGroup(group+"b")) + }) } func TestPrepareRunAndInsert_ExpressionsSeeRunID(t *testing.T) { diff --git a/services/actions/notifier_helper.go b/services/actions/notifier_helper.go index 84fd41f8c01..8ce0e0e7fef 100644 --- a/services/actions/notifier_helper.go +++ b/services/actions/notifier_helper.go @@ -396,7 +396,7 @@ func handleWorkflows( for _, dwf := range detectedWorkflows { // repo-level run: the workflow content is this repo at dwf.SourceCommitSHA if err := buildApproveAndInsertRun(ctx, input, ref, commit, payload, isForkPullRequest, dwf, input.Repo.ID, false); err != nil { - log.Error("repo %s: %v", input.Repo.FullName(), err) + log.Error("repo %s workflow %s ref %s: %v", input.Repo.FullName(), dwf.EntryName, ref, err) continue } } @@ -816,7 +816,7 @@ func detectAndHandleScopedWorkflows( } if err := buildApproveAndInsertRun(ctx, input, ref, consumerCommit, payload, isForkPullRequest, dwf, sourceRepo.ID, true); err != nil { - log.Error("scoped workflows: source %s workflow %s: %v", sourceRepo.FullName(), dwf.EntryName, err) + log.Error("scoped workflows: source %s workflow %s for consumer %s ref %s: %v", sourceRepo.FullName(), dwf.EntryName, input.Repo.FullName(), ref, err) continue } }