Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API to get single commit via SHA and Ref #10915

Merged
merged 12 commits into from
Apr 8, 2020
Next Next commit
GET /repos/:owner/:repo/commits/:ref
  • Loading branch information
6543 committed Apr 5, 2020
commit d3bb560511058a2056efdeebe1222e01b838f14e
1 change: 0 additions & 1 deletion modules/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,6 @@ func ToCommitUser(sig *git.Signature) *api.CommitUser {
func ToCommitMeta(repo *models.Repository, tag *git.Tag) *api.CommitMeta {
return &api.CommitMeta{
SHA: tag.Object.String(),
// TODO: Add the /commits API endpoint and use it here (https://developer.github.com/v3/repos/commits/#get-a-single-commit)
URL: util.URLJoin(repo.APIURL(), "git/commits", tag.ID.String()),
}
}
Expand Down
4 changes: 2 additions & 2 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,14 +798,14 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Group("/commits", func() {
m.Get("", repo.GetAllCommits)
m.Group("/:ref", func() {
// TODO: Add m.Get("") for single commit (https://developer.github.com/v3/repos/commits/#get-a-single-commit)
m.Get("", repo.GetSingleCommitByRef)
m.Get("/status", repo.GetCombinedCommitStatusByRef)
m.Get("/statuses", repo.GetCommitStatusesByRef)
})
}, reqRepoReader(models.UnitTypeCode))
m.Group("/git", func() {
m.Group("/commits", func() {
m.Get("/:sha", repo.GetSingleCommit)
m.Get("/:sha", repo.GetSingleCommitBySHA)
})
m.Get("/refs", repo.GetGitAllRefs)
m.Get("/refs/*", repo.GetGitRefs)
Expand Down
57 changes: 52 additions & 5 deletions routers/api/v1/repo/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
"code.gitea.io/gitea/routers/api/v1/utils"
)

// GetSingleCommit get a commit via
func GetSingleCommit(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha} repository repoGetSingleCommit
// GetSingleCommitBySHA get a commit via sha
func GetSingleCommitBySHA(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha} repository repoGetSingleCommitBySHA
// ---
// summary: Get a single commit from a repository
// produces:
Expand All @@ -48,13 +48,61 @@ func GetSingleCommit(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"

sha := ctx.Params(":sha")
if len(sha) == 0 {
6543 marked this conversation as resolved.
Show resolved Hide resolved
ctx.Error(http.StatusBadRequest, "ref not given", nil)
return
}
getCommit(ctx, sha)
}

// GetSingleCommitByRef get a commit via ref
func GetSingleCommitByRef(ctx *context.APIContext) {
6543 marked this conversation as resolved.
Show resolved Hide resolved
// swagger:operation GET /repos/{owner}/{repo}/commits/{ref} repository repoGetSingleCommitByRef
// ---
// summary: Get a single commit from a repository
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: ref
// in: path
// description: a git ref
// type: string
// required: true
// responses:
// "200":
// "$ref": "#/responses/Commit"
// "400":
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/notFound"

ref := ctx.Params("ref")
if len(ref) == 0 {
6543 marked this conversation as resolved.
Show resolved Hide resolved
ctx.Error(http.StatusBadRequest, "ref not given", nil)
return
}
getCommit(ctx, ref)
}

func getCommit(ctx *context.APIContext, identifier string) {
gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
if err != nil {
ctx.ServerError("OpenRepository", err)
return
}
defer gitRepo.Close()
commit, err := gitRepo.GetCommit(ctx.Params(":sha"))
commit, err := gitRepo.GetCommit(identifier)
if err != nil {
ctx.NotFoundOrServerError("GetCommit", git.IsErrNotExist, err)
return
Expand All @@ -65,7 +113,6 @@ func GetSingleCommit(ctx *context.APIContext) {
ctx.ServerError("toCommit", err)
return
}

ctx.JSON(http.StatusOK, json)
}

Expand Down
48 changes: 47 additions & 1 deletion templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2511,6 +2511,52 @@
}
}
},
"/repos/{owner}/{repo}/commits/{ref}": {
"get": {
"produces": [
"application/json"
],
"tags": [
"repository"
],
"summary": "Get a single commit from a repository",
"operationId": "repoGetSingleCommitByRef",
"parameters": [
{
"type": "string",
"description": "owner of the repo",
"name": "owner",
"in": "path",
"required": true
},
{
"type": "string",
"description": "name of the repo",
"name": "repo",
"in": "path",
"required": true
},
{
"type": "string",
"description": "a git ref",
"name": "ref",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"$ref": "#/responses/Commit"
},
"400": {
"$ref": "#/responses/error"
},
"404": {
"$ref": "#/responses/notFound"
}
}
}
},
"/repos/{owner}/{repo}/commits/{ref}/statuses": {
"get": {
"produces": [
Expand Down Expand Up @@ -2976,7 +3022,7 @@
"repository"
],
"summary": "Get a single commit from a repository",
"operationId": "repoGetSingleCommit",
"operationId": "repoGetSingleCommitBySHA",
"parameters": [
{
"type": "string",
Expand Down