mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-10-10 19:32:02 +00:00
feat: make upload URL compatible with GitHub API (#9285)
Adds new a function, `AcceptsGithubResponse`, to the API router context struct to check if the requests accepts a Github response. Although Forgejo API will never be compatible with the Github API, historically Forgejo's API has been designed to follow that of Github closely and we know that a lot of tooling that uses the Github API can be used against the Forgejo API with little to no problem. As a meet in the middle solution, this function can be used to respond with a more appropriate response that follows the Github API. This allows Forgejo to avoid breaking compatibility with existing users of the API and allows the API to be oh so slightly more compatible with that of Github for API clients that expect a Github response. Because the `upload_url` field was added purely to match the Github API (forgejo/forgejo#580), it is fair to actually make it compatible with how the Github API intended it to be and that is by adding `{?name,label}` which is used by Github's Oktokit. Only add `{?name,label}` when Forgejo knows the request accepts a Github response. This avoids breaking the API compatibility with non-Github API clients. Resolves Codeberg/Community#2132 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9285 Reviewed-by: 0ko <0ko@noreply.codeberg.org> Reviewed-by: oliverpool <oliverpool@noreply.codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
parent
5c6645a8af
commit
389b32f51a
12 changed files with 80 additions and 18 deletions
|
@ -4,6 +4,7 @@
|
|||
package context
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
@ -50,3 +51,26 @@ func TestGenAPILinks(t *testing.T) {
|
|||
assert.Equal(t, links, response)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAcceptsGithubResponse(t *testing.T) {
|
||||
t.Run("Normal", func(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
resp := httptest.NewRecorder()
|
||||
base, baseCleanUp := NewBaseContext(resp, req)
|
||||
t.Cleanup(baseCleanUp)
|
||||
ctx := &APIContext{Base: base}
|
||||
|
||||
assert.False(t, ctx.AcceptsGithubResponse())
|
||||
})
|
||||
|
||||
t.Run("Accepts Github", func(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
req.Header.Add("Accept", "application/vnd.github+json")
|
||||
resp := httptest.NewRecorder()
|
||||
base, baseCleanUp := NewBaseContext(resp, req)
|
||||
t.Cleanup(baseCleanUp)
|
||||
ctx := &APIContext{Base: base}
|
||||
|
||||
assert.True(t, ctx.AcceptsGithubResponse())
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue