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/api/v1/repo | |
parent | 84b56c3c53fb931f569f9d0b5e2bf3832aba6abe (diff) | |
download | gitea-0b273ac4d566837b668d9e521476cd94ac28b6d4.tar.gz gitea-0b273ac4d566837b668d9e521476cd94ac28b6d4.zip |
#3383 code cleanup
Diffstat (limited to 'routers/api/v1/repo')
-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 |
3 files changed, 19 insertions, 149 deletions
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) - } -} |