feat: Allow HEAD /repos/{owner}/{repo}/raw/{filepath}

- Make the API route `/repos/{owner}/{repo}/raw/{filepath}` accept
`HEAD` as a method, this can then be used to confirm if a specific file
exists or not.
- Resolves forgejo/forgejo#6992
- Added integration test.
This commit is contained in:
Gusted 2025-02-19 15:40:27 +01:00
parent d81baf21e9
commit f41d20ec67
No known key found for this signature in database
GPG key ID: FD821B732837125F
2 changed files with 9 additions and 1 deletions

View file

@ -1143,7 +1143,7 @@ func Routes() *web.Route {
Put(reqAdmin(), repo.AddTeam).
Delete(reqAdmin(), repo.DeleteTeam)
}, reqToken())
m.Get("/raw/*", context.ReferencesGitRepo(), context.RepoRefForAPI, reqRepoReader(unit.TypeCode), repo.GetRawFile)
m.Methods("GET,HEAD", "/raw/*", context.ReferencesGitRepo(), context.RepoRefForAPI, reqRepoReader(unit.TypeCode), repo.GetRawFile)
m.Get("/media/*", context.ReferencesGitRepo(), context.RepoRefForAPI, reqRepoReader(unit.TypeCode), repo.GetRawFileOrLFS)
m.Get("/archive/*", reqRepoReader(unit.TypeCode), repo.GetArchive)
if !setting.Repository.DisableForks {

View file

@ -18,6 +18,7 @@ import (
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
repo_service "code.gitea.io/gitea/services/repository"
"code.gitea.io/gitea/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -192,5 +193,12 @@ func TestAPIGetContentsRefFormats(t *testing.T) {
raw, err = io.ReadAll(resp.Body)
require.NoError(t, err)
assert.EqualValues(t, content, string(raw))
t.Run("HEAD request", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
MakeRequest(t, NewRequest(t, http.MethodHead, noRef), http.StatusOK)
MakeRequest(t, NewRequest(t, http.MethodHead, "api/v1/repos/user2/repo1/raw/file-does-not-exist"), http.StatusNotFound)
})
})
}