diff options
author | Unknwon <u@gogs.io> | 2016-08-24 16:05:56 -0700 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2016-08-24 16:05:56 -0700 |
commit | 0b273ac4d566837b668d9e521476cd94ac28b6d4 (patch) | |
tree | 034db5238583a8f73bc5c83c2e4f1ae724eea092 /routers | |
parent | 84b56c3c53fb931f569f9d0b5e2bf3832aba6abe (diff) | |
download | gitea-0b273ac4d566837b668d9e521476cd94ac28b6d4.tar.gz gitea-0b273ac4d566837b668d9e521476cd94ac28b6d4.zip |
#3383 code cleanup
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/api.go | 22 | ||||
-rw-r--r-- | routers/api/v1/repo/issue.go | 3 | ||||
-rw-r--r-- | routers/api/v1/repo/issue_milestone.go | 76 | ||||
-rw-r--r-- | routers/api/v1/repo/milestone.go | 89 | ||||
-rw-r--r-- | routers/repo/issue.go | 24 |
5 files changed, 44 insertions, 170 deletions
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index eb15acc313..ffbfce893a 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -110,6 +110,15 @@ func reqAdmin() macaron.Handler { } } +func reqRepoWriter() macaron.Handler { + return func(ctx *context.Context) { + if !ctx.Repo.IsWriter() { + ctx.Error(403) + return + } + } +} + func orgAssignment(args ...bool) macaron.Handler { var ( assignOrg bool @@ -259,11 +268,6 @@ func RegisterRoutes(m *macaron.Macaron) { Delete(repo.ClearIssueLabels) m.Delete("/:id", repo.DeleteIssueLabel) }) - m.Group("/milestone", func() { - m.Combo("").Get(repo.GetIssueMilestone). - Post(bind(api.SetIssueMilestoneOption{}), repo.SetIssueMilestone). - Delete(repo.DeleteIssueMilestone) - }) }) }, mustEnableIssues) @@ -275,10 +279,10 @@ func RegisterRoutes(m *macaron.Macaron) { }) m.Group("/milestones", func() { m.Combo("").Get(repo.ListMilestones). - Post(bind(api.CreateMilestoneOption{}), repo.CreateMilestone) - m.Combo("/:id").Get(repo.GetMilestone).Patch(bind(api.EditMilestoneOption{}), repo.EditMilestone). - Delete(repo.DeleteMilestone) - m.Post("/:id/:action", repo.ChangeMilestoneStatus) + Post(reqRepoWriter(), bind(api.CreateMilestoneOption{}), repo.CreateMilestone) + m.Combo("/:id").Get(repo.GetMilestone). + Patch(reqRepoWriter(), bind(api.EditMilestoneOption{}), repo.EditMilestone). + Delete(reqRepoWriter(), repo.DeleteMilestone) }) }, repoAssignment()) }, reqToken()) diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go index 2d37a5d0ef..b83f7e66b9 100644 --- a/routers/api/v1/repo/issue.go +++ b/routers/api/v1/repo/issue.go @@ -49,7 +49,6 @@ func GetIssue(ctx *context.APIContext) { } return } - ctx.JSON(200, issue.APIFormat()) } @@ -133,7 +132,7 @@ func EditIssue(ctx *context.APIContext, form api.EditIssueOption) { assignee, err := models.GetUserByName(*form.Assignee) if err != nil { if models.IsErrUserNotExist(err) { - ctx.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", *form.Assignee)) + ctx.Error(422, "", fmt.Sprintf("assignee does not exist: [name: %s]", *form.Assignee)) } else { ctx.Error(500, "GetUserByName", err) } diff --git a/routers/api/v1/repo/issue_milestone.go b/routers/api/v1/repo/issue_milestone.go deleted file mode 100644 index 8dfdf5ef53..0000000000 --- a/routers/api/v1/repo/issue_milestone.go +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright 2016 The Gogs Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package repo - -import ( - api "github.com/gogits/go-gogs-client" - - "github.com/gogits/gogs/models" - "github.com/gogits/gogs/modules/context" - "github.com/gogits/gogs/routers/api/v1/convert" -) - -func GetIssueMilestone(ctx *context.APIContext) { - issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) - if err != nil { - if models.IsErrIssueNotExist(err) { - ctx.Status(404) - } else { - ctx.Error(500, "GetIssueByIndex", err) - } - return - } - - apiMilestone := convert.ToMilestone(issue.Milestone) - ctx.JSON(200, &apiMilestone) -} - -func SetIssueMilestone(ctx *context.APIContext, form api.SetIssueMilestoneOption) { - if !ctx.Repo.IsWriter() { - ctx.Status(403) - return - } - - issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) - if err != nil { - if models.IsErrIssueNotExist(err) { - ctx.Status(404) - } else { - ctx.Error(500, "GetIssueByIndex", err) - } - return - } - - oldMid := issue.MilestoneID - if oldMid != form.ID { - issue.MilestoneID = form.ID - if err = models.ChangeMilestoneAssign(oldMid, issue); err != nil { - ctx.Error(500, "ChangeMilestoneAssign", err) - return - } - } - - // Refresh issue to return updated milestone - issue, err = models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) - if err != nil { - if models.IsErrIssueNotExist(err) { - ctx.Status(404) - } else { - ctx.Error(500, "GetIssueByIndex", err) - } - return - } - - apiMilestone := convert.ToMilestone(issue.Milestone) - ctx.JSON(200, &apiMilestone) -} - -func DeleteIssueMilestone(ctx *context.APIContext) { - form := api.SetIssueMilestoneOption{ - ID: 0, - } - - SetIssueMilestone(ctx, form) -} diff --git a/routers/api/v1/repo/milestone.go b/routers/api/v1/repo/milestone.go index 416d9da90f..652ccbb5c6 100644 --- a/routers/api/v1/repo/milestone.go +++ b/routers/api/v1/repo/milestone.go @@ -5,47 +5,42 @@ package repo import ( + "time" + api "github.com/gogits/go-gogs-client" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/context" - "github.com/gogits/gogs/routers/api/v1/convert" - "time" ) func ListMilestones(ctx *context.APIContext) { - milestones, err := models.GetAllRepoMilestones(ctx.Repo.Repository.ID) + milestones, err := models.GetMilestonesByRepoID(ctx.Repo.Repository.ID) if err != nil { - ctx.Error(500, "GetAllRepoMilestones", err) + ctx.Error(500, "GetMilestonesByRepoID", err) return } apiMilestones := make([]*api.Milestone, len(milestones)) for i := range milestones { - apiMilestones[i] = convert.ToMilestone(milestones[i]) + apiMilestones[i] = milestones[i].APIFormat() } ctx.JSON(200, &apiMilestones) } func GetMilestone(ctx *context.APIContext) { - milestone, err := models.GetRepoMilestoneByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) + milestone, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) if err != nil { if models.IsErrMilestoneNotExist(err) { ctx.Status(404) } else { - ctx.Error(500, "GetRepoMilestoneByID", err) + ctx.Error(500, "GetMilestoneByRepoID", err) } return } - ctx.JSON(200, convert.ToMilestone(milestone)) + ctx.JSON(200, milestone.APIFormat()) } func CreateMilestone(ctx *context.APIContext, form api.CreateMilestoneOption) { - if !ctx.Repo.IsWriter() { - ctx.Status(403) - return - } - if form.Deadline == nil { defaultDeadline, _ := time.ParseInLocation("2006-01-02", "9999-12-31", time.Local) form.Deadline = &defaultDeadline @@ -62,21 +57,16 @@ func CreateMilestone(ctx *context.APIContext, form api.CreateMilestoneOption) { ctx.Error(500, "NewMilestone", err) return } - ctx.JSON(201, convert.ToMilestone(milestone)) + ctx.JSON(201, milestone.APIFormat()) } func EditMilestone(ctx *context.APIContext, form api.EditMilestoneOption) { - if !ctx.Repo.IsWriter() { - ctx.Status(403) - return - } - - milestone, err := models.GetRepoMilestoneByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) + milestone, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) if err != nil { if models.IsErrMilestoneNotExist(err) { ctx.Status(404) } else { - ctx.Error(500, "GetRepoMilestoneByID", err) + ctx.Error(500, "GetMilestoneByRepoID", err) } return } @@ -84,67 +74,24 @@ func EditMilestone(ctx *context.APIContext, form api.EditMilestoneOption) { if len(form.Title) > 0 { milestone.Name = form.Title } - if len(form.Description) > 0 { - milestone.Content = form.Description + if form.Description != nil { + milestone.Content = *form.Description } - if !form.Deadline.IsZero() { + if form.Deadline != nil && !form.Deadline.IsZero() { milestone.Deadline = *form.Deadline } + if err := models.UpdateMilestone(milestone); err != nil { ctx.Handle(500, "UpdateMilestone", err) return } - ctx.JSON(200, convert.ToMilestone(milestone)) + ctx.JSON(200, milestone.APIFormat()) } func DeleteMilestone(ctx *context.APIContext) { - if !ctx.Repo.IsWriter() { - ctx.Status(403) - return - } - - if err := models.DeleteMilestoneByID(ctx.ParamsInt64(":id")); err != nil { - ctx.Error(500, "DeleteMilestoneByID", err) + if err := models.DeleteMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil { + ctx.Error(500, "DeleteMilestoneByRepoID", err) return } ctx.Status(204) } - -func ChangeMilestoneStatus(ctx *context.APIContext) { - if !ctx.Repo.IsWriter() { - ctx.Status(403) - return - } - - m, err := models.GetMilestoneByID(ctx.ParamsInt64(":id")) - if err != nil { - if models.IsErrMilestoneNotExist(err) { - ctx.Handle(404, "GetMilestoneByID", err) - } else { - ctx.Handle(500, "GetMilestoneByID", err) - } - return - } - - switch ctx.Params(":action") { - case "open": - if m.IsClosed { - if err = models.ChangeMilestoneStatus(m, false); err != nil { - ctx.Handle(500, "ChangeMilestoneStatus", err) - return - } - } - ctx.JSON(200, convert.ToMilestone(m)) - case "close": - if !m.IsClosed { - m.ClosedDate = time.Now() - if err = models.ChangeMilestoneStatus(m, true); err != nil { - ctx.Handle(500, "ChangeMilestoneStatus", err) - return - } - } - ctx.JSON(200, convert.ToMilestone(m)) - default: - ctx.Status(400) - } -} diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 37731e5ee6..2c23a7bea3 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -210,7 +210,7 @@ func Issues(ctx *context.Context) { ctx.Data["Issues"] = issues // Get milestones. - ctx.Data["Milestones"], err = models.GetAllRepoMilestones(repo.ID) + ctx.Data["Milestones"], err = models.GetMilestonesByRepoID(repo.ID) if err != nil { ctx.Handle(500, "GetAllRepoMilestones", err) return @@ -1099,12 +1099,12 @@ func EditMilestone(ctx *context.Context) { ctx.Data["RequireDatetimepicker"] = true ctx.Data["DateLang"] = setting.DateLang(ctx.Locale.Language()) - m, err := models.GetMilestoneByID(ctx.ParamsInt64(":id")) + m, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) if err != nil { if models.IsErrMilestoneNotExist(err) { - ctx.Handle(404, "GetMilestoneByID", nil) + ctx.Handle(404, "", nil) } else { - ctx.Handle(500, "GetMilestoneByID", err) + ctx.Handle(500, "GetMilestoneByRepoID", err) } return } @@ -1138,12 +1138,12 @@ func EditMilestonePost(ctx *context.Context, form auth.CreateMilestoneForm) { return } - m, err := models.GetMilestoneByID(ctx.ParamsInt64(":id")) + m, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) if err != nil { if models.IsErrMilestoneNotExist(err) { - ctx.Handle(404, "GetMilestoneByID", nil) + ctx.Handle(404, "", nil) } else { - ctx.Handle(500, "GetMilestoneByID", err) + ctx.Handle(500, "GetMilestoneByRepoID", err) } return } @@ -1160,12 +1160,12 @@ func EditMilestonePost(ctx *context.Context, form auth.CreateMilestoneForm) { } func ChangeMilestonStatus(ctx *context.Context) { - m, err := models.GetMilestoneByID(ctx.ParamsInt64(":id")) + m, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) if err != nil { if models.IsErrMilestoneNotExist(err) { - ctx.Handle(404, "GetMilestoneByID", err) + ctx.Handle(404, "", err) } else { - ctx.Handle(500, "GetMilestoneByID", err) + ctx.Handle(500, "GetMilestoneByRepoID", err) } return } @@ -1194,8 +1194,8 @@ func ChangeMilestonStatus(ctx *context.Context) { } func DeleteMilestone(ctx *context.Context) { - if err := models.DeleteMilestoneByID(ctx.QueryInt64("id")); err != nil { - ctx.Flash.Error("DeleteMilestoneByID: " + err.Error()) + if err := models.DeleteMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.QueryInt64("id")); err != nil { + ctx.Flash.Error("DeleteMilestoneByRepoID: " + err.Error()) } else { ctx.Flash.Success(ctx.Tr("repo.milestones.deletion_success")) } |