aboutsummaryrefslogtreecommitdiffstats
path: root/routers/api
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2019-02-03 11:35:17 +0800
committertechknowlogick <matti@mdranta.net>2019-02-02 22:35:17 -0500
commitecefa9e724460deb70b97dd7c52fc8f4db94be93 (patch)
tree0d2ea72f0a682b7ab775879b5737782e4c000e69 /routers/api
parent3d91bb2f2dc8584b76a49a1a40c3f688c21380f5 (diff)
downloadgitea-ecefa9e724460deb70b97dd7c52fc8f4db94be93.tar.gz
gitea-ecefa9e724460deb70b97dd7c52fc8f4db94be93.zip
Add single commit API support (#5843)
* add single commit API support
Diffstat (limited to 'routers/api')
-rw-r--r--routers/api/v1/api.go3
-rw-r--r--routers/api/v1/repo/commits.go119
-rw-r--r--routers/api/v1/swagger/repo.go7
3 files changed, 129 insertions, 0 deletions
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go
index 7e7bf6a50b..55f5c66290 100644
--- a/routers/api/v1/api.go
+++ b/routers/api/v1/api.go
@@ -617,6 +617,9 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Get("/statuses", repo.GetCommitStatusesByRef)
}, reqRepoReader(models.UnitTypeCode))
m.Group("/git", func() {
+ m.Group("/commits", func() {
+ m.Get("/:sha", repo.GetSingleCommit)
+ })
m.Get("/refs", repo.GetGitAllRefs)
m.Get("/refs/*", repo.GetGitRefs)
m.Combo("/trees/:sha", context.RepoRef()).Get(repo.GetTree)
diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go
new file mode 100644
index 0000000000..a4cf5037d7
--- /dev/null
+++ b/routers/api/v1/repo/commits.go
@@ -0,0 +1,119 @@
+// Copyright 2018 The Gogs Authors. All rights reserved.
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package repo
+
+import (
+ "time"
+
+ "code.gitea.io/git"
+ api "code.gitea.io/sdk/gitea"
+
+ "code.gitea.io/gitea/models"
+ "code.gitea.io/gitea/modules/context"
+ "code.gitea.io/gitea/modules/setting"
+)
+
+// GetSingleCommit get a commit via
+func GetSingleCommit(ctx *context.APIContext) {
+ // swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha} repository repoGetSingleCommit
+ // ---
+ // 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: sha
+ // in: path
+ // description: the commit hash
+ // type: string
+ // required: true
+ // responses:
+ // "200":
+ // "$ref": "#/responses/Commit"
+ // "404":
+ // "$ref": "#/responses/notFound"
+
+ gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
+ if err != nil {
+ ctx.ServerError("OpenRepository", err)
+ return
+ }
+ commit, err := gitRepo.GetCommit(ctx.Params(":sha"))
+ if err != nil {
+ ctx.NotFoundOrServerError("GetCommit", git.IsErrNotExist, err)
+ return
+ }
+
+ // Retrieve author and committer information
+ var apiAuthor, apiCommitter *api.User
+ author, err := models.GetUserByEmail(commit.Author.Email)
+ if err != nil && !models.IsErrUserNotExist(err) {
+ ctx.ServerError("Get user by author email", err)
+ return
+ } else if err == nil {
+ apiAuthor = author.APIFormat()
+ }
+ // Save one query if the author is also the committer
+ if commit.Committer.Email == commit.Author.Email {
+ apiCommitter = apiAuthor
+ } else {
+ committer, err := models.GetUserByEmail(commit.Committer.Email)
+ if err != nil && !models.IsErrUserNotExist(err) {
+ ctx.ServerError("Get user by committer email", err)
+ return
+ } else if err == nil {
+ apiCommitter = committer.APIFormat()
+ }
+ }
+
+ // Retrieve parent(s) of the commit
+ apiParents := make([]*api.CommitMeta, commit.ParentCount())
+ for i := 0; i < commit.ParentCount(); i++ {
+ sha, _ := commit.ParentID(i)
+ apiParents[i] = &api.CommitMeta{
+ URL: ctx.Repo.Repository.APIURL() + "/git/commits/" + sha.String(),
+ SHA: sha.String(),
+ }
+ }
+
+ ctx.JSON(200, &api.Commit{
+ CommitMeta: &api.CommitMeta{
+ URL: setting.AppURL + ctx.Link[1:],
+ SHA: commit.ID.String(),
+ },
+ HTMLURL: ctx.Repo.Repository.HTMLURL() + "/commits/" + commit.ID.String(),
+ RepoCommit: &api.RepoCommit{
+ URL: setting.AppURL + ctx.Link[1:],
+ Author: &api.CommitUser{
+ Name: commit.Author.Name,
+ Email: commit.Author.Email,
+ Date: commit.Author.When.Format(time.RFC3339),
+ },
+ Committer: &api.CommitUser{
+ Name: commit.Committer.Name,
+ Email: commit.Committer.Email,
+ Date: commit.Committer.When.Format(time.RFC3339),
+ },
+ Message: commit.Summary(),
+ Tree: &api.CommitMeta{
+ URL: ctx.Repo.Repository.APIURL() + "/trees/" + commit.ID.String(),
+ SHA: commit.ID.String(),
+ },
+ },
+ Author: apiAuthor,
+ Committer: apiCommitter,
+ Parents: apiParents,
+ })
+}
diff --git a/routers/api/v1/swagger/repo.go b/routers/api/v1/swagger/repo.go
index 0c9f95f962..5b930e295e 100644
--- a/routers/api/v1/swagger/repo.go
+++ b/routers/api/v1/swagger/repo.go
@@ -140,3 +140,10 @@ type swaggerGitTreeResponse struct {
//in: body
Body api.GitTreeResponse `json:"body"`
}
+
+// Commit
+// swagger:response Commit
+type swaggerCommit struct {
+ //in: body
+ Body api.Commit `json:"body"`
+}