2025-11-29 11:49:04 -05:00
// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package actions
import (
"testing"
actions_model "forgejo.org/models/actions"
2026-01-02 13:02:10 -05:00
git_model "forgejo.org/models/git"
2025-11-29 11:49:04 -05:00
"forgejo.org/models/unittest"
2026-01-02 13:02:10 -05:00
"forgejo.org/modules/cache"
"forgejo.org/modules/structs"
2025-11-29 11:49:04 -05:00
2026-01-02 13:02:10 -05:00
"github.com/stretchr/testify/assert"
2025-11-29 11:49:04 -05:00
"github.com/stretchr/testify/require"
2026-01-02 13:02:10 -05:00
"xorm.io/builder"
2025-11-29 11:49:04 -05:00
)
func TestCreateCommitStatus_IncompleteMatrix ( t * testing . T ) {
require . NoError ( t , unittest . PrepareTestDatabase ( ) )
job := unittest . AssertExistsAndLoadBean ( t , & actions_model . ActionRunJob { ID : 192 } )
// Normally this job will attempt to create a commit status on a commit that doesn't exist in the test repo,
// resulting in an error due to the test fixture data not matching the related repos. But it tried.
err := createCommitStatus ( t . Context ( ) , job )
require . ErrorContains ( t , err , "object does not exist [id: 7a3858dc7f059543a8807a8b551304b7e362a7ef" )
feat: support reusable workflow expansion when `with` or `strategy.matrix` contains ${{ needs... }} (#10647)
This change allows the `with:` field of a reusable workflow to reference a previous job, such as `with: { some-input: "${{ needs.other-job.outputs.other-output }}" }`. `strategy.matrix` can also reference `${{ needs... }}`.
When a job is parsed and encounters this situation, the outer job of the workflow is marked with a field `incomplete_with` (or `incomplete_matrix`), indicating to Forgejo that it can't be executed as-is and the other jobs in its `needs` list need to be completed first. And then in `job_emitter.go` when one job is completed, it checks if other jobs had a `needs` reference to it and unblocks those jobs -- but if they're marked with `incomplete_with` then they can be sent back through the job parser, with the now-available job outputs, to be expanded into the correct definition of the job.
The core functionality for this already exists to allow `runs-on` and `strategy.matrix` to reference the outputs of other jobs, but it is expanded upon here to include `with` for reusable workflows.
There is one known defect in this implementation, but it has a limited scope -- if this code path is used to expand a nested reusable workflow, then the `${{ input.... }}` context will be incorrect. This will require an update to the jobparser in runner version 12.4.0, and so it is out-of-scope of this PR.
## Checklist
The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. 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
- 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 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)).
- **end-to-end test:** will require the noted "known defect" to be resolved, but tests are authored at https://code.forgejo.org/forgejo/end-to-end/compare/main...mfenniak:expand-reusable-workflows-needs
### Documentation
- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.
### Release notes
- [ ] I do not want this change to show in the release notes.
- [x] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10647
Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
2025-12-31 13:04:35 -05:00
// Transition from HasIncompleteMatrix()=false to true...
isIncomplete , _ , err := job . HasIncompleteMatrix ( )
2025-11-29 11:49:04 -05:00
require . NoError ( t , err )
require . False ( t , isIncomplete )
job . WorkflowPayload = append ( job . WorkflowPayload , "\nincomplete_matrix: true\n" ... )
2025-12-05 11:17:37 -05:00
job . ClearCachedWorkflowPayload ( )
feat: support reusable workflow expansion when `with` or `strategy.matrix` contains ${{ needs... }} (#10647)
This change allows the `with:` field of a reusable workflow to reference a previous job, such as `with: { some-input: "${{ needs.other-job.outputs.other-output }}" }`. `strategy.matrix` can also reference `${{ needs... }}`.
When a job is parsed and encounters this situation, the outer job of the workflow is marked with a field `incomplete_with` (or `incomplete_matrix`), indicating to Forgejo that it can't be executed as-is and the other jobs in its `needs` list need to be completed first. And then in `job_emitter.go` when one job is completed, it checks if other jobs had a `needs` reference to it and unblocks those jobs -- but if they're marked with `incomplete_with` then they can be sent back through the job parser, with the now-available job outputs, to be expanded into the correct definition of the job.
The core functionality for this already exists to allow `runs-on` and `strategy.matrix` to reference the outputs of other jobs, but it is expanded upon here to include `with` for reusable workflows.
There is one known defect in this implementation, but it has a limited scope -- if this code path is used to expand a nested reusable workflow, then the `${{ input.... }}` context will be incorrect. This will require an update to the jobparser in runner version 12.4.0, and so it is out-of-scope of this PR.
## Checklist
The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. 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
- 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 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)).
- **end-to-end test:** will require the noted "known defect" to be resolved, but tests are authored at https://code.forgejo.org/forgejo/end-to-end/compare/main...mfenniak:expand-reusable-workflows-needs
### Documentation
- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.
### Release notes
- [ ] I do not want this change to show in the release notes.
- [x] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10647
Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
2025-12-31 13:04:35 -05:00
isIncomplete , _ , err = job . HasIncompleteMatrix ( )
2025-11-29 11:49:04 -05:00
require . NoError ( t , err )
require . True ( t , isIncomplete )
feat: support reusable workflow expansion when `with` or `strategy.matrix` contains ${{ needs... }} (#10647)
This change allows the `with:` field of a reusable workflow to reference a previous job, such as `with: { some-input: "${{ needs.other-job.outputs.other-output }}" }`. `strategy.matrix` can also reference `${{ needs... }}`.
When a job is parsed and encounters this situation, the outer job of the workflow is marked with a field `incomplete_with` (or `incomplete_matrix`), indicating to Forgejo that it can't be executed as-is and the other jobs in its `needs` list need to be completed first. And then in `job_emitter.go` when one job is completed, it checks if other jobs had a `needs` reference to it and unblocks those jobs -- but if they're marked with `incomplete_with` then they can be sent back through the job parser, with the now-available job outputs, to be expanded into the correct definition of the job.
The core functionality for this already exists to allow `runs-on` and `strategy.matrix` to reference the outputs of other jobs, but it is expanded upon here to include `with` for reusable workflows.
There is one known defect in this implementation, but it has a limited scope -- if this code path is used to expand a nested reusable workflow, then the `${{ input.... }}` context will be incorrect. This will require an update to the jobparser in runner version 12.4.0, and so it is out-of-scope of this PR.
## Checklist
The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. 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
- 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 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)).
- **end-to-end test:** will require the noted "known defect" to be resolved, but tests are authored at https://code.forgejo.org/forgejo/end-to-end/compare/main...mfenniak:expand-reusable-workflows-needs
### Documentation
- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.
### Release notes
- [ ] I do not want this change to show in the release notes.
- [x] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10647
Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
2025-12-31 13:04:35 -05:00
// Now there should be no error since createCommitStatus will exit early due to the HasIncompleteMatrix() flag.
2025-11-29 11:49:04 -05:00
err = createCommitStatus ( t . Context ( ) , job )
require . NoError ( t , err )
}
2026-01-02 13:02:10 -05:00
func TestCreateCommitStatus_AvoidsDuplicates ( t * testing . T ) {
defer unittest . OverrideFixtures ( "services/actions/TestCreateCommitStatus" ) ( )
require . NoError ( t , unittest . PrepareTestDatabase ( ) )
cache . Init ( )
job := unittest . AssertExistsAndLoadBean ( t , & actions_model . ActionRunJob { ID : 400 } )
targetCommitStatusFilter := builder . Eq { "repo_id" : 4 , "sha" : job . CommitSHA }
// Begin with 0 commit statuses
assert . Equal ( t , 0 , unittest . GetCount ( t , & git_model . CommitStatus { } , targetCommitStatusFilter ) )
err := createCommitStatus ( t . Context ( ) , job )
require . NoError ( t , err )
// Should have 1 commit status now with one createCommitStatus call
assert . Equal ( t , 1 , unittest . GetCount ( t , & git_model . CommitStatus { } , targetCommitStatusFilter ) )
status := unittest . AssertExistsAndLoadBean ( t , & git_model . CommitStatus { } , targetCommitStatusFilter )
assert . EqualValues ( t , 4 , status . RepoID )
assert . Equal ( t , structs . CommitStatusState ( "pending" ) , status . State )
assert . Equal ( t , "c7cd3cd144e6d23c9d6f3d07e52b2c1a956e0338" , status . SHA )
assert . Equal ( t , "/user5/repo4/actions/runs/200/jobs/0" , status . TargetURL )
assert . Equal ( t , "Blocked by required conditions" , status . Description )
assert . Equal ( t , "39c46bc9f0f68e0dcfbb59118e12778fa095b066" , status . ContextHash )
assert . Equal ( t , "/ job_2 (push)" , status . Context ) // This test is intended to cover the runName = "" case, which trims whitespace in this context string -- don't change it in the future
2026-01-05 08:47:27 -05:00
assert . EqualValues ( t , 1 , status . Index )
2026-01-02 13:02:10 -05:00
// No status change, but createCommitStatus invoked again
err = createCommitStatus ( t . Context ( ) , job )
require . NoError ( t , err )
// Should have just the same 1 commit status since the context & state was unchanged.
assert . Equal ( t , 1 , unittest . GetCount ( t , & git_model . CommitStatus { } , targetCommitStatusFilter ) )
2026-01-05 08:47:27 -05:00
// Change status, but still pending -- should add new commit status just for the Description change
job . Status = actions_model . StatusWaiting // Blocked -> Waiting
err = createCommitStatus ( t . Context ( ) , job )
require . NoError ( t , err )
// New commit status added
assert . Equal ( t , 2 , unittest . GetCount ( t , & git_model . CommitStatus { } , targetCommitStatusFilter ) )
status = unittest . AssertExistsAndLoadBean ( t , & git_model . CommitStatus { Index : 2 } , targetCommitStatusFilter )
assert . Equal ( t , structs . CommitStatusState ( "pending" ) , status . State )
assert . Equal ( t , "Waiting to run" , status . Description )
// Invoke createCommitStatus again, check no new record created again
err = createCommitStatus ( t . Context ( ) , job )
require . NoError ( t , err )
assert . Equal ( t , 2 , unittest . GetCount ( t , & git_model . CommitStatus { } , targetCommitStatusFilter ) )
2026-01-02 13:02:10 -05:00
// Update job status & create new commit status
job . Status = actions_model . StatusSuccess
err = createCommitStatus ( t . Context ( ) , job )
require . NoError ( t , err )
2026-01-05 08:47:27 -05:00
// New commit status added w/ updated state & description
assert . Equal ( t , 3 , unittest . GetCount ( t , & git_model . CommitStatus { } , targetCommitStatusFilter ) )
status = unittest . AssertExistsAndLoadBean ( t , & git_model . CommitStatus { Index : 3 } , targetCommitStatusFilter )
assert . Equal ( t , structs . CommitStatusState ( "success" ) , status . State )
assert . Equal ( t , "Successful in 1m38s" , status . Description )
2026-01-02 13:02:10 -05:00
}