Quellcode durchsuchen

api: add GetReferenceSHA

Yoginth vor 7 Jahren
Ursprung
Commit
b561f3dac3
4 geänderte Dateien mit 50 neuen und 3 gelöschten Zeilen
  1. 1 1
      gitote.go
  2. 1 2
      pkg/context/api.go
  3. 1 0
      routes/api/v1/api.go
  4. 47 0
      routes/api/v1/repo/commits.go

+ 1 - 1
gitote.go

@@ -29,7 +29,7 @@ import (
 )
 
 // AppVer represents the version of Gitote
-const AppVer = "1.0.1-prod-rc.16"
+const AppVer = "1.0.1-prod-rc.17"
 
 // APIVer represents the API version of Gitote
 const APIVer = "v1"

+ 1 - 2
pkg/context/api.go

@@ -25,8 +25,7 @@ type APIContext struct {
 	Org     *APIOrganization
 }
 
-// FIXME: move to gitlab.com/gitote/gitote-client
-const DOC_URL = "https://gitlab.com/gitote/gitote-client/wiki"
+const DOC_URL = "https://api.gitote.in"
 
 // Error responses error message to client with given message.
 // If status is 500, also it prints error to log.

+ 1 - 0
routes/api/v1/api.go

@@ -250,6 +250,7 @@ func RegisterRoutes(m *macaron.Macaron) {
 				})
 				m.Group("/commits", func() {
 					m.Get("/:sha", repo.GetSingleCommit)
+					m.Get("/*", repo.GetReferenceSHA)
 				})
 				m.Group("/keys", func() {
 					m.Combo("").Get(repo.ListDeployKeys).

+ 47 - 0
routes/api/v1/repo/commits.go

@@ -7,6 +7,8 @@
 package repo
 
 import (
+	"net/http"
+	"strings"
 	"time"
 
 	"gitlab.com/gitote/git-module"
@@ -19,6 +21,12 @@ import (
 )
 
 func GetSingleCommit(c *context.APIContext) {
+	if strings.Contains(c.Req.Header.Get("Accept"), api.MediaApplicationSHA) {
+		c.SetParams("*", c.Params(":sha"))
+		GetReferenceSHA(c)
+		return
+	}
+
 	gitRepo, err := git.OpenRepository(c.Repo.Repository.RepoPath())
 	if err != nil {
 		c.ServerError("OpenRepository", err)
@@ -91,3 +99,42 @@ func GetSingleCommit(c *context.APIContext) {
 		Parents:   apiParents,
 	})
 }
+
+func GetReferenceSHA(c *context.APIContext) {
+	gitRepo, err := git.OpenRepository(c.Repo.Repository.RepoPath())
+	if err != nil {
+		c.ServerError("OpenRepository", err)
+		return
+	}
+
+	ref := c.Params("*")
+	refType := 0 // 0-undetermined, 1-branch, 2-tag
+	if strings.HasPrefix(ref, git.BRANCH_PREFIX) {
+		ref = strings.TrimPrefix(ref, git.BRANCH_PREFIX)
+		refType = 1
+	} else if strings.HasPrefix(ref, git.TAG_PREFIX) {
+		ref = strings.TrimPrefix(ref, git.TAG_PREFIX)
+		refType = 2
+	} else {
+		if gitRepo.IsBranchExist(ref) {
+			refType = 1
+		} else if gitRepo.IsTagExist(ref) {
+			refType = 2
+		} else {
+			c.NotFound()
+			return
+		}
+	}
+
+	var sha string
+	if refType == 1 {
+		sha, err = gitRepo.GetBranchCommitID(ref)
+	} else if refType == 2 {
+		sha, err = gitRepo.GetTagCommitID(ref)
+	}
+	if err != nil {
+		c.NotFoundOrServerError("get reference commit ID", git.IsErrNotExist, err)
+		return
+	}
+	c.PlainText(http.StatusOK, []byte(sha))
+}