Skip to content

Commit b17506c

Browse files
committed
GitHub API Compliance (& linting)
1 parent 09ec5d0 commit b17506c

File tree

5 files changed

+61
-9
lines changed

5 files changed

+61
-9
lines changed

models/issue_label.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (label *Label) APIFormat() *api.Label {
6666
return &api.Label{
6767
ID: label.ID,
6868
Name: label.Name,
69-
Color: label.Color,
69+
Color: strings.TrimLeft(label.Color, "#"),
7070
}
7171
}
7272

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

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

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

modules/auth/auth.go

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

routers/api/v1/repo/issue.go

Lines changed: 16 additions & 4 deletions
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+
temp_issues, err := models.Issues(&issueOpts)
34+
if err != nil {
35+
ctx.Error(500, "Issues", err)
36+
return
37+
}
38+
issues = append(issues, temp_issues...)
39+
}
2840

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

routers/api/v1/repo/label.go

Lines changed: 12 additions & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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,7 +244,7 @@ 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
@@ -266,7 +266,7 @@ func GetByID(ctx *context.APIContext) {
266266
ctx.JSON(200, repo.APIFormat(&api.Permission{true, true, true}))
267267
}
268268

269-
// Delete delete one repository
269+
// Delete one repository
270270
// see https://github.com/gogits/go-gogs-client/wiki/Repositories#delete
271271
func Delete(ctx *context.APIContext) {
272272
owner := ctx.Repo.Owner

0 commit comments

Comments
 (0)