Skip to content

Commit 32b8172

Browse files
CirnoTzeripath
andauthored
Consolidate API for getting single commit (#11368)
* Allow Git ref for /repos/{owner}/{repo}/git/commits/{sha} * Consolidate API for getting single commit * Fix tests and do it differently Co-authored-by: zeripath <[email protected]>
1 parent 59b9b77 commit 32b8172

File tree

4 files changed

+15
-119
lines changed

4 files changed

+15
-119
lines changed

integrations/api_repo_git_commits_test.go

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,14 @@ func TestAPIReposGitCommits(t *testing.T) {
2121
session := loginUser(t, user.Name)
2222
token := getTokenForLoggedInUser(t, session)
2323

24-
//check invalid requests for GetCommitsBySHA
25-
req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/master?token="+token, user.Name)
26-
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
27-
28-
req = NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/12345?token="+token, user.Name)
24+
// check invalid requests
25+
req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/12345?token="+token, user.Name)
2926
session.MakeRequest(t, req, http.StatusNotFound)
3027

31-
//check invalid requests for GetCommitsByRef
32-
req = NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/commits/..?token="+token, user.Name)
28+
req = NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/..?token="+token, user.Name)
3329
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
3430

35-
req = NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/commits/branch-not-exist?token="+token, user.Name)
31+
req = NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/branch-not-exist?token="+token, user.Name)
3632
session.MakeRequest(t, req, http.StatusNotFound)
3733

3834
for _, ref := range [...]string{
@@ -41,20 +37,8 @@ func TestAPIReposGitCommits(t *testing.T) {
4137
"65f1", // short sha
4238
"65f1bf27bc3bf70f64657658635e66094edbcb4d", // full sha
4339
} {
44-
req = NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/commits/%s?token="+token, user.Name, ref)
45-
resp := session.MakeRequest(t, req, http.StatusOK)
46-
commitByRef := new(api.Commit)
47-
DecodeJSON(t, resp, commitByRef)
48-
assert.Len(t, commitByRef.SHA, 40)
49-
assert.EqualValues(t, commitByRef.SHA, commitByRef.RepoCommit.Tree.SHA)
50-
req = NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/%s?token="+token, user.Name, commitByRef.SHA)
51-
resp = session.MakeRequest(t, req, http.StatusOK)
52-
commitBySHA := new(api.Commit)
53-
DecodeJSON(t, resp, commitBySHA)
54-
55-
assert.EqualValues(t, commitByRef.SHA, commitBySHA.SHA)
56-
assert.EqualValues(t, commitByRef.HTMLURL, commitBySHA.HTMLURL)
57-
assert.EqualValues(t, commitByRef.RepoCommit.Message, commitBySHA.RepoCommit.Message)
40+
req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/%s?token="+token, user.Name, ref)
41+
session.MakeRequest(t, req, http.StatusOK)
5842
}
5943
}
6044

routers/api/v1/api.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -817,14 +817,13 @@ func RegisterRoutes(m *macaron.Macaron) {
817817
m.Group("/commits", func() {
818818
m.Get("", repo.GetAllCommits)
819819
m.Group("/:ref", func() {
820-
m.Get("", repo.GetSingleCommitByRef)
821820
m.Get("/status", repo.GetCombinedCommitStatusByRef)
822821
m.Get("/statuses", repo.GetCommitStatusesByRef)
823822
})
824823
}, reqRepoReader(models.UnitTypeCode))
825824
m.Group("/git", func() {
826825
m.Group("/commits", func() {
827-
m.Get("/:sha", repo.GetSingleCommitBySHA)
826+
m.Get("/:sha", repo.GetSingleCommit)
828827
})
829828
m.Get("/refs", repo.GetGitAllRefs)
830829
m.Get("/refs/*", repo.GetGitRefs)

routers/api/v1/repo/commits.go

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121
"code.gitea.io/gitea/routers/api/v1/utils"
2222
)
2323

24-
// GetSingleCommitBySHA get a commit via sha
25-
func GetSingleCommitBySHA(ctx *context.APIContext) {
26-
// swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha} repository repoGetSingleCommitBySHA
24+
// GetSingleCommit get a commit via sha
25+
func GetSingleCommit(ctx *context.APIContext) {
26+
// swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha} repository repoGetSingleCommit
2727
// ---
2828
// summary: Get a single commit from a repository
2929
// produces:
@@ -41,7 +41,7 @@ func GetSingleCommitBySHA(ctx *context.APIContext) {
4141
// required: true
4242
// - name: sha
4343
// in: path
44-
// description: the commit hash
44+
// description: a git ref or commit sha
4545
// type: string
4646
// required: true
4747
// responses:
@@ -53,54 +53,13 @@ func GetSingleCommitBySHA(ctx *context.APIContext) {
5353
// "$ref": "#/responses/notFound"
5454

5555
sha := ctx.Params(":sha")
56-
if !git.SHAPattern.MatchString(sha) {
57-
ctx.Error(http.StatusUnprocessableEntity, "no valid sha", fmt.Sprintf("no valid sha: %s", sha))
56+
if (validation.GitRefNamePatternInvalid.MatchString(sha) || !validation.CheckGitRefAdditionalRulesValid(sha)) && !git.SHAPattern.MatchString(sha) {
57+
ctx.Error(http.StatusUnprocessableEntity, "no valid ref or sha", fmt.Sprintf("no valid ref or sha: %s", sha))
5858
return
5959
}
6060
getCommit(ctx, sha)
6161
}
6262

63-
// GetSingleCommitByRef get a commit via ref
64-
func GetSingleCommitByRef(ctx *context.APIContext) {
65-
// swagger:operation GET /repos/{owner}/{repo}/commits/{ref} repository repoGetSingleCommitByRef
66-
// ---
67-
// summary: Get a single commit from a repository
68-
// produces:
69-
// - application/json
70-
// parameters:
71-
// - name: owner
72-
// in: path
73-
// description: owner of the repo
74-
// type: string
75-
// required: true
76-
// - name: repo
77-
// in: path
78-
// description: name of the repo
79-
// type: string
80-
// required: true
81-
// - name: ref
82-
// in: path
83-
// description: a git ref
84-
// type: string
85-
// required: true
86-
// responses:
87-
// "200":
88-
// "$ref": "#/responses/Commit"
89-
// "422":
90-
// "$ref": "#/responses/validationError"
91-
// "404":
92-
// "$ref": "#/responses/notFound"
93-
94-
ref := ctx.Params("ref")
95-
96-
if validation.GitRefNamePatternInvalid.MatchString(ref) || !validation.CheckGitRefAdditionalRulesValid(ref) {
97-
ctx.Error(http.StatusUnprocessableEntity, "no valid sha", fmt.Sprintf("no valid ref: %s", ref))
98-
return
99-
}
100-
101-
getCommit(ctx, ref)
102-
}
103-
10463
func getCommit(ctx *context.APIContext, identifier string) {
10564
gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
10665
if err != nil {

templates/swagger/v1_json.tmpl

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2549,52 +2549,6 @@
25492549
}
25502550
}
25512551
},
2552-
"/repos/{owner}/{repo}/commits/{ref}": {
2553-
"get": {
2554-
"produces": [
2555-
"application/json"
2556-
],
2557-
"tags": [
2558-
"repository"
2559-
],
2560-
"summary": "Get a single commit from a repository",
2561-
"operationId": "repoGetSingleCommitByRef",
2562-
"parameters": [
2563-
{
2564-
"type": "string",
2565-
"description": "owner of the repo",
2566-
"name": "owner",
2567-
"in": "path",
2568-
"required": true
2569-
},
2570-
{
2571-
"type": "string",
2572-
"description": "name of the repo",
2573-
"name": "repo",
2574-
"in": "path",
2575-
"required": true
2576-
},
2577-
{
2578-
"type": "string",
2579-
"description": "a git ref",
2580-
"name": "ref",
2581-
"in": "path",
2582-
"required": true
2583-
}
2584-
],
2585-
"responses": {
2586-
"200": {
2587-
"$ref": "#/responses/Commit"
2588-
},
2589-
"404": {
2590-
"$ref": "#/responses/notFound"
2591-
},
2592-
"422": {
2593-
"$ref": "#/responses/validationError"
2594-
}
2595-
}
2596-
}
2597-
},
25982552
"/repos/{owner}/{repo}/commits/{ref}/statuses": {
25992553
"get": {
26002554
"produces": [
@@ -3075,7 +3029,7 @@
30753029
"repository"
30763030
],
30773031
"summary": "Get a single commit from a repository",
3078-
"operationId": "repoGetSingleCommitBySHA",
3032+
"operationId": "repoGetSingleCommit",
30793033
"parameters": [
30803034
{
30813035
"type": "string",
@@ -3093,7 +3047,7 @@
30933047
},
30943048
{
30953049
"type": "string",
3096-
"description": "the commit hash",
3050+
"description": "a git ref or commit sha",
30973051
"name": "sha",
30983052
"in": "path",
30993053
"required": true

0 commit comments

Comments
 (0)