Files
gitocean/models/actions/task_test.go
T
Andreas AhlenstorfandMathieu Fenniak fa5a2501d0 fix: do not hide previous attempts without task for latest attempt (#12779)
If a Forgejo Actions job was run more than once, Forgejo would not display previous attempts if no `ActionTask` existed for the latest attempt. That is the case when a job is cancelled or skipped before having been dispatched to a runner or while it is waiting for a runner. This is fixed by always loading all existing attempts.

Resolves #12626.

## 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

- 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/12779
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
Reviewed-by: 0ko <0ko@noreply.codeberg.org>
2026-05-28 15:16:48 +02:00

79 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/unittest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
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, 1, 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)
}
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)
}