From 51d7f1264bc39a75b4fb016bee7d0f87f8485f3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=A0=E9=97=BB?= Date: Mon, 5 Jun 2017 15:34:11 -0400 Subject: [PATCH] api: GitHub compliance (#4549) * Add undocumented endpoint for /repositories/:id * GitHub API Compliance --- models/issue_label.go | 30 ++++++++++++++++++++++++++++-- pkg/auth/auth.go | 13 ++++++++----- routers/api/v1/repo/issue.go | 6 ++++-- routers/api/v1/repo/label.go | 11 ++++++++++- 4 files changed, 50 insertions(+), 10 deletions(-) diff --git a/models/issue_label.go b/models/issue_label.go index 8c56a7fd8..cb9723a92 100644 --- a/models/issue_label.go +++ b/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) { diff --git a/pkg/auth/auth.go b/pkg/auth/auth.go index a52ef958b..ba9ccce10 100644 --- a/pkg/auth/auth.go +++ b/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" { diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go index 4a0726334..d6ae7b4d8 100644 --- a/routers/api/v1/repo/issue.go +++ b/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) diff --git a/routers/api/v1/repo/label.go b/routers/api/v1/repo/label.go index 289f40a41..1161d6335 100644 --- a/routers/api/v1/repo/label.go +++ b/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)