mirror of
https://github.com/go-gitea/gitea.git
synced 2026-09-16 01:43:24 +09:00
enhance(api): add GitHub-compatible /repos/{owner}/{repo}/commits/{ref} endpoint (#38770)
Gitea currently exposes the endpoint:
`GET /repos/{owner}/{repo}/git/commits/{sha}`
to retrieve a single commit. However, GitHub provides the equivalent
endpoint as:
`GET /repos/{owner}/{repo}/commits/{ref}`
Applications integrating with both GitHub and Gitea must implement
platform-specific logic to use different endpoints, reducing API
compatibility.
Fixes #38225
---------
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Giteabot <teabot@gitea.io>
This commit is contained in:
co-authored by
wxiaoguang
Giteabot
parent
c98a1597c9
commit
c46f0d545e
@@ -1484,7 +1484,11 @@ func Routes() *web.Router {
|
|||||||
Post(reqToken(), reqRepoWriter(unit.TypeCode), bind(api.CreateStatusOption{}), repo.NewCommitStatus)
|
Post(reqToken(), reqRepoWriter(unit.TypeCode), bind(api.CreateStatusOption{}), repo.NewCommitStatus)
|
||||||
}, reqRepoReader(unit.TypeCode))
|
}, reqRepoReader(unit.TypeCode))
|
||||||
m.Group("/commits", func() {
|
m.Group("/commits", func() {
|
||||||
m.Get("", context.ReferencesGitRepo(), repo.GetAllCommits)
|
m.Group("", func() {
|
||||||
|
m.Get("", repo.GetAllCommits)
|
||||||
|
m.Get("/{sha}", repo.GetSingleCommit) // GitHub-compatible endpoint
|
||||||
|
m.Get("/{sha}.{diffType:diff|patch}", repo.DownloadCommitDiffOrPatch)
|
||||||
|
}, context.ReferencesGitRepo(true))
|
||||||
m.PathGroup("/*", func(g *web.RouterPathGroup) {
|
m.PathGroup("/*", func(g *web.RouterPathGroup) {
|
||||||
// Mis-configured reverse proxy might decode the `%2F` to slash ahead, so we need to support both formats (escaped, unescaped) here.
|
// Mis-configured reverse proxy might decode the `%2F` to slash ahead, so we need to support both formats (escaped, unescaped) here.
|
||||||
// It also matches GitHub's behavior
|
// It also matches GitHub's behavior
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ import (
|
|||||||
func GetSingleCommit(ctx *context.APIContext) {
|
func GetSingleCommit(ctx *context.APIContext) {
|
||||||
// swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha} repository repoGetSingleCommit
|
// swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha} repository repoGetSingleCommit
|
||||||
// ---
|
// ---
|
||||||
// summary: Get a single commit from a repository
|
// summary: Get a single commit from a repository, it has a GitHub-compatible alias "/repos/{owner}/{repo}/commits/{ref}"
|
||||||
// produces:
|
// produces:
|
||||||
// - application/json
|
// - application/json
|
||||||
// parameters:
|
// parameters:
|
||||||
|
|||||||
+1
-1
@@ -20470,7 +20470,7 @@
|
|||||||
"$ref": "#/components/responses/validationError"
|
"$ref": "#/components/responses/validationError"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"summary": "Get a single commit from a repository",
|
"summary": "Get a single commit from a repository, it has a GitHub-compatible alias \"/repos/{owner}/{repo}/commits/{ref}\"",
|
||||||
"tags": [
|
"tags": [
|
||||||
"repository"
|
"repository"
|
||||||
]
|
]
|
||||||
|
|||||||
+1
-1
@@ -9067,7 +9067,7 @@
|
|||||||
"tags": [
|
"tags": [
|
||||||
"repository"
|
"repository"
|
||||||
],
|
],
|
||||||
"summary": "Get a single commit from a repository",
|
"summary": "Get a single commit from a repository, it has a GitHub-compatible alias \"/repos/{owner}/{repo}/commits/{ref}\"",
|
||||||
"operationId": "repoGetSingleCommit",
|
"operationId": "repoGetSingleCommit",
|
||||||
"parameters": [
|
"parameters": [
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -26,22 +26,15 @@ func compareCommitFiles(t *testing.T, expect []string, files []*api.CommitAffect
|
|||||||
|
|
||||||
func TestAPIReposGitCommits(t *testing.T) {
|
func TestAPIReposGitCommits(t *testing.T) {
|
||||||
defer tests.PrepareTestEnv(t)()
|
defer tests.PrepareTestEnv(t)()
|
||||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||||
// Login as User2.
|
session := loginUser(t, user2.Name)
|
||||||
session := loginUser(t, user.Name)
|
|
||||||
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
|
||||||
|
|
||||||
// check invalid requests
|
// check invalid requests
|
||||||
req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/12345", user.Name).
|
req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/..", user2.Name).AddTokenAuth(token)
|
||||||
AddTokenAuth(token)
|
|
||||||
MakeRequest(t, req, http.StatusNotFound)
|
|
||||||
|
|
||||||
req = NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/..", user.Name).
|
|
||||||
AddTokenAuth(token)
|
|
||||||
MakeRequest(t, req, http.StatusUnprocessableEntity)
|
MakeRequest(t, req, http.StatusUnprocessableEntity)
|
||||||
|
|
||||||
req = NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/branch-not-exist", user.Name).
|
req = NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/ref-not-exist", user2.Name).AddTokenAuth(token)
|
||||||
AddTokenAuth(token)
|
|
||||||
MakeRequest(t, req, http.StatusNotFound)
|
MakeRequest(t, req, http.StatusNotFound)
|
||||||
|
|
||||||
for _, ref := range [...]string{
|
for _, ref := range [...]string{
|
||||||
@@ -50,8 +43,10 @@ func TestAPIReposGitCommits(t *testing.T) {
|
|||||||
"65f1", // short sha
|
"65f1", // short sha
|
||||||
"65f1bf27bc3bf70f64657658635e66094edbcb4d", // full sha
|
"65f1bf27bc3bf70f64657658635e66094edbcb4d", // full sha
|
||||||
} {
|
} {
|
||||||
req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/%s", user.Name, ref).
|
req = NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/%s", user2.Name, ref).AddTokenAuth(token)
|
||||||
AddTokenAuth(token)
|
MakeRequest(t, req, http.StatusOK)
|
||||||
|
// GitHub-compatible
|
||||||
|
req = NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/commits/%s", user2.Name, ref).AddTokenAuth(token)
|
||||||
MakeRequest(t, req, http.StatusOK)
|
MakeRequest(t, req, http.StatusOK)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user