Browse Source

GitHub API Compliance

pull/3742/head
Kim "BKC" Carlbäcker 8 years ago
parent
commit
524062bb99
  1. 28
      models/issue_label.go
  2. 3
      modules/auth/auth.go
  3. 20
      routers/api/v1/repo/issue.go
  4. 13
      routers/api/v1/repo/label.go

28
models/issue_label.go

@ -68,7 +68,7 @@ func (label *Label) APIFormat() *api.Label {
return &api.Label{ return &api.Label{
ID: label.ID, ID: label.ID,
Name: label.Name, Name: label.Name,
Color: label.Color, Color: strings.TrimLeft(label.Color, "#"),
} }
} }
@ -103,6 +103,27 @@ func NewLabels(labels ...*Label) error {
return err return err
} }
// getLabelInRepoByName 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 getLabelInRepoByName(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. // getLabelInRepoByID returns a label by ID in given repository.
// If pass repoID as 0, then ORM will ignore limitation of repository // If pass repoID as 0, then ORM will ignore limitation of repository
// and can return arbitrary label with any valid ID. // and can return arbitrary label with any valid ID.
@ -129,6 +150,11 @@ func GetLabelByID(id int64) (*Label, error) {
return getLabelInRepoByID(x, 0, id) return getLabelInRepoByID(x, 0, id)
} }
// GetLabelInRepoByID returns a label by ID in given repository.
func GetLabelInRepoByName(repoID int64, labelName string) (*Label, error) {
return getLabelInRepoByName(x, repoID, labelName)
}
// GetLabelInRepoByID returns a label by ID in given repository. // GetLabelInRepoByID returns a label by ID in given repository.
func GetLabelInRepoByID(repoID, labelID int64) (*Label, error) { func GetLabelInRepoByID(repoID, labelID int64) (*Label, error) {
return getLabelInRepoByID(x, repoID, labelID) return getLabelInRepoByID(x, repoID, labelID)

3
modules/auth/auth.go

@ -34,6 +34,9 @@ func SignedInID(ctx *macaron.Context, sess session.Store) int64 {
// Check access token. // Check access token.
if IsAPIPath(ctx.Req.URL.Path) { if IsAPIPath(ctx.Req.URL.Path) {
tokenSHA := ctx.Query("token") tokenSHA := ctx.Query("token")
if len(tokenSHA) <= 0 {
tokenSHA = ctx.Query("access_token")
}
if len(tokenSHA) == 0 { if len(tokenSHA) == 0 {
// Well, check with header again. // Well, check with header again.
auHead := ctx.Req.Header.Get("Authorization") auHead := ctx.Req.Header.Get("Authorization")

20
routers/api/v1/repo/issue.go

@ -16,14 +16,26 @@ import (
) )
func ListIssues(ctx *context.APIContext) { func ListIssues(ctx *context.APIContext) {
issues, err := models.Issues(&models.IssuesOptions{ issueOpts := models.IssuesOptions{
RepoID: ctx.Repo.Repository.ID, RepoID: ctx.Repo.Repository.ID,
Page: ctx.QueryInt("page"), Page: ctx.QueryInt("page"),
}) IsClosed: ctx.Query("state") == "closed",
}
issues, err := models.Issues(&issueOpts)
if err != nil { if err != nil {
ctx.Error(500, "Issues", err) ctx.Error(500, "Issues", err)
return return
} }
if ctx.Query("state") == "all" {
issueOpts.IsClosed = !issueOpts.IsClosed
temp_issues, err := models.Issues(&issueOpts)
if err != nil {
ctx.Error(500, "Issues", err)
return
}
issues = append(issues, temp_issues...)
}
// FIXME: use IssueList to improve performance. // FIXME: use IssueList to improve performance.
apiIssues := make([]*api.Issue, len(issues)) apiIssues := make([]*api.Issue, len(issues))

13
routers/api/v1/repo/label.go

@ -5,6 +5,8 @@
package repo package repo
import ( import (
"strconv"
api "github.com/gogits/go-gogs-client" api "github.com/gogits/go-gogs-client"
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
@ -26,7 +28,16 @@ func ListLabels(ctx *context.APIContext) {
} }
func GetLabel(ctx *context.APIContext) { func GetLabel(ctx *context.APIContext) {
label, err := models.GetLabelInRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) var (
label *models.Label
err error
)
strID := ctx.Params(":id")
if intID, err2 := strconv.ParseInt(strID, 10, 64); err2 != nil {
label, err = models.GetLabelInRepoByName(ctx.Repo.Repository.ID, strID)
} else {
label, err = models.GetLabelInRepoByID(ctx.Repo.Repository.ID, intID)
}
if err != nil { if err != nil {
if models.IsErrLabelNotExist(err) { if models.IsErrLabelNotExist(err) {
ctx.Status(404) ctx.Status(404)

Loading…
Cancel
Save