mirror of
https://github.com/go-gitea/gitea.git
synced 2026-09-26 23:56:37 +09:00
a15f032026
Gitea doesn't evaluate a job's `if:` before checking the job's concurrency group, which causes a job that should have been skipped to incorrectly cancel other jobs in the same concurrency group. This PR makes Gitea decide `if:` for every job before it becomes waiting, including jobs without `needs` at insertion, on approval and on rerun. A skipped job therefore no longer takes part in job concurrency or holds a max-parallel slot, and a reusable caller whose `if:` is false is no longer expanded on approval or rerun. An invalid `if:` skips the job with an error summary. After this PR, Gitea decides all jobs' `if:` expressions and sends `if: always()` to the runner, so the runner no longer needs to evaluate a job's `if:` again ([gitea/runner `run_context.go`](https://gitea.com/gitea/runner/src/commit/81add274599355ec1838b6ebe45804890d40bab9/act/runner/run_context.go#L1195)). --------- Co-authored-by: silverwind <me@silverwind.io>
112 lines
4.5 KiB
Go
112 lines
4.5 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package actions
|
|
|
|
import (
|
|
"testing"
|
|
|
|
actions_model "gitea.dev/models/actions"
|
|
"gitea.dev/models/db"
|
|
repo_model "gitea.dev/models/repo"
|
|
"gitea.dev/models/unittest"
|
|
user_model "gitea.dev/models/user"
|
|
"gitea.dev/modules/test"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestApproveRuns(t *testing.T) {
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
insertRun := func(index int64, status actions_model.Status, needApproval bool, approvedBy int64) *actions_model.ActionRun {
|
|
run := &actions_model.ActionRun{
|
|
Title: "approve-run", RepoID: repo.ID, OwnerID: repo.OwnerID, WorkflowID: "test.yaml", Index: index,
|
|
TriggerUserID: doer.ID, Ref: "refs/heads/main",
|
|
CommitSHA: "c2d72f548424103f01ee1dc02889c1e2bff816b0", Event: "push", TriggerEvent: "push",
|
|
Status: status, NeedApproval: needApproval, ApprovedBy: approvedBy,
|
|
}
|
|
require.NoError(t, db.Insert(t.Context(), run))
|
|
return run
|
|
}
|
|
insertJob := func(run *actions_model.ActionRun, status actions_model.Status, needs ...string) *actions_model.ActionRunJob {
|
|
job := &actions_model.ActionRunJob{
|
|
RunID: run.ID, RepoID: run.RepoID, OwnerID: run.OwnerID, CommitSHA: run.CommitSHA,
|
|
Name: "job1", Attempt: 1, JobID: "job1", Status: status,
|
|
RunsOn: []string{"ubuntu-latest"}, Needs: needs,
|
|
WorkflowPayload: minimalWorkflowPayload("job1"),
|
|
}
|
|
require.NoError(t, db.Insert(t.Context(), job))
|
|
return job
|
|
}
|
|
|
|
t.Run("approve unblocks a job with no dependencies", func(t *testing.T) {
|
|
run := insertRun(1001, actions_model.StatusBlocked, true, 0)
|
|
job := insertJob(run, actions_model.StatusBlocked)
|
|
|
|
approved, err := ApproveRuns(t.Context(), repo, doer, []int64{run.ID})
|
|
require.NoError(t, err)
|
|
require.Len(t, approved, 1)
|
|
assert.False(t, approved[0].NeedApproval)
|
|
assert.Equal(t, doer.ID, approved[0].ApprovedBy)
|
|
|
|
assert.Equal(t, actions_model.StatusWaiting, unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunJob{ID: job.ID}).Status)
|
|
})
|
|
|
|
t.Run("approve skips a job whose if is false", func(t *testing.T) {
|
|
defer test.MockVariableValue(&EmitJobsIfReadyByRun, func(int64) error { return nil })()
|
|
run := insertRun(1006, actions_model.StatusBlocked, true, 0)
|
|
job := insertJob(run, actions_model.StatusBlocked)
|
|
job.WorkflowPayload = []byte("jobs:\n job1:\n if: false\n")
|
|
_, err := actions_model.UpdateRunJob(t.Context(), job, nil, "workflow_payload")
|
|
require.NoError(t, err)
|
|
|
|
_, err = ApproveRuns(t.Context(), repo, doer, []int64{run.ID})
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, actions_model.StatusSkipped, unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunJob{ID: job.ID}).Status)
|
|
})
|
|
|
|
t.Run("a job with unmet dependencies stays blocked", func(t *testing.T) {
|
|
run := insertRun(1002, actions_model.StatusBlocked, true, 0)
|
|
job := insertJob(run, actions_model.StatusBlocked, "some-other-job")
|
|
|
|
approved, err := ApproveRuns(t.Context(), repo, doer, []int64{run.ID})
|
|
require.NoError(t, err)
|
|
require.Len(t, approved, 1)
|
|
assert.False(t, approved[0].NeedApproval)
|
|
|
|
assert.Equal(t, actions_model.StatusBlocked, unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunJob{ID: job.ID}).Status)
|
|
})
|
|
|
|
t.Run("re-approving an approved run is a no-op", func(t *testing.T) {
|
|
run := insertRun(1005, actions_model.StatusRunning, false, 4)
|
|
job := insertJob(run, actions_model.StatusRunning)
|
|
|
|
approved, err := ApproveRuns(t.Context(), repo, doer, []int64{run.ID})
|
|
require.NoError(t, err)
|
|
require.Len(t, approved, 1)
|
|
assert.EqualValues(t, 4, approved[0].ApprovedBy, "approver must not be overwritten")
|
|
assert.Equal(t, actions_model.StatusRunning, unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunJob{ID: job.ID}).Status, "started job must not be reset to waiting")
|
|
})
|
|
|
|
t.Run("approving several runs returns them in the requested order", func(t *testing.T) {
|
|
run1 := insertRun(1003, actions_model.StatusBlocked, true, 0)
|
|
insertJob(run1, actions_model.StatusBlocked)
|
|
run2 := insertRun(1004, actions_model.StatusBlocked, true, 0)
|
|
insertJob(run2, actions_model.StatusBlocked)
|
|
|
|
approved, err := ApproveRuns(t.Context(), repo, doer, []int64{run2.ID, run1.ID})
|
|
require.NoError(t, err)
|
|
require.Len(t, approved, 2)
|
|
assert.Equal(t, run2.ID, approved[0].ID)
|
|
assert.Equal(t, run1.ID, approved[1].ID)
|
|
assert.False(t, approved[0].NeedApproval)
|
|
assert.False(t, approved[1].NeedApproval)
|
|
})
|
|
}
|