diff options
author | 6543 <6543@obermui.de> | 2020-04-08 04:54:46 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-07 22:54:46 -0400 |
commit | 3d63caa54245d87dd057d4e853bb5dc7fc39e7db (patch) | |
tree | 2fbbb48a39cff157f8cf171b0dad51af366c0908 /routers | |
parent | 71979d9663d8e43b772c37f2a79af5b8911df661 (diff) | |
download | gitea-3d63caa54245d87dd057d4e853bb5dc7fc39e7db.tar.gz gitea-3d63caa54245d87dd057d4e853bb5dc7fc39e7db.zip |
[API] Get a single commit via Ref (#10915)
* GET /repos/:owner/:repo/commits/:ref
* add Validation Checks
* Fix & Extend TEST
* add two new tast cases
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/api.go | 4 | ||||
-rw-r--r-- | routers/api/v1/repo/commits.go | 63 |
2 files changed, 60 insertions, 7 deletions
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index e5bb98033b..8fc1eeefd1 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -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) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index f7da1698dc..aa949aa9ec 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -6,6 +6,7 @@ package repo import ( + "fmt" "math" "net/http" "strconv" @@ -16,12 +17,13 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/modules/validation" "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: @@ -45,16 +47,68 @@ func GetSingleCommit(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/Commit" + // "422": + // "$ref": "#/responses/validationError" // "404": // "$ref": "#/responses/notFound" + sha := ctx.Params(":sha") + if !git.SHAPattern.MatchString(sha) { + ctx.Error(http.StatusUnprocessableEntity, "no valid sha", fmt.Sprintf("no valid sha: %s", sha)) + return + } + getCommit(ctx, sha) +} + +// GetSingleCommitByRef get a commit via ref +func GetSingleCommitByRef(ctx *context.APIContext) { + // 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" + // "422": + // "$ref": "#/responses/validationError" + // "404": + // "$ref": "#/responses/notFound" + + ref := ctx.Params("ref") + + if validation.GitRefNamePatternInvalid.MatchString(ref) || !validation.CheckGitRefAdditionalRulesValid(ref) { + ctx.Error(http.StatusUnprocessableEntity, "no valid sha", fmt.Sprintf("no valid ref: %s", ref)) + 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 @@ -65,7 +119,6 @@ func GetSingleCommit(ctx *context.APIContext) { ctx.ServerError("toCommit", err) return } - ctx.JSON(http.StatusOK, json) } |