Skip to content

Commit d7ed78a

Browse files
authored
Merge pull request #227 from go-gitea/api/github-compliance
GitHub API Compliance Fixes
2 parents bea9d55 + e8e0539 commit d7ed78a

File tree

6 files changed

+78
-9
lines changed

6 files changed

+78
-9
lines changed

models/issue_label.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (label *Label) APIFormat() *api.Label {
6767
return &api.Label{
6868
ID: label.ID,
6969
Name: label.Name,
70-
Color: label.Color,
70+
Color: strings.TrimLeft(label.Color, "#"),
7171
}
7272
}
7373

@@ -102,6 +102,27 @@ func NewLabels(labels ...*Label) error {
102102
return err
103103
}
104104

105+
// getLabelInRepoByName returns a label by Name in given repository.
106+
// If pass repoID as 0, then ORM will ignore limitation of repository
107+
// and can return arbitrary label with any valid ID.
108+
func getLabelInRepoByName(e Engine, repoID int64, labelName string) (*Label, error) {
109+
if len(labelName) <= 0 {
110+
return nil, ErrLabelNotExist{0, repoID}
111+
}
112+
113+
l := &Label{
114+
Name: labelName,
115+
RepoID: repoID,
116+
}
117+
has, err := x.Get(l)
118+
if err != nil {
119+
return nil, err
120+
} else if !has {
121+
return nil, ErrLabelNotExist{0, l.RepoID}
122+
}
123+
return l, nil
124+
}
125+
105126
// getLabelInRepoByID returns a label by ID in given repository.
106127
// If pass repoID as 0, then ORM will ignore limitation of repository
107128
// and can return arbitrary label with any valid ID.
@@ -128,6 +149,11 @@ func GetLabelByID(id int64) (*Label, error) {
128149
return getLabelInRepoByID(x, 0, id)
129150
}
130151

152+
// GetLabelInRepoByName returns a label by name in given repository.
153+
func GetLabelInRepoByName(repoID int64, labelName string) (*Label, error) {
154+
return getLabelInRepoByName(x, repoID, labelName)
155+
}
156+
131157
// GetLabelInRepoByID returns a label by ID in given repository.
132158
func GetLabelInRepoByID(repoID, labelID int64) (*Label, error) {
133159
return getLabelInRepoByID(x, repoID, labelID)

modules/auth/auth.go

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ func SignedInID(ctx *macaron.Context, sess session.Store) int64 {
3535
// Check access token.
3636
if IsAPIPath(ctx.Req.URL.Path) {
3737
tokenSHA := ctx.Query("token")
38+
if len(tokenSHA) <= 0 {
39+
tokenSHA = ctx.Query("access_token")
40+
}
3841
if len(tokenSHA) == 0 {
3942
// Well, check with header again.
4043
auHead := ctx.Req.Header.Get("Authorization")

routers/api/v1/api.go

+2
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ func RegisterRoutes(m *macaron.Macaron) {
243243
m.Get("/search", repo.Search)
244244
})
245245

246+
m.Combo("/repositories/:id", reqToken()).Get(repo.GetByID)
247+
246248
m.Group("/repos", func() {
247249
m.Post("/migrate", bind(auth.MigrateRepoForm{}), repo.Migrate)
248250
m.Combo("/:username/:reponame", context.ExtractOwnerAndRepo()).

routers/api/v1/repo/issue.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,26 @@ import (
1717

1818
// ListIssues list the issues of a repository
1919
func ListIssues(ctx *context.APIContext) {
20-
issues, err := models.Issues(&models.IssuesOptions{
21-
RepoID: ctx.Repo.Repository.ID,
22-
Page: ctx.QueryInt("page"),
23-
})
20+
issueOpts := models.IssuesOptions{
21+
RepoID: ctx.Repo.Repository.ID,
22+
Page: ctx.QueryInt("page"),
23+
IsClosed: ctx.Query("state") == "closed",
24+
}
25+
26+
issues, err := models.Issues(&issueOpts)
2427
if err != nil {
2528
ctx.Error(500, "Issues", err)
2629
return
2730
}
31+
if ctx.Query("state") == "all" {
32+
issueOpts.IsClosed = !issueOpts.IsClosed
33+
tempIssues, err := models.Issues(&issueOpts)
34+
if err != nil {
35+
ctx.Error(500, "Issues", err)
36+
return
37+
}
38+
issues = append(issues, tempIssues...)
39+
}
2840

2941
// FIXME: use IssueList to improve performance.
3042
apiIssues := make([]*api.Issue, len(issues))

routers/api/v1/repo/label.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package repo
66

77
import (
8+
"strconv"
9+
810
api "code.gitea.io/sdk/gitea"
911

1012
"code.gitea.io/gitea/models"
@@ -28,7 +30,16 @@ func ListLabels(ctx *context.APIContext) {
2830

2931
// GetLabel get label by repository and label id
3032
func GetLabel(ctx *context.APIContext) {
31-
label, err := models.GetLabelInRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
33+
var (
34+
label *models.Label
35+
err error
36+
)
37+
strID := ctx.Params(":id")
38+
if intID, err2 := strconv.ParseInt(strID, 10, 64); err2 != nil {
39+
label, err = models.GetLabelInRepoByName(ctx.Repo.Repository.ID, strID)
40+
} else {
41+
label, err = models.GetLabelInRepoByID(ctx.Repo.Repository.ID, intID)
42+
}
3243
if err != nil {
3344
if models.IsErrLabelNotExist(err) {
3445
ctx.Status(404)

routers/api/v1/repo/repo.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateR
141141
ctx.JSON(201, repo.APIFormat(&api.Permission{true, true, true}))
142142
}
143143

144-
// Create create one repository of mine
144+
// Create one repository of mine
145145
// see https://github.com/gogits/go-gogs-client/wiki/Repositories#create
146146
func Create(ctx *context.APIContext, opt api.CreateRepoOption) {
147147
// Shouldn't reach this condition, but just in case.
@@ -244,14 +244,29 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
244244
ctx.JSON(201, repo.APIFormat(&api.Permission{true, true, true}))
245245
}
246246

247-
// Get get one repository
247+
// Get one repository
248248
// see https://github.com/gogits/go-gogs-client/wiki/Repositories#get
249249
func Get(ctx *context.APIContext) {
250250
repo := ctx.Repo.Repository
251251
ctx.JSON(200, repo.APIFormat(&api.Permission{true, true, true}))
252252
}
253253

254-
// Delete delete one repository
254+
// GetByID returns a single Repository
255+
func GetByID(ctx *context.APIContext) {
256+
repo, err := models.GetRepositoryByID(ctx.ParamsInt64(":id"))
257+
if err != nil {
258+
if models.IsErrRepoNotExist(err) {
259+
ctx.Status(404)
260+
} else {
261+
ctx.Error(500, "GetRepositoryByID", err)
262+
}
263+
return
264+
}
265+
266+
ctx.JSON(200, repo.APIFormat(&api.Permission{true, true, true}))
267+
}
268+
269+
// Delete one repository
255270
// see https://github.com/gogits/go-gogs-client/wiki/Repositories#delete
256271
func Delete(ctx *context.APIContext) {
257272
owner := ctx.Repo.Owner

0 commit comments

Comments
 (0)