Refactor integration test DecodeJSON calls to use generic return value (#37432)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: wxiaoguang <2114189+wxiaoguang@users.noreply.github.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Copilot
2026-04-26 14:57:07 +00:00
committed by GitHub
parent 99cd4f6b22
commit 2671b997f2
105 changed files with 569 additions and 930 deletions
@@ -194,8 +194,7 @@ func TestAPICreateFile(t *testing.T) {
lastCommitterWhen: lastCommit.Committer.When,
lastAuthorWhen: lastCommit.Author.When,
})
var fileResponse api.FileResponse
DecodeJSON(t, resp, &fileResponse)
fileResponse := DecodeJSON(t, resp, &api.FileResponse{})
normalizeFileContentResponseCommitTime(fileResponse.Content)
assert.Equal(t, expectedFileResponse.Content, fileResponse.Content)
assert.Equal(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
@@ -217,8 +216,7 @@ func TestAPICreateFile(t *testing.T) {
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user2.Name, repo1.Name, treePath), &createFileOptions).
AddTokenAuth(token2)
resp := MakeRequest(t, req, http.StatusCreated)
var fileResponse api.FileResponse
DecodeJSON(t, resp, &fileResponse)
fileResponse := DecodeJSON(t, resp, &api.FileResponse{})
expectedSHA := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
expectedHTMLURL := fmt.Sprintf(setting.AppURL+"user2/repo1/src/branch/new_branch/new/file%d.txt", fileID)
expectedDownloadURL := fmt.Sprintf(setting.AppURL+"user2/repo1/raw/branch/new_branch/new/file%d.txt", fileID)
@@ -235,7 +233,7 @@ func TestAPICreateFile(t *testing.T) {
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user2.Name, repo1.Name, treePath), &createFileOptions).
AddTokenAuth(token2)
resp = MakeRequest(t, req, http.StatusCreated)
DecodeJSON(t, resp, &fileResponse)
fileResponse = DecodeJSON(t, resp, &api.FileResponse{})
expectedMessage := "Add " + treePath + "\n"
assert.Equal(t, expectedMessage, fileResponse.Commit.Message)
@@ -245,12 +243,11 @@ func TestAPICreateFile(t *testing.T) {
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user2.Name, repo1.Name, treePath), &createFileOptions).
AddTokenAuth(token2)
resp = MakeRequest(t, req, http.StatusUnprocessableEntity)
expectedAPIError := context.APIError{
expectedAPIError := &context.APIError{
Message: "repository file already exists [path: " + treePath + "]",
URL: setting.API.SwaggerURL,
}
var apiError context.APIError
DecodeJSON(t, resp, &apiError)
apiError := DecodeJSON(t, resp, &context.APIError{})
assert.Equal(t, expectedAPIError, apiError)
// Test creating a file in repo1 by user4 who does not have write access
@@ -320,7 +317,7 @@ func TestAPICreateFile(t *testing.T) {
lastCommitterWhen: latestCommit.Committer.When,
lastAuthorWhen: latestCommit.Author.When,
})
DecodeJSON(t, resp, &fileResponse)
fileResponse = DecodeJSON(t, resp, &api.FileResponse{})
normalizeFileContentResponseCommitTime(fileResponse.Content)
assert.Equal(t, expectedFileResponse.Content, fileResponse.Content)
assert.Equal(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)