mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-12 22:10:25 +00:00
fix: prevent jobs with unknown needs from running (#12046)
If Forgejo encounters an Actions workflow with unknown jobs in a needs definition, Forgejo will ignore those and run the job anyway. That is bad. For example, releases could be published without any testing because the name of the testing job was misspelt.
Workflow that demonstrates the problem:
```yaml
on:
push:
workflow_dispatch:
jobs:
build:
runs-on: debian
steps:
- run: |
echo "OK"
test:
runs-on: debian
needs: [does-not-exist]
steps:
- run: |
echo "OK"
```
Now, before a workflow is run, Forgejo will check whether all jobs referenced in `needs` exist. If any of them does not, it raises a pre-execution error which fails the workflow immediately. It also displays an appropriate error to the user, for example:
```
Workflow was not executed due to an error that blocked the execution attempt.
Job with ID test references unknown jobs in `needs`: does-not-exist.
```
Futhermore, workflows with pre-execution errors can no longer be rerun, which was previously possible.
Original issue: https://code.forgejo.org/forgejo/runner/issues/977.
## Checklist
The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. All work and communication must conform to Forgejo's [AI Agreement](https://codeberg.org/forgejo/governance/src/branch/main/AIAgreement.md). There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).
### Tests for Go changes
(can be removed for JavaScript changes)
- I added test coverage for Go changes...
- [x] in their respective `*_test.go` for unit tests.
- [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I ran...
- [x] `make pr-go` before pushing
### Tests for JavaScript changes
(can be removed for Go changes)
- I added test coverage for JavaScript changes...
- [ ] in `web_src/js/*.test.js` if it can be unit tested.
- [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)).
### Documentation
- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [ ] I did not document these changes and I do not expect someone else to do it.
### Release notes
- [x] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change.
- [ ] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change.
*The decision if the pull request will be shown in the release notes is up to the mergers / release team.*
The content of the `release-notes/<pull request number>.md` file will serve as the basis for the release notes. If the file does not exist, the title of the pull request will be used instead.
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12046
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
Co-authored-by: Andreas Ahlenstorf <andreas@ahlenstorf.ch>
Co-committed-by: Andreas Ahlenstorf <andreas@ahlenstorf.ch>
This commit is contained in:
parent
43075c080a
commit
d1b69632aa
18 changed files with 478 additions and 23 deletions
|
|
@ -30,6 +30,7 @@ const (
|
|||
ErrorCodeIncompleteWithMissingOutput
|
||||
ErrorCodeIncompleteWithMissingMatrixDimension
|
||||
ErrorCodeIncompleteWithUnknownCause
|
||||
ErrorCodeUnknownJobInNeeds
|
||||
)
|
||||
|
||||
func TranslatePreExecutionError(lang translation.Locale, run *ActionRun) string {
|
||||
|
|
@ -69,6 +70,8 @@ func TranslatePreExecutionError(lang translation.Locale, run *ActionRun) string
|
|||
return lang.TrString("actions.workflow.incomplete_with_missing_matrix_dimension", run.PreExecutionErrorDetails...)
|
||||
case ErrorCodeIncompleteWithUnknownCause:
|
||||
return lang.TrString("actions.workflow.incomplete_with_unknown_cause", run.PreExecutionErrorDetails...)
|
||||
case ErrorCodeUnknownJobInNeeds:
|
||||
return lang.TrString("actions.workflow.unknown_job_in_needs", run.PreExecutionErrorDetails...)
|
||||
}
|
||||
return fmt.Sprintf("<unsupported error: code=%v details=%#v", run.PreExecutionErrorCode, run.PreExecutionErrorDetails)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -255,6 +255,19 @@ func (run *ActionRun) IsDispatchedRun() bool {
|
|||
return run.TriggerEvent == "workflow_dispatch"
|
||||
}
|
||||
|
||||
// IsRunnable indicates whether this ActionRun can generally be run.
|
||||
func (run *ActionRun) IsRunnable() bool {
|
||||
return run.PreExecutionErrorCode == 0 && run.PreExecutionError == ""
|
||||
}
|
||||
|
||||
// CanBeRerun indicates whether this ActionRun can be rerun.
|
||||
func (run *ActionRun) CanBeRerun() bool {
|
||||
if !run.IsRunnable() {
|
||||
return false
|
||||
}
|
||||
return run.Status.IsDone()
|
||||
}
|
||||
|
||||
func actionsCountOpenCacheKey(repoID int64) string {
|
||||
return fmt.Sprintf("Actions:CountOpenActionRuns:%d", repoID)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -140,6 +140,15 @@ func (job *ActionRunJob) PrepareNextAttempt(initialStatus Status) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// CanBeRerun answers whether this ActionRunJob can be rerun. Returns true if it is done and the Run it belongs to
|
||||
// is runnable. Returns false in all other cases, including when Run is nil.
|
||||
func (job *ActionRunJob) CanBeRerun() bool {
|
||||
if job.Run == nil || !job.Run.IsRunnable() {
|
||||
return false
|
||||
}
|
||||
return job.Status.IsDone()
|
||||
}
|
||||
|
||||
func GetRunJobByID(ctx context.Context, id int64) (*ActionRunJob, error) {
|
||||
var job ActionRunJob
|
||||
has, err := db.GetEngine(ctx).Where("id=?", id).Get(&job)
|
||||
|
|
@ -338,3 +347,17 @@ func (job *ActionRunJob) EnableOpenIDConnect() (bool, error) {
|
|||
}
|
||||
return jobWorkflow.EnableOpenIDConnect, nil
|
||||
}
|
||||
|
||||
// AllNeedsExist checks whether this ActionRunJob's Needs can theoretically be met by comparing them with the supplied
|
||||
// list of all job IDs that part of a particular workflow run. Returns the list of unknown job IDs found in Needs
|
||||
// alongside an indicator whether the check was successful.
|
||||
func (job *ActionRunJob) AllNeedsExist(allExistingJobIDs container.Set[string]) ([]string, bool) {
|
||||
unknownJobIDs := []string{}
|
||||
for _, need := range job.Needs {
|
||||
if !allExistingJobIDs.Contains(need) {
|
||||
unknownJobIDs = append(unknownJobIDs, need)
|
||||
}
|
||||
}
|
||||
|
||||
return unknownJobIDs, len(unknownJobIDs) == 0
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,14 @@ func (jobs ActionJobList) GetRunIDs() []int64 {
|
|||
})
|
||||
}
|
||||
|
||||
func (jobs ActionJobList) GetJobIDs() container.Set[string] {
|
||||
jobIDs := container.SetOf[string]()
|
||||
for _, job := range jobs {
|
||||
jobIDs.Add(job.JobID)
|
||||
}
|
||||
return jobIDs
|
||||
}
|
||||
|
||||
func (jobs ActionJobList) LoadRuns(ctx context.Context, withRepo bool) error {
|
||||
runIDs := jobs.GetRunIDs()
|
||||
runs := make(map[int64]*ActionRun, len(runIDs))
|
||||
|
|
|
|||
21
models/actions/run_job_list_test.go
Normal file
21
models/actions/run_job_list_test.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
// Copyright 2026 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package actions
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"forgejo.org/modules/container"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestActionJobList_GetJobIDs(t *testing.T) {
|
||||
jobs := ActionJobList{
|
||||
&ActionRunJob{JobID: "job 1"},
|
||||
&ActionRunJob{JobID: "job 2"},
|
||||
}
|
||||
|
||||
assert.Equal(t, container.SetOf("job 2", "job 1"), jobs.GetJobIDs())
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@ import (
|
|||
|
||||
"forgejo.org/models/db"
|
||||
"forgejo.org/models/unittest"
|
||||
"forgejo.org/modules/container"
|
||||
"forgejo.org/modules/timeutil"
|
||||
|
||||
"code.forgejo.org/forgejo/runner/v12/act/jobparser"
|
||||
|
|
@ -369,3 +370,126 @@ func TestIsRequestedByRunner(t *testing.T) {
|
|||
|
||||
assert.False(t, emptyHandleJob.IsRequestedByRunner(&differentHandle))
|
||||
}
|
||||
|
||||
func TestAllNeedsExist(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
job ActionRunJob
|
||||
existingJobIDs container.Set[string]
|
||||
expectedUnknownIDs []string
|
||||
ok bool
|
||||
}{
|
||||
{
|
||||
name: "no needs",
|
||||
job: ActionRunJob{Needs: nil},
|
||||
existingJobIDs: container.Set[string]{},
|
||||
expectedUnknownIDs: []string{},
|
||||
ok: true,
|
||||
},
|
||||
{
|
||||
name: "empty needs",
|
||||
job: ActionRunJob{Needs: []string{}},
|
||||
existingJobIDs: container.Set[string]{},
|
||||
expectedUnknownIDs: []string{},
|
||||
ok: true,
|
||||
},
|
||||
{
|
||||
name: "satisfied needs",
|
||||
job: ActionRunJob{Needs: []string{"job1", "job2"}},
|
||||
existingJobIDs: container.SetOf("job2", "job1"),
|
||||
expectedUnknownIDs: []string{},
|
||||
ok: true,
|
||||
},
|
||||
{
|
||||
name: "unsatisfied needs",
|
||||
job: ActionRunJob{Needs: []string{"unknown", "job2"}},
|
||||
existingJobIDs: container.SetOf("job2", "job1"),
|
||||
expectedUnknownIDs: []string{"unknown"},
|
||||
ok: false,
|
||||
},
|
||||
{
|
||||
name: "comparison is case-sensitive",
|
||||
job: ActionRunJob{Needs: []string{"Job1", "job2"}},
|
||||
existingJobIDs: container.SetOf("job2", "job1"),
|
||||
expectedUnknownIDs: []string{"Job1"},
|
||||
ok: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
unknownIDs, ok := testCase.job.AllNeedsExist(testCase.existingJobIDs)
|
||||
|
||||
assert.Equal(t, testCase.ok, ok)
|
||||
assert.Equal(t, testCase.expectedUnknownIDs, unknownIDs)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestActionRunJob_CanBeRerun(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
job ActionRunJob
|
||||
canBeRerun bool
|
||||
}{
|
||||
{
|
||||
name: "job with unknown status",
|
||||
job: ActionRunJob{Run: &ActionRun{Status: StatusSuccess}, Status: StatusUnknown},
|
||||
canBeRerun: false,
|
||||
},
|
||||
{
|
||||
name: "successful job",
|
||||
job: ActionRunJob{Run: &ActionRun{Status: StatusSuccess}, Status: StatusSuccess},
|
||||
canBeRerun: true,
|
||||
},
|
||||
{
|
||||
name: "failed job",
|
||||
job: ActionRunJob{Run: &ActionRun{Status: StatusSuccess}, Status: StatusFailure},
|
||||
canBeRerun: true,
|
||||
},
|
||||
{
|
||||
name: "cancelled job",
|
||||
job: ActionRunJob{Run: &ActionRun{Status: StatusSuccess}, Status: StatusCancelled},
|
||||
canBeRerun: true,
|
||||
},
|
||||
{
|
||||
name: "skipped job",
|
||||
job: ActionRunJob{Run: &ActionRun{Status: StatusSuccess}, Status: StatusSkipped},
|
||||
canBeRerun: true,
|
||||
},
|
||||
{
|
||||
name: "waiting job",
|
||||
job: ActionRunJob{Run: &ActionRun{Status: StatusSuccess}, Status: StatusWaiting},
|
||||
canBeRerun: false,
|
||||
},
|
||||
{
|
||||
name: "blocked job",
|
||||
job: ActionRunJob{Run: &ActionRun{Status: StatusSuccess}, Status: StatusBlocked},
|
||||
canBeRerun: false,
|
||||
},
|
||||
{
|
||||
name: "ActionRun is nil",
|
||||
job: ActionRunJob{Run: nil, Status: StatusSuccess},
|
||||
canBeRerun: false,
|
||||
},
|
||||
{
|
||||
name: "with busy run but completed job",
|
||||
job: ActionRunJob{Run: &ActionRun{Status: StatusRunning}, Status: StatusSuccess},
|
||||
canBeRerun: true,
|
||||
},
|
||||
{
|
||||
name: "with run that cannot be run",
|
||||
job: ActionRunJob{
|
||||
Run: &ActionRun{Status: StatusRunning, PreExecutionErrorCode: ErrorCodeEventDetectionError},
|
||||
Status: StatusSuccess,
|
||||
},
|
||||
canBeRerun: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
assert.Equal(t, testCase.canBeRerun, testCase.job.CanBeRerun())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -96,6 +96,86 @@ func TestIsManualRun(t *testing.T) {
|
|||
assert.False(t, pushRun.IsDispatchedRun())
|
||||
}
|
||||
|
||||
func TestActionRun_IsRunnable(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
run ActionRun
|
||||
isRunnable bool
|
||||
}{
|
||||
{
|
||||
name: "valid run",
|
||||
run: ActionRun{},
|
||||
isRunnable: true,
|
||||
},
|
||||
{
|
||||
name: "with pre-execution error",
|
||||
run: ActionRun{PreExecutionErrorCode: ErrorCodeIncompleteRunsOnMissingOutput},
|
||||
isRunnable: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
assert.Equal(t, testCase.isRunnable, testCase.run.IsRunnable())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestActionRun_CanBeRerun(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
run ActionRun
|
||||
canBeRerun bool
|
||||
}{
|
||||
{
|
||||
name: "run with unknown status",
|
||||
run: ActionRun{Status: StatusUnknown},
|
||||
canBeRerun: false,
|
||||
},
|
||||
{
|
||||
name: "successful run",
|
||||
run: ActionRun{Status: StatusSuccess},
|
||||
canBeRerun: true,
|
||||
},
|
||||
{
|
||||
name: "failed run",
|
||||
run: ActionRun{Status: StatusFailure},
|
||||
canBeRerun: true,
|
||||
},
|
||||
{
|
||||
name: "cancelled run",
|
||||
run: ActionRun{Status: StatusCancelled},
|
||||
canBeRerun: true,
|
||||
},
|
||||
{
|
||||
name: "skipped run",
|
||||
run: ActionRun{Status: StatusSkipped},
|
||||
canBeRerun: true,
|
||||
},
|
||||
{
|
||||
name: "waiting run",
|
||||
run: ActionRun{Status: StatusWaiting},
|
||||
canBeRerun: false,
|
||||
},
|
||||
{
|
||||
name: "blocked run",
|
||||
run: ActionRun{Status: StatusBlocked},
|
||||
canBeRerun: false,
|
||||
},
|
||||
{
|
||||
name: "with pre-execution error",
|
||||
run: ActionRun{PreExecutionErrorCode: ErrorCodeIncompleteRunsOnMissingOutput, Status: StatusSuccess},
|
||||
canBeRerun: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
assert.Equal(t, testCase.canBeRerun, testCase.run.CanBeRerun())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepoNumOpenActions(t *testing.T) {
|
||||
require.NoError(t, unittest.PrepareTestDatabase())
|
||||
err := cache.Init()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue