aboutsummaryrefslogtreecommitdiff
path: root/routes/api
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2018-12-15 00:24:41 -0500
committerUnknwon <u@gogs.io>2018-12-15 00:24:41 -0500
commitee82d35ed807b32aaf04a505e5b5260f20aaf641 (patch)
tree56eda61eccb6bf1317ba1fc260a575a99d935ece /routes/api
parent8bca30cfe44bd443906dc7a6d1491a94220a1dc1 (diff)
api: add GetSingleCommit (#5546)
Diffstat (limited to 'routes/api')
-rw-r--r--routes/api/v1/api.go5
-rw-r--r--routes/api/v1/repo/commits.go91
2 files changed, 96 insertions, 0 deletions
diff --git a/routes/api/v1/api.go b/routes/api/v1/api.go
index 2fb2ebb0..ccecc6a7 100644
--- a/routes/api/v1/api.go
+++ b/routes/api/v1/api.go
@@ -249,6 +249,11 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Get("", repo.ListBranches)
m.Get("/*", repo.GetBranch)
})
+
+ m.Group("/commits", func() {
+ m.Get("/:sha", repo.GetSingleCommit)
+ })
+
m.Group("/keys", func() {
m.Combo("").Get(repo.ListDeployKeys).
Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey)
diff --git a/routes/api/v1/repo/commits.go b/routes/api/v1/repo/commits.go
new file mode 100644
index 00000000..a856d37f
--- /dev/null
+++ b/routes/api/v1/repo/commits.go
@@ -0,0 +1,91 @@
+// Copyright 2018 The Gogs 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"
+
+ "github.com/gogs/git-module"
+ api "github.com/gogs/go-gogs-client"
+
+ "github.com/gogs/gogs/models"
+ "github.com/gogs/gogs/models/errors"
+ "github.com/gogs/gogs/pkg/context"
+ "github.com/gogs/gogs/pkg/setting"
+)
+
+func GetSingleCommit(c *context.APIContext) {
+ gitRepo, err := git.OpenRepository(c.Repo.Repository.RepoPath())
+ if err != nil {
+ c.ServerError("OpenRepository", err)
+ return
+ }
+ commit, err := gitRepo.GetCommit(c.Params(":sha"))
+ if err != nil {
+ c.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 && !errors.IsUserNotExist(err) {
+ c.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 && !errors.IsUserNotExist(err) {
+ c.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: c.BaseURL + "/repos/" + c.Repo.Repository.FullName() + "/commits/" + sha.String(),
+ SHA: sha.String(),
+ }
+ }
+
+ c.JSONSuccess(&api.Commit{
+ CommitMeta: &api.CommitMeta{
+ URL: setting.AppURL + c.Link[1:],
+ SHA: commit.ID.String(),
+ },
+ HTMLURL: c.Repo.Repository.HTMLURL() + "/commits/" + commit.ID.String(),
+ RepoCommit: &api.RepoCommit{
+ URL: setting.AppURL + c.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: c.BaseURL + "/repos/" + c.Repo.Repository.FullName() + "/tree/" + commit.ID.String(),
+ SHA: commit.ID.String(),
+ },
+ },
+ Author: apiAuthor,
+ Committer: apiCommitter,
+ Parents: apiParents,
+ })
+}