fix: prevent initial 'blank' display of action logs view, remove unnecessary API calls (#9099)

When you access Actions logs, there is a momentary render of a partially blank screen such as this:
![actions-log-blank](/attachments/6cd7d97d-edf6-453b-8fe4-2ed7f4339ac4)
This is caused by the `RepoActionView` Vue component having no data.  It then performs a fetch to two APIs in parallel (shown here as `/0` and `/artifacts`) to retrieve the steps and artifacts for the action run, and then it renders the page.

And then, even if the job is complete and the user hasn't yet interacted to expand any logs, it performs the same API calls a second time.
![actions-logs-network-completed-task](/attachments/58044542-eb89-453b-a3f6-d02c9079db44)

This PR fixes both problems by:
- Injecting the initial data for the Action's steps & Artifacts into the initial page render, and using that data into the `RepoActionView` to serve as its initial data load.
- No longer unconditionally starting a second invocation of `loadJob` 1 second later, as the initial data can be used to determine whether the job is still running and requires an interval refresh.

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

**WIP**
- I added test coverage for Go changes...
  - [ ] 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.
  - [ ] 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 bug fixes
  - [PR](https://codeberg.org/forgejo/forgejo/pulls/9099): <!--number 9099 --><!--line 0 --><!--description cHJldmVudCBpbml0aWFsICdibGFuaycgZGlzcGxheSBvZiBhY3Rpb24gbG9ncyB2aWV3LCByZW1vdmUgdW5uZWNlc3NhcnkgQVBJIGNhbGxz-->prevent initial 'blank' display of action logs view, remove unnecessary API calls<!--description-->
<!--end release-notes-assistant-->

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9099
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Reviewed-by: Gusted <gusted@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-05 06:55:32 +02:00 committed by Earl Warren
commit a082555c04
6 changed files with 278 additions and 152 deletions

View file

@ -26,6 +26,7 @@ import (
"forgejo.org/modules/actions"
"forgejo.org/modules/base"
"forgejo.org/modules/git"
"forgejo.org/modules/json"
"forgejo.org/modules/log"
"forgejo.org/modules/setting"
"forgejo.org/modules/storage"
@ -81,6 +82,28 @@ func View(ctx *context_module.Context) {
ctx.Data["WorkflowName"] = workflowName
ctx.Data["WorkflowURL"] = ctx.Repo.RepoLink + "/actions?workflow=" + workflowName
viewResponse := getViewResponse(ctx, &ViewRequest{}, runIndex, jobIndex, attemptNumber)
if ctx.Written() {
return
}
artifactsViewResponse := getArtifactsViewResponse(ctx, runIndex)
if ctx.Written() {
return
}
var buf1, buf2 strings.Builder
if err := json.NewEncoder(&buf1).Encode(viewResponse); err != nil {
ctx.ServerError("EncodingError", err)
return
}
ctx.Data["InitialData"] = buf1.String()
if err := json.NewEncoder(&buf2).Encode(artifactsViewResponse); err != nil {
ctx.ServerError("EncodingError", err)
return
}
ctx.Data["InitialArtifactsData"] = buf2.String()
ctx.HTML(http.StatusOK, tplViewActions)
}
@ -230,14 +253,23 @@ func ViewPost(ctx *context_module.Context) {
// which uses 1-based numbering... would be confusing as "Index" if it later can't be used to index an slice/array.
attemptNumber := ctx.ParamsInt64("attempt")
current, jobs := getRunJobs(ctx, runIndex, jobIndex)
resp := getViewResponse(ctx, req, runIndex, jobIndex, attemptNumber)
if ctx.Written() {
return
}
ctx.JSON(http.StatusOK, resp)
}
func getViewResponse(ctx *context_module.Context, req *ViewRequest, runIndex, jobIndex, attemptNumber int64) *ViewResponse {
current, jobs := getRunJobs(ctx, runIndex, jobIndex)
if ctx.Written() {
return nil
}
run := current.Run
if err := run.LoadAttributes(ctx); err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
return
return nil
}
resp := &ViewResponse{}
@ -309,12 +341,12 @@ func ViewPost(ctx *context_module.Context) {
task, err = actions_model.GetTaskByJobAttempt(ctx, current.ID, attemptNumber)
if err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
return
return nil
}
task.Job = current
if err := task.LoadAttributes(ctx); err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
return
return nil
}
}
@ -329,7 +361,7 @@ func ViewPost(ctx *context_module.Context) {
taskAttempts, err := task.GetAllAttempts(ctx)
if err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
return
return nil
}
allAttempts := make([]*TaskAttempt, len(taskAttempts))
for i, actionTask := range taskAttempts {
@ -396,7 +428,7 @@ func ViewPost(ctx *context_module.Context) {
logRows, err := actions.ReadLogs(ctx, task.LogInStorage, task.LogFilename, offset, length)
if err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
return
return nil
}
for i, row := range logRows {
@ -417,7 +449,7 @@ func ViewPost(ctx *context_module.Context) {
}
}
ctx.JSON(http.StatusOK, resp)
return resp
}
// Rerun will rerun jobs in the given run
@ -685,19 +717,27 @@ type ArtifactsViewItem struct {
func ArtifactsView(ctx *context_module.Context) {
runIndex := ctx.ParamsInt64("run")
artifactsResponse := getArtifactsViewResponse(ctx, runIndex)
if ctx.Written() {
return
}
ctx.JSON(http.StatusOK, artifactsResponse)
}
func getArtifactsViewResponse(ctx *context_module.Context, runIndex int64) *ArtifactsViewResponse {
run, err := actions_model.GetRunByIndex(ctx, ctx.Repo.Repository.ID, runIndex)
if err != nil {
if errors.Is(err, util.ErrNotExist) {
ctx.Error(http.StatusNotFound, err.Error())
return
return nil
}
ctx.Error(http.StatusInternalServerError, err.Error())
return
return nil
}
artifacts, err := actions_model.ListUploadedArtifactsMeta(ctx, run.ID)
if err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
return
return nil
}
artifactsResponse := ArtifactsViewResponse{
Artifacts: make([]*ArtifactsViewItem, 0, len(artifacts)),
@ -713,7 +753,7 @@ func ArtifactsView(ctx *context_module.Context) {
Status: status,
})
}
ctx.JSON(http.StatusOK, artifactsResponse)
return &artifactsResponse
}
func ArtifactsDeleteView(ctx *context_module.Context) {