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
|
|
@ -88,3 +88,21 @@
|
|||
updated: 1683636626
|
||||
need_approval: 0
|
||||
approved_by: 0
|
||||
-
|
||||
id: 905
|
||||
title: "waiting workflow_dispatch run"
|
||||
repo_id: 63
|
||||
owner_id: 2
|
||||
workflow_id: "running.yaml"
|
||||
index: 9
|
||||
trigger_user_id: 2
|
||||
ref: "refs/heads/main"
|
||||
commit_sha: "97f29ee599c373c729132a5c46a046978311e0ee"
|
||||
trigger_event: "workflow_dispatch"
|
||||
is_fork_pull_request: 0
|
||||
status: 5 # waiting
|
||||
started: 1683636528
|
||||
created: 1683636108
|
||||
updated: 1683636626
|
||||
need_approval: 0
|
||||
approved_by: 0
|
||||
|
|
|
|||
|
|
@ -172,3 +172,47 @@
|
|||
incomplete_runs_on_matrix:
|
||||
dimension: platform-oops-wrong-dimension
|
||||
incomplete_matrix: true
|
||||
-
|
||||
id: 607
|
||||
run_id: 905
|
||||
repo_id: 63
|
||||
owner_id: 2
|
||||
commit_sha: 97f29ee599c373c729132a5c46a046978311e0ee
|
||||
is_fork_pull_request: 0
|
||||
name: job_1
|
||||
attempt: 1
|
||||
job_id: job_1
|
||||
task_id: 0
|
||||
status: 7 # blocked
|
||||
runs_on: '["fedora"]'
|
||||
needs: '[]'
|
||||
workflow_payload: |
|
||||
"on":
|
||||
push:
|
||||
jobs:
|
||||
job_1:
|
||||
runs-on: fedora
|
||||
steps:
|
||||
- run: echo "OK!"
|
||||
-
|
||||
id: 608
|
||||
run_id: 905
|
||||
repo_id: 63
|
||||
owner_id: 2
|
||||
commit_sha: 97f29ee599c373c729132a5c46a046978311e0ee
|
||||
is_fork_pull_request: 0
|
||||
name: job_2
|
||||
attempt: 1
|
||||
job_id: job_2
|
||||
task_id: 0
|
||||
status: 7 # blocked
|
||||
runs_on: '["fedora"]'
|
||||
needs: '["unknown","Job_1"]' # invalid capitalization of job_1
|
||||
workflow_payload: |
|
||||
"on":
|
||||
push:
|
||||
jobs:
|
||||
job_2:
|
||||
runs-on: fedora
|
||||
steps:
|
||||
- run: echo "OK!"
|
||||
|
|
|
|||
|
|
@ -136,6 +136,10 @@ type jobStatusResolver struct {
|
|||
jobMap map[int64]*actions_model.ActionRunJob
|
||||
}
|
||||
|
||||
// unknownJobID stores the ID of an unknown job that might be referenced in the workflow. The ID can be any number as
|
||||
// long it does not match the ID of an existing job.
|
||||
var unknownJobID int64 = -1
|
||||
|
||||
func newJobStatusResolver(jobs actions_model.ActionJobList) *jobStatusResolver {
|
||||
idToJobs := make(map[string][]*actions_model.ActionRunJob, len(jobs))
|
||||
jobMap := make(map[int64]*actions_model.ActionRunJob)
|
||||
|
|
@ -149,8 +153,14 @@ func newJobStatusResolver(jobs actions_model.ActionJobList) *jobStatusResolver {
|
|||
for _, job := range jobs {
|
||||
statuses[job.ID] = job.Status
|
||||
for _, need := range job.Needs {
|
||||
for _, v := range idToJobs[need] {
|
||||
needs[job.ID] = append(needs[job.ID], v.ID)
|
||||
neededJobs, ok := idToJobs[need]
|
||||
if ok {
|
||||
for _, v := range neededJobs {
|
||||
needs[job.ID] = append(needs[job.ID], v.ID)
|
||||
}
|
||||
} else {
|
||||
// Handles the case of an unknown job being referenced in `needs`, for example, `needs: ["unknown"]`.
|
||||
needs[job.ID] = append(needs[job.ID], unknownJobID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -231,6 +231,30 @@ __metadata:
|
|||
3: actions_model.StatusFailure,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "blocked if needs are unknown",
|
||||
jobs: actions_model.ActionJobList{
|
||||
{ID: 1, JobID: "build", Status: actions_model.StatusSuccess, Needs: []string{}},
|
||||
{ID: 2, JobID: "test", Status: actions_model.StatusBlocked, Needs: []string{"build", "unknown"}},
|
||||
},
|
||||
want: map[int64]actions_model.Status{},
|
||||
},
|
||||
{
|
||||
name: "blocked if needs are unknown despite always()",
|
||||
jobs: actions_model.ActionJobList{
|
||||
{ID: 1, JobID: "build", Status: actions_model.StatusSuccess, Needs: []string{}},
|
||||
{ID: 45, JobID: "test", Needs: []string{"build", "unknown"}, Status: actions_model.StatusBlocked, WorkflowPayload: []byte(`
|
||||
on: push
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
needs: [build, unknown]
|
||||
if: always()
|
||||
steps: []
|
||||
`)},
|
||||
},
|
||||
want: map[int64]actions_model.Status{},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -97,11 +97,17 @@ func FailRunPreExecutionError(ctx context.Context, run *actions_model.ActionRun,
|
|||
|
||||
// Perform pre-execution checks that would affect the ability for a job to reach an executing stage.
|
||||
func consistencyCheckRun(ctx context.Context, run *actions_model.ActionRun) error {
|
||||
var jobs actions_model.ActionJobList
|
||||
jobs, err := actions_model.GetRunJobsByRunID(ctx, run.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
validJobIDs := jobs.GetJobIDs()
|
||||
for _, job := range jobs {
|
||||
if unknownJobIDs, ok := job.AllNeedsExist(validJobIDs); !ok {
|
||||
return FailRunPreExecutionError(ctx, run, actions_model.ErrorCodeUnknownJobInNeeds,
|
||||
[]any{job.JobID, strings.Join(unknownJobIDs, ", ")})
|
||||
}
|
||||
if stop, err := checkJobWillRevisit(ctx, job); err != nil {
|
||||
return err
|
||||
} else if stop {
|
||||
|
|
|
|||
|
|
@ -137,6 +137,12 @@ func TestActions_consistencyCheckRun(t *testing.T) {
|
|||
name: "consistent: matrix missing dimension but matrix is dynamic",
|
||||
runID: 904,
|
||||
},
|
||||
{
|
||||
name: "unknown job in needs",
|
||||
runID: 905,
|
||||
preExecutionError: actions_model.ErrorCodeUnknownJobInNeeds,
|
||||
preExecutionErrorDetails: []any{"job_2", "unknown, Job_1"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue