mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-20 01:36:37 +00:00
Placeholder tasks, which are used to store the outputs of a reusable workflow, were hard coded to always have attempt 1. If you executed "Re-run all jobs" with a reusable workflow, a second placeholder task would be created with the same attempt, which caused: (a) Forgejo to not know which attempt, and therefore which outputs, were valid, and (b) the UI to be stuck in "You are viewing an out-of-date run of this job..." when viewing the job. ## 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)). ### 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] I do not want this change to show in the release notes. - [ ] 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/10666 Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org> Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net> Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
78 lines
2.7 KiB
Go
78 lines
2.7 KiB
Go
// 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")
|
|
}
|
|
|
|
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)
|
|
assert.EqualValues(t, 0, task.Attempt)
|
|
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)
|
|
}
|