fix: display action run attempt status instead of job status (#10321)

On the page displaying the logs of an action run attempt, the header directly above the logs always showed the status of the job. That resulted in the wrong status being displayed for previous run attempts.

Fixes https://codeberg.org/forgejo/forgejo/issues/10236.

![wrong-run-attempt](/attachments/d4f54cc7-a52b-4399-a7cd-efd71f5ebfa7)

## 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.
  - [x] 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.
  - [x] 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.
- [ ] 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.
- [ ] 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-->
- Bug fixes
  - [PR](https://codeberg.org/forgejo/forgejo/pulls/10321): <!--number 10321 --><!--line 0 --><!--description ZGlzcGxheSBhY3Rpb24gcnVuIGF0dGVtcHQgc3RhdHVzIGluc3RlYWQgb2Ygam9iIHN0YXR1cw==-->display action run attempt status instead of job status<!--description-->
<!--end release-notes-assistant-->

Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10321
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
Co-authored-by: Andreas Ahlenstorf <andreas@ahlenstorf.ch>
Co-committed-by: Andreas Ahlenstorf <andreas@ahlenstorf.ch>
This commit is contained in:
Andreas Ahlenstorf 2025-12-07 15:10:47 +01:00 committed by Mathieu Fenniak
parent 9dec4c2888
commit ff4038970d
7 changed files with 186 additions and 133 deletions

View file

@ -32,6 +32,7 @@ import (
"forgejo.org/modules/storage"
"forgejo.org/modules/templates"
"forgejo.org/modules/timeutil"
"forgejo.org/modules/translation"
"forgejo.org/modules/util"
"forgejo.org/modules/web"
"forgejo.org/routers/common"
@ -241,9 +242,10 @@ type ViewStepLogLine struct {
}
type TaskAttempt struct {
Number int64 `json:"number"`
Started template.HTML `json:"time_since_started_html"`
Status string `json:"status"`
Number int64 `json:"number"`
Started template.HTML `json:"time_since_started_html"`
Status string `json:"status"`
StatusDiagnostics []template.HTML `json:"status_diagnostics"`
}
func ViewPost(ctx *app_context.Context) {
@ -358,7 +360,7 @@ func getViewResponse(ctx *app_context.Context, req *ViewRequest, runIndex, jobIn
}
resp.State.CurrentJob.Title = current.Name
resp.State.CurrentJob.Details = current.StatusDiagnostics(ctx.Locale)
resp.State.CurrentJob.Details = statusDiagnostics(current.Status, current, ctx.Locale)
resp.State.CurrentJob.Steps = make([]*ViewJobStep, 0) // marshal to '[]' instead of 'null' in json
resp.Logs.StepsLog = make([]*ViewStepLog, 0) // marshal to '[]' instead of 'null' in json
@ -372,9 +374,10 @@ func getViewResponse(ctx *app_context.Context, req *ViewRequest, runIndex, jobIn
allAttempts := make([]*TaskAttempt, len(taskAttempts))
for i, actionTask := range taskAttempts {
allAttempts[i] = &TaskAttempt{
Number: actionTask.Attempt,
Started: templates.TimeSince(actionTask.Started),
Status: actionTask.Status.String(),
Number: actionTask.Attempt,
Started: templates.TimeSince(actionTask.Started),
Status: actionTask.Status.String(),
StatusDiagnostics: statusDiagnostics(actionTask.Status, task.Job, ctx.Locale),
}
}
resp.State.CurrentJob.AllAttempts = allAttempts
@ -968,3 +971,24 @@ func disableOrEnableWorkflowFile(ctx *app_context.Context, isEnable bool) {
url.QueryEscape(ctx.FormString("actor")), url.QueryEscape(ctx.FormString("status")))
ctx.JSONRedirect(redirectURL)
}
// statusDiagnostics returns optional diagnostic information to display to the user. It should help the user understand
// what the current Status means and whether an action needs to be performed, for example, approving a job.
func statusDiagnostics(status actions_model.Status, job *actions_model.ActionRunJob, lang translation.Locale) []template.HTML {
// Initialize as empty container for it to be serialized to an empty JSON array, not `null`.
diagnostics := []template.HTML{}
switch status {
case actions_model.StatusWaiting:
joinedLabels := strings.Join(job.RunsOn, ", ")
diagnostics = append(diagnostics, lang.TrPluralString(len(job.RunsOn), "actions.status.diagnostics.waiting", joinedLabels))
default:
diagnostics = append(diagnostics, template.HTML(status.LocaleString(lang)))
}
if job.Run.NeedApproval {
diagnostics = append(diagnostics, template.HTML(lang.TrString("actions.need_approval_desc")))
}
return diagnostics
}

View file

@ -13,6 +13,7 @@ import (
repo_model "forgejo.org/models/repo"
unittest_model "forgejo.org/models/unittest"
"forgejo.org/modules/json"
"forgejo.org/modules/translation"
"forgejo.org/modules/web"
"forgejo.org/services/contexttest"
@ -20,7 +21,7 @@ import (
"github.com/stretchr/testify/require"
)
func Test_getRunByID(t *testing.T) {
func TestActionsViewGetRunByID(t *testing.T) {
unittest_model.PrepareTestEnv(t)
repo := unittest_model.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: 5, ID: 4})
@ -61,7 +62,7 @@ func Test_getRunByID(t *testing.T) {
}
}
func Test_artifactsFind(t *testing.T) {
func TestActionsViewArtifactsFind(t *testing.T) {
unittest_model.PrepareTestEnv(t)
for _, testCase := range []struct {
@ -93,7 +94,7 @@ func Test_artifactsFind(t *testing.T) {
}
}
func Test_artifactsFindByNameOrID(t *testing.T) {
func TestActionsViewArtifactsFindByNameOrID(t *testing.T) {
unittest_model.PrepareTestEnv(t)
for _, testCase := range []struct {
@ -195,19 +196,22 @@ func baseExpectedViewResponse() *ViewResponse {
},
AllAttempts: []*TaskAttempt{
{
Number: 3,
Started: template.HTML("<relative-time prefix=\"\" tense=\"past\" datetime=\"2023-05-09T12:48:48Z\" data-tooltip-content data-tooltip-interactive=\"true\">2023-05-09 12:48:48 +00:00</relative-time>"),
Status: "running",
Number: 3,
Started: template.HTML("<relative-time prefix=\"\" tense=\"past\" datetime=\"2023-05-09T12:48:48Z\" data-tooltip-content data-tooltip-interactive=\"true\">2023-05-09 12:48:48 +00:00</relative-time>"),
Status: "running",
StatusDiagnostics: []template.HTML{"actions.status.running"},
},
{
Number: 2,
Started: template.HTML("<relative-time prefix=\"\" tense=\"past\" datetime=\"2023-05-09T12:48:48Z\" data-tooltip-content data-tooltip-interactive=\"true\">2023-05-09 12:48:48 +00:00</relative-time>"),
Status: "success",
Number: 2,
Started: template.HTML("<relative-time prefix=\"\" tense=\"past\" datetime=\"2023-05-09T12:48:48Z\" data-tooltip-content data-tooltip-interactive=\"true\">2023-05-09 12:48:48 +00:00</relative-time>"),
Status: "success",
StatusDiagnostics: []template.HTML{"actions.status.success"},
},
{
Number: 1,
Started: template.HTML("<relative-time prefix=\"\" tense=\"past\" datetime=\"2023-05-09T12:48:48Z\" data-tooltip-content data-tooltip-interactive=\"true\">2023-05-09 12:48:48 +00:00</relative-time>"),
Status: "success",
Number: 1,
Started: template.HTML("<relative-time prefix=\"\" tense=\"past\" datetime=\"2023-05-09T12:48:48Z\" data-tooltip-content data-tooltip-interactive=\"true\">2023-05-09 12:48:48 +00:00</relative-time>"),
Status: "success",
StatusDiagnostics: []template.HTML{"actions.status.success"},
},
},
},
@ -281,9 +285,10 @@ func TestActionsViewViewPost(t *testing.T) {
}
resp.State.CurrentJob.AllAttempts = []*TaskAttempt{
{
Number: 1,
Started: template.HTML("<relative-time prefix=\"\" tense=\"past\" datetime=\"2023-05-09T12:48:48Z\" data-tooltip-content data-tooltip-interactive=\"true\">2023-05-09 12:48:48 +00:00</relative-time>"),
Status: "success",
Number: 1,
Started: template.HTML("<relative-time prefix=\"\" tense=\"past\" datetime=\"2023-05-09T12:48:48Z\" data-tooltip-content data-tooltip-interactive=\"true\">2023-05-09 12:48:48 +00:00</relative-time>"),
Status: "success",
StatusDiagnostics: []template.HTML{"actions.status.success"},
},
}
@ -510,3 +515,91 @@ func TestActionsRerun(t *testing.T) {
})
}
}
func TestActionsViewStatusDiagnostics(t *testing.T) {
translation.InitLocales(t.Context())
english := translation.NewLocale("en-US")
testCases := []struct {
name string
status actions_model.Status
job actions_model.ActionRunJob
expected []template.HTML
}{
{
name: "Unknown status",
status: actions_model.StatusUnknown,
job: actions_model.ActionRunJob{RunsOn: []string{"windows"}, Run: &actions_model.ActionRun{NeedApproval: false}},
expected: []template.HTML{"Unknown"},
},
{
name: "Waiting without labels",
status: actions_model.StatusWaiting,
job: actions_model.ActionRunJob{RunsOn: []string{}, Run: &actions_model.ActionRun{NeedApproval: false}},
expected: []template.HTML{"Waiting for a runner with the following labels: "},
},
{
name: "Waiting with one label",
status: actions_model.StatusWaiting,
job: actions_model.ActionRunJob{RunsOn: []string{"freebsd"}, Run: &actions_model.ActionRun{NeedApproval: false}},
expected: []template.HTML{"Waiting for a runner with the following label: freebsd"},
},
{
name: "Waiting with labels, no approval",
status: actions_model.StatusWaiting,
job: actions_model.ActionRunJob{RunsOn: []string{"docker", "ubuntu"}, Run: &actions_model.ActionRun{NeedApproval: false}},
expected: []template.HTML{"Waiting for a runner with the following labels: docker, ubuntu"},
},
{
name: "Waiting with labels, approval",
status: actions_model.StatusWaiting,
job: actions_model.ActionRunJob{RunsOn: []string{"docker", "ubuntu"}, Run: &actions_model.ActionRun{NeedApproval: true}},
expected: []template.HTML{
"Waiting for a runner with the following labels: docker, ubuntu",
"Need approval to run workflows for fork pull request.",
},
},
{
name: "Running",
status: actions_model.StatusRunning,
job: actions_model.ActionRunJob{RunsOn: []string{"debian"}, Run: &actions_model.ActionRun{NeedApproval: false}},
expected: []template.HTML{"Running"},
},
{
name: "Success",
status: actions_model.StatusSuccess,
job: actions_model.ActionRunJob{RunsOn: []string{"debian"}, Run: &actions_model.ActionRun{NeedApproval: false}},
expected: []template.HTML{"Success"},
},
{
name: "Failure",
status: actions_model.StatusFailure,
job: actions_model.ActionRunJob{RunsOn: []string{"debian"}, Run: &actions_model.ActionRun{NeedApproval: false}},
expected: []template.HTML{"Failure"},
},
{
name: "Cancelled",
status: actions_model.StatusCancelled,
job: actions_model.ActionRunJob{RunsOn: []string{"debian"}, Run: &actions_model.ActionRun{NeedApproval: false}},
expected: []template.HTML{"Canceled"},
},
{
name: "Skipped",
status: actions_model.StatusSkipped,
job: actions_model.ActionRunJob{RunsOn: []string{"debian"}, Run: &actions_model.ActionRun{NeedApproval: false}},
expected: []template.HTML{"Skipped"},
},
{
name: "Blocked",
status: actions_model.StatusBlocked,
job: actions_model.ActionRunJob{RunsOn: []string{"debian"}, Run: &actions_model.ActionRun{NeedApproval: false}},
expected: []template.HTML{"Blocked"},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
assert.Equal(t, testCase.expected, statusDiagnostics(testCase.status, &testCase.job, english))
})
}
}