From 906e2e7c4a0219c286fadadb36bb7a9465cca3ea Mon Sep 17 00:00:00 2001 From: Mathieu Fenniak Date: Tue, 14 Oct 2025 02:10:46 +0200 Subject: [PATCH] fix: false error logging "Render JSON failed" from workflow dispatch via API (#9675) When making a `POST` to start a workflow dispatch, and not specifying the option `"return_run_info": true`, the API handler attempts to render a `nil` as a JSON body to a `204 No Content` response. This results in an error being logged to the console, as this status code does not permit a body. ``` Render JSON failed: http: request method or response status code does not allow body ``` There is no functional impact except for a false error log, as `ctx.JSON` just logs a `Render JSON failed` error if it fails. I could not find any existing code which allows integration tests to intercept or inspect log output, which would be required for an automated test verifying this is fixed. If anyone could advise an existing test that performs log interception, or any hints on how such a mechanism would be created, I don't mind adding it... but it may not be warranted for such a tiny bug either. ## 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... - [ ] 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 - [ ] 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/.md` to be be used for the release notes instead of the title. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9675 Reviewed-by: Gusted Co-authored-by: Mathieu Fenniak Co-committed-by: Mathieu Fenniak --- routers/api/v1/repo/action.go | 2 +- tests/integration/api_repo_actions_test.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/routers/api/v1/repo/action.go b/routers/api/v1/repo/action.go index 1efb3c2449..716a0b7fdb 100644 --- a/routers/api/v1/repo/action.go +++ b/routers/api/v1/repo/action.go @@ -807,7 +807,7 @@ func DispatchWorkflow(ctx *context.APIContext) { if opt.ReturnRunInfo { ctx.JSON(http.StatusCreated, workflowRun) } else { - ctx.JSON(http.StatusNoContent, nil) + ctx.Status(http.StatusNoContent) } } diff --git a/tests/integration/api_repo_actions_test.go b/tests/integration/api_repo_actions_test.go index 056adce698..1875f3269e 100644 --- a/tests/integration/api_repo_actions_test.go +++ b/tests/integration/api_repo_actions_test.go @@ -5,6 +5,7 @@ package integration import ( "fmt" + "io" "net/http" "net/url" "strings" @@ -21,6 +22,7 @@ import ( "forgejo.org/tests" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestActionsAPISearchActionJobs_RepoRunner(t *testing.T) { @@ -99,6 +101,24 @@ jobs: assert.NotZero(t, run.ID) assert.NotZero(t, run.RunNumber) assert.Len(t, run.Jobs, 2) + + req = NewRequestWithJSON( + t, + http.MethodPost, + fmt.Sprintf( + "/api/v1/repos/%s/%s/actions/workflows/%s/dispatches", + repo.OwnerName, repo.Name, workflowName, + ), + &api.DispatchWorkflowOption{ + Ref: repo.DefaultBranch, + ReturnRunInfo: false, + }, + ) + req.AddTokenAuth(token) + res = MakeRequest(t, req, http.StatusNoContent) + body, err := io.ReadAll(res.Body) + require.NoError(t, err) + assert.Empty(t, body) // 204 No Content doesn't support a body, so should be empty }) }