Browse Source

api: GitHub compliance (#4549)

* Add undocumented endpoint for /repositories/:id

* GitHub API Compliance
无闻 7 years ago
parent
commit
51d7f1264b
4 changed files with 50 additions and 10 deletions
  1. 28 2
      models/issue_label.go
  2. 8 5
      pkg/auth/auth.go
  3. 4 2
      routers/api/v1/repo/issue.go
  4. 10 1
      routers/api/v1/repo/label.go

+ 28 - 2
models/issue_label.go

@@ -68,7 +68,7 @@ func (label *Label) APIFormat() *api.Label {
 	return &api.Label{
 		ID:    label.ID,
 		Name:  label.Name,
-		Color: label.Color,
+		Color: strings.TrimLeft(label.Color, "#"),
 	}
 }
 
@@ -103,7 +103,28 @@ func NewLabels(labels ...*Label) error {
 	return err
 }
 
-// getLabelOfRepoByID returns a label by ID in given repository.
+// getLabelOfRepoByName returns a label by Name in given repository.
+// If pass repoID as 0, then ORM will ignore limitation of repository
+// and can return arbitrary label with any valid ID.
+func getLabelOfRepoByName(e Engine, repoID int64, labelName string) (*Label, error) {
+	if len(labelName) <= 0 {
+		return nil, ErrLabelNotExist{0, repoID}
+	}
+
+	l := &Label{
+		Name:   labelName,
+		RepoID: repoID,
+	}
+	has, err := x.Get(l)
+	if err != nil {
+		return nil, err
+	} else if !has {
+		return nil, ErrLabelNotExist{0, l.RepoID}
+	}
+	return l, nil
+}
+
+// getLabelInRepoByID returns a label by ID in given repository.
 // If pass repoID as 0, then ORM will ignore limitation of repository
 // and can return arbitrary label with any valid ID.
 func getLabelOfRepoByID(e Engine, repoID, labelID int64) (*Label, error) {
@@ -134,6 +155,11 @@ func GetLabelOfRepoByID(repoID, labelID int64) (*Label, error) {
 	return getLabelOfRepoByID(x, repoID, labelID)
 }
 
+// GetLabelOfRepoByName returns a label by name in given repository.
+func GetLabelOfRepoByName(repoID int64, labelName string) (*Label, error) {
+	return getLabelOfRepoByName(x, repoID, labelName)
+}
+
 // GetLabelsInRepoByIDs returns a list of labels by IDs in given repository,
 // it silently ignores label IDs that are not belong to the repository.
 func GetLabelsInRepoByIDs(repoID int64, labelIDs []int64) ([]*Label, error) {

+ 8 - 5
pkg/auth/auth.go

@@ -15,8 +15,8 @@ import (
 
 	"github.com/gogits/gogs/models"
 	"github.com/gogits/gogs/models/errors"
-	"github.com/gogits/gogs/pkg/tool"
 	"github.com/gogits/gogs/pkg/setting"
+	"github.com/gogits/gogs/pkg/tool"
 )
 
 func IsAPIPath(url string) bool {
@@ -24,17 +24,20 @@ func IsAPIPath(url string) bool {
 }
 
 // SignedInID returns the id of signed in user.
-func SignedInID(ctx *macaron.Context, sess session.Store) int64 {
+func SignedInID(c *macaron.Context, sess session.Store) int64 {
 	if !models.HasEngine {
 		return 0
 	}
 
 	// Check access token.
-	if IsAPIPath(ctx.Req.URL.Path) {
-		tokenSHA := ctx.Query("token")
+	if IsAPIPath(c.Req.URL.Path) {
+		tokenSHA := c.Query("token")
+		if len(tokenSHA) <= 0 {
+			tokenSHA = c.Query("access_token")
+		}
 		if len(tokenSHA) == 0 {
 			// Well, check with header again.
-			auHead := ctx.Req.Header.Get("Authorization")
+			auHead := c.Req.Header.Get("Authorization")
 			if len(auHead) > 0 {
 				auths := strings.Fields(auHead)
 				if len(auths) == 2 && auths[0] == "token" {

+ 4 - 2
routers/api/v1/repo/issue.go

@@ -47,6 +47,7 @@ func ListUserIssues(c *context.APIContext) {
 	opts := models.IssuesOptions{
 		AssigneeID: c.User.ID,
 		Page:       c.QueryInt("page"),
+		IsClosed:   api.StateType(c.Query("state")) == api.STATE_CLOSED,
 	}
 
 	listIssues(c, &opts)
@@ -54,8 +55,9 @@ func ListUserIssues(c *context.APIContext) {
 
 func ListIssues(c *context.APIContext) {
 	opts := models.IssuesOptions{
-		RepoID: c.Repo.Repository.ID,
-		Page:   c.QueryInt("page"),
+		RepoID:   c.Repo.Repository.ID,
+		Page:     c.QueryInt("page"),
+		IsClosed: api.StateType(c.Query("state")) == api.STATE_CLOSED,
 	}
 
 	listIssues(c, &opts)

+ 10 - 1
routers/api/v1/repo/label.go

@@ -5,6 +5,8 @@
 package repo
 
 import (
+	"github.com/Unknwon/com"
+
 	api "github.com/gogits/go-gogs-client"
 
 	"github.com/gogits/gogs/models"
@@ -26,7 +28,14 @@ func ListLabels(c *context.APIContext) {
 }
 
 func GetLabel(c *context.APIContext) {
-	label, err := models.GetLabelOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
+	var label *models.Label
+	var err error
+	idStr := c.Params(":id")
+	if id := com.StrTo(idStr).MustInt64(); id > 0 {
+		label, err = models.GetLabelOfRepoByID(c.Repo.Repository.ID, id)
+	} else {
+		label, err = models.GetLabelOfRepoByName(c.Repo.Repository.ID, idStr)
+	}
 	if err != nil {
 		if models.IsErrLabelNotExist(err) {
 			c.Status(404)