feat: ability to view previous logs for Actions runs that have been retried (#9017)

Adds a new dropdown to the job logs, visible only when a job has been retried at least once:
![action-with-dropdown](/attachments/9669b47e-2239-4f07-b823-2759dd99a4fb)

When an older run attempt from the dropdown is selected, displays the older run's logs:
![historical-action-logs](/attachments/8b737386-63fb-4f3f-b5b5-ac38c62ed648)

Context on implementation & design decisions:
- It is important that when a URL from an Action's log is shared, the person on the other side sees the exact same logs that were being viewed.  For this reason, all log views are automatically redirected to a fully-qualified URL (including the *run*, *job*, and *attempt*), so that when they are shared there is a guarantee of stability in the viewed logs.
- Individual jobs can be rerun any number of times independent of other jobs in the same workflow.  This means there isn't a "set" of related jobs that were executed at the same time, and this led me to remove the display of current status of jobs on the left-hand side of the view.  There isn't a logical set of job statuses to display here.

Fixes #1043.  Based upon @gmem's original work in #1416.

## 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...
  - [x] 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

- [ ] 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.

<!--start release-notes-assistant-->

## Release notes
<!--URL:https://codeberg.org/forgejo/forgejo-->
- User Interface features
  - [PR](https://codeberg.org/forgejo/forgejo/pulls/9017): <!--number 9017 --><!--line 0 --><!--description YWJpbGl0eSB0byB2aWV3IHByZXZpb3VzIGxvZ3MgZm9yIEFjdGlvbnMgcnVucyB0aGF0IGhhdmUgYmVlbiByZXRyaWVk-->ability to view previous logs for Actions runs that have been retried<!--description-->
<!--end release-notes-assistant-->

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9017
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
This commit is contained in:
Mathieu Fenniak 2025-09-04 22:46:22 +02:00 committed by Earl Warren
commit 8e813902c5
18 changed files with 765 additions and 38 deletions

View file

@ -15,6 +15,10 @@ func TestMain(m *testing.M) {
"action_runner.yml",
"repository.yml",
"action_runner_token.yml",
"user.yml",
"action_run.yml",
"action_run_job.yml",
"action_task.yml",
},
})
}

View file

@ -44,6 +44,31 @@ func init() {
db.RegisterModel(new(ActionRunJob))
}
func (job *ActionRunJob) HTMLURL(ctx context.Context) (string, error) {
if job.Run == nil || job.Run.Repo == nil {
return "", fmt.Errorf("action_run_job: load run and repo before accessing HTMLURL")
}
// Find the "index" of the currently selected job... kinda ugly that the URL uses the index rather than some other
// unique identifier of the job which could actually be stored upon it. But hard to change that now.
allJobs, err := GetRunJobsByRunID(ctx, job.RunID)
if err != nil {
return "", err
}
jobIndex := -1
for i, otherJob := range allJobs {
if job.ID == otherJob.ID {
jobIndex = i
break
}
}
if jobIndex == -1 {
return "", fmt.Errorf("action_run_job: unable to find job on run: %d", job.ID)
}
return fmt.Sprintf("%s/actions/runs/%d/jobs/%d/attempt/%d", job.Run.Repo.HTMLURL(), job.Run.Index, jobIndex, job.Attempt), nil
}
func (job *ActionRunJob) Duration() time.Duration {
return calculateDuration(job.Started, job.Stopped, job.Status)
}

View file

@ -3,9 +3,14 @@
package actions
import (
"fmt"
"testing"
"forgejo.org/models/db"
"forgejo.org/models/unittest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestActionRunJob_ItRunsOn(t *testing.T) {
@ -27,3 +32,41 @@ func TestActionRunJob_ItRunsOn(t *testing.T) {
assert.False(t, actionJob.ItRunsOn(agentLabels))
}
func TestActionRunJob_HTMLURL(t *testing.T) {
require.NoError(t, unittest.PrepareTestDatabase())
tests := []struct {
id int64
expected string
}{
{
id: 192,
expected: "https://try.gitea.io/user5/repo4/actions/runs/187/jobs/0/attempt/1",
},
{
id: 393,
expected: "https://try.gitea.io/user2/repo1/actions/runs/187/jobs/1/attempt/1",
},
{
id: 394,
expected: "https://try.gitea.io/user2/repo1/actions/runs/187/jobs/2/attempt/2",
},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("id=%d", tt.id), func(t *testing.T) {
var job ActionRunJob
has, err := db.GetEngine(t.Context()).Where("id=?", tt.id).Get(&job)
require.NoError(t, err)
require.True(t, has, "load ActionRunJob from fixture")
err = job.LoadAttributes(t.Context())
require.NoError(t, err)
url, err := job.HTMLURL(t.Context())
require.NoError(t, err)
assert.Equal(t, tt.expected, url)
})
}
}

View file

@ -148,6 +148,21 @@ func (task *ActionTask) GenerateToken() (err error) {
return err
}
// Retrieve all the attempts from the same job as the target `ActionTask`. Limited fields are queried to avoid loading
// the LogIndexes blob when not needed.
func (task *ActionTask) GetAllAttempts(ctx context.Context) ([]*ActionTask, error) {
var attempts []*ActionTask
err := db.GetEngine(ctx).
Cols("id", "attempt", "status", "started").
Where("job_id=?", task.JobID).
Desc("attempt").
Find(&attempts)
if err != nil {
return nil, err
}
return attempts, nil
}
func GetTaskByID(ctx context.Context, id int64) (*ActionTask, error) {
var task ActionTask
has, err := db.GetEngine(ctx).Where("id=?", id).Get(&task)
@ -160,6 +175,18 @@ func GetTaskByID(ctx context.Context, id int64) (*ActionTask, error) {
return &task, nil
}
func GetTaskByJobAttempt(ctx context.Context, jobID, attempt int64) (*ActionTask, error) {
var task ActionTask
has, err := db.GetEngine(ctx).Where("job_id=?", jobID).Where("attempt=?", attempt).Get(&task)
if err != nil {
return nil, err
} else if !has {
return nil, fmt.Errorf("task with job_id %d and attempt %d: %w", jobID, attempt, util.ErrNotExist)
}
return &task, nil
}
func GetRunningTaskByToken(ctx context.Context, token string) (*ActionTask, error) {
errNotExist := fmt.Errorf("task with token %q: %w", token, util.ErrNotExist)
if token == "" {

View file

@ -0,0 +1,48 @@
// 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")
}

View file

@ -106,7 +106,7 @@
commit_sha: 985f0301dba5e7b34be866819cd15ad3d8f508ee
is_fork_pull_request: 0
name: job_2
attempt: 1
attempt: 2
job_id: job_2
task_id: 47
status: 5

View file

@ -117,3 +117,43 @@
log_length: 707
log_size: 90179
log_expired: 0
-
id: 52
job_id: 192
attempt: 1
runner_id: 1
status: 1 # success
started: 1683636528
stopped: 1683636626
repo_id: 4
owner_id: 1
commit_sha: c2d72f548424103f01ee1dc02889c1e2bff816b0
is_fork_pull_request: 0
token_hash: b8d3962425466b6709b9ac51446f93260c54afe8e7b6d3686e34f991fb8a8953822b0deed86fe41a103f34bc48dbc4784223
token_salt: ffffffffff
token_last_eight: ffffffff
log_filename: artifact-test2/2f/47.log
log_in_storage: 1
log_length: 707
log_size: 90179
log_expired: 0
-
id: 53
job_id: 192
attempt: 2
runner_id: 1
status: 1 # success
started: 1683636528
stopped: 1683636626
repo_id: 4
owner_id: 1
commit_sha: c2d72f548424103f01ee1dc02889c1e2bff816b0
is_fork_pull_request: 0
token_hash: b8d3962425466b6709b9ac51446f93260c54afe8e7b6d3686e34f991fb8a8953822b0deed86fe41a103f34bc48dbc4784224
token_salt: ffffffffff
token_last_eight: ffffffff
log_filename: artifact-test2/2f/47.log
log_in_storage: 1
log_length: 707
log_size: 90179
log_expired: 0