2025-09-04 22:46:22 +02:00
|
|
|
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
|
|
package actions
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"forgejo.org/models/db"
|
|
|
|
|
"forgejo.org/models/unittest"
|
|
|
|
|
"forgejo.org/modules/timeutil"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestActionTask_GetAllAttempts(t *testing.T) {
|
|
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
|
|
|
|
|
var task ActionTask
|
|
|
|
|
has, err := db.GetEngine(t.Context()).Where("id=?", 47).Get(&task)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.True(t, has, "load ActionTask from fixture")
|
|
|
|
|
|
|
|
|
|
allAttempts, err := task.GetAllAttempts(t.Context())
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Len(t, allAttempts, 3)
|
|
|
|
|
assert.EqualValues(t, 47, allAttempts[0].ID, "ordered by attempt, 1")
|
|
|
|
|
assert.EqualValues(t, 53, allAttempts[1].ID, "ordered by attempt, 2")
|
|
|
|
|
assert.EqualValues(t, 52, allAttempts[2].ID, "ordered by attempt, 3")
|
|
|
|
|
|
|
|
|
|
// GetAllAttempts doesn't populate all fields; so check expected fields from one of the records
|
|
|
|
|
assert.EqualValues(t, 3, allAttempts[0].Attempt, "read Attempt field")
|
|
|
|
|
assert.Equal(t, StatusRunning, allAttempts[0].Status, "read Status field")
|
|
|
|
|
assert.Equal(t, timeutil.TimeStamp(1683636528), allAttempts[0].Started, "read Started field")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestActionTask_GetTaskByJobAttempt(t *testing.T) {
|
|
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
|
|
|
|
|
task, err := GetTaskByJobAttempt(t.Context(), 192, 2)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.EqualValues(t, 192, task.JobID)
|
|
|
|
|
assert.EqualValues(t, 2, task.Attempt)
|
|
|
|
|
|
|
|
|
|
_, err = GetTaskByJobAttempt(t.Context(), 192, 100)
|
|
|
|
|
assert.ErrorContains(t, err, "task with job_id 192 and attempt 100: resource does not exist")
|
|
|
|
|
}
|
feat: expand reusable workflow calls into their inner jobs (#10525)
Previously, Forgejo's behaviour for an Actions reusable workflow was to send the entire job to one specific Forgejo Runner based upon its required `runs-on` label, and that single Runner would then read the workflow file and perform all the jobs inside simultaneously, merging their log output into one output (#9768).
This PR begins an implementation of expanding reusable workflows into their internal jobs.
In this PR, the most basic support is implemented for expanding reusable workflows:
- If a `runs-on` field is provided on the workflow, then the legacy behaviour of sending the reusable workflow to a runner is maintained.
- If the `runs-on` field is omitted, then the job may be expanded, if:
- If the `uses:` is a local path within the repo -- expanded
- If the `uses:` is a path to another repo that is on the same Forgejo server -- expanded
- If the `uses:` is a fully-qualified URL -- not expanded
Because this is an "opt-in" implementation by omitting `runs-on`, and all existing capability is retained, I've **omitted some features** from this PR to make the scope small and manageable for review and testing. These features will be implemented after the initial support is landed:
- Workflow input variables
- Workflow secrets
- Workflow output variables
- "Incomplete" workflows which require multiple passes to evaluate -- any job within a reusable workflow where the `with`, `runs-on`, or `strategy.matrix` fields contain an output from another job with `${{ needs... }}`
Although this implementation has restrictions with missing features, it is intended to fix #9768.
Replaces PR #10448.
## 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 testing: https://code.forgejo.org/forgejo/end-to-end/pulls/1316
### Documentation
- [x] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- https://codeberg.org/forgejo/docs/pulls/1648
- [ ] 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/10525
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-24 20:47:21 +01:00
|
|
|
|
|
|
|
|
func TestActionTask_CreatePlaceholderTask(t *testing.T) {
|
|
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
|
|
|
|
|
job := unittest.AssertExistsAndLoadBean(t, &ActionRunJob{ID: 396})
|
|
|
|
|
assert.EqualValues(t, 0, job.TaskID)
|
|
|
|
|
|
|
|
|
|
task, err := CreatePlaceholderTask(t.Context(), job, map[string]string{"output1": "value1", "output2": "value2"})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
assert.NotEqualValues(t, 0, task.ID)
|
|
|
|
|
assert.Equal(t, job.ID, task.JobID)
|
2026-01-02 15:26:11 +01:00
|
|
|
assert.EqualValues(t, 0, task.Attempt)
|
feat: expand reusable workflow calls into their inner jobs (#10525)
Previously, Forgejo's behaviour for an Actions reusable workflow was to send the entire job to one specific Forgejo Runner based upon its required `runs-on` label, and that single Runner would then read the workflow file and perform all the jobs inside simultaneously, merging their log output into one output (#9768).
This PR begins an implementation of expanding reusable workflows into their internal jobs.
In this PR, the most basic support is implemented for expanding reusable workflows:
- If a `runs-on` field is provided on the workflow, then the legacy behaviour of sending the reusable workflow to a runner is maintained.
- If the `runs-on` field is omitted, then the job may be expanded, if:
- If the `uses:` is a local path within the repo -- expanded
- If the `uses:` is a path to another repo that is on the same Forgejo server -- expanded
- If the `uses:` is a fully-qualified URL -- not expanded
Because this is an "opt-in" implementation by omitting `runs-on`, and all existing capability is retained, I've **omitted some features** from this PR to make the scope small and manageable for review and testing. These features will be implemented after the initial support is landed:
- Workflow input variables
- Workflow secrets
- Workflow output variables
- "Incomplete" workflows which require multiple passes to evaluate -- any job within a reusable workflow where the `with`, `runs-on`, or `strategy.matrix` fields contain an output from another job with `${{ needs... }}`
Although this implementation has restrictions with missing features, it is intended to fix #9768.
Replaces PR #10448.
## 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 testing: https://code.forgejo.org/forgejo/end-to-end/pulls/1316
### Documentation
- [x] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- https://codeberg.org/forgejo/docs/pulls/1648
- [ ] 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/10525
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-24 20:47:21 +01:00
|
|
|
assert.NotEqualValues(t, 0, task.Started)
|
|
|
|
|
assert.NotEqualValues(t, 0, task.Stopped)
|
|
|
|
|
assert.Equal(t, job.Status, task.Status)
|
|
|
|
|
assert.Equal(t, job.RepoID, task.RepoID)
|
|
|
|
|
assert.Equal(t, job.OwnerID, task.OwnerID)
|
|
|
|
|
assert.Equal(t, job.CommitSHA, task.CommitSHA)
|
|
|
|
|
assert.Equal(t, job.IsForkPullRequest, task.IsForkPullRequest)
|
|
|
|
|
|
|
|
|
|
taskOutputs, err := FindTaskOutputByTaskID(t.Context(), task.ID)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Len(t, taskOutputs, 2)
|
|
|
|
|
finalOutputs := map[string]string{}
|
|
|
|
|
for _, to := range taskOutputs {
|
|
|
|
|
finalOutputs[to.OutputKey] = to.OutputValue
|
|
|
|
|
}
|
|
|
|
|
assert.Equal(t, map[string]string{"output1": "value1", "output2": "value2"}, finalOutputs)
|
|
|
|
|
}
|
fix: allow Actions runner to recover tasks lost during fetching from intermittent errors (#11401)
Probably fixes (or improves, at least) https://code.forgejo.org/forgejo/runner/issues/1391, paired with the runner implementation https://code.forgejo.org/forgejo/runner/pulls/1393.
When the FetchTask() API is invoked to create a task, unpreventable environmental errors may occur; for example, network disconnects and timeouts. It's possible that these errors occur after the server-side has assigned a task to the runner during the API call, in which case the error would cause that task to be lost between the two systems -- the server will think it's assigned to the runner, and the runner never received it. This can cause jobs to appear stuck at "Set up job".
The solution implemented here is idempotency in the FetchTask() API call, which means that the "same" FetchTask() API call is expected to return the same values. Specifically, the runner creates a unique identifier which is transmitted to the server as a header `x-runner-request-key` with each FetchTask() invocation which defines the sameness of the call, and the runner retains the value until the API call receives a successful response. The server implementation returns the same tasks back if a second (or Nth) call is received with the same `x-runner-request-key` header. In order to accomplish this is records the `x-runner-request-key` value that is used with each request that assigns tasks.
As a complication, the Forgejo server is unable to return the same `${{ secrets.forgejo_token }}` for the task because the server stores that value in a one-way hash in the database. To resolve this, the server regenerates the token when retrieving tasks for a second time.
## 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 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.
- [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I ran...
- [x] `make pr-go` before pushing
### 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
- [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/11401
Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
2026-02-22 23:24:38 +01:00
|
|
|
|
|
|
|
|
func TestActionTask_GetTasksByRunnerRequestKey(t *testing.T) {
|
|
|
|
|
defer unittest.OverrideFixtures("models/actions/TestActionTask_GetTasksByRunnerRequestKey")()
|
|
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
|
|
|
|
|
runner := unittest.AssertExistsAndLoadBean(t, &ActionRunner{ID: 12345678})
|
|
|
|
|
|
|
|
|
|
// not matching runner_request_key
|
|
|
|
|
tasks, err := GetTasksByRunnerRequestKey(t.Context(), runner, "22288392-2c70-4125-bb01-c7da79fa280c")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Empty(t, tasks)
|
|
|
|
|
|
|
|
|
|
// matching both runner_id and runner_request_key
|
|
|
|
|
tasks, err = GetTasksByRunnerRequestKey(t.Context(), runner, "0a7e017d-4201-4b34-8cf4-de0f431893a4")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Len(t, tasks, 2)
|
|
|
|
|
|
|
|
|
|
// not matching runner_id
|
|
|
|
|
runner = unittest.AssertExistsAndLoadBean(t, &ActionRunner{ID: 10000001})
|
|
|
|
|
tasks, err = GetTasksByRunnerRequestKey(t.Context(), runner, "0a7e017d-4201-4b34-8cf4-de0f431893a4")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Empty(t, tasks)
|
|
|
|
|
}
|