milestone.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "time"
  7. api "github.com/gogits/go-gogs-client"
  8. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/pkg/context"
  10. )
  11. func ListMilestones(ctx *context.APIContext) {
  12. milestones, err := models.GetMilestonesByRepoID(ctx.Repo.Repository.ID)
  13. if err != nil {
  14. ctx.Error(500, "GetMilestonesByRepoID", err)
  15. return
  16. }
  17. apiMilestones := make([]*api.Milestone, len(milestones))
  18. for i := range milestones {
  19. apiMilestones[i] = milestones[i].APIFormat()
  20. }
  21. ctx.JSON(200, &apiMilestones)
  22. }
  23. func GetMilestone(ctx *context.APIContext) {
  24. milestone, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  25. if err != nil {
  26. if models.IsErrMilestoneNotExist(err) {
  27. ctx.Status(404)
  28. } else {
  29. ctx.Error(500, "GetMilestoneByRepoID", err)
  30. }
  31. return
  32. }
  33. ctx.JSON(200, milestone.APIFormat())
  34. }
  35. func CreateMilestone(ctx *context.APIContext, form api.CreateMilestoneOption) {
  36. if form.Deadline == nil {
  37. defaultDeadline, _ := time.ParseInLocation("2006-01-02", "9999-12-31", time.Local)
  38. form.Deadline = &defaultDeadline
  39. }
  40. milestone := &models.Milestone{
  41. RepoID: ctx.Repo.Repository.ID,
  42. Name: form.Title,
  43. Content: form.Description,
  44. Deadline: *form.Deadline,
  45. }
  46. if err := models.NewMilestone(milestone); err != nil {
  47. ctx.Error(500, "NewMilestone", err)
  48. return
  49. }
  50. ctx.JSON(201, milestone.APIFormat())
  51. }
  52. func EditMilestone(ctx *context.APIContext, form api.EditMilestoneOption) {
  53. milestone, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  54. if err != nil {
  55. if models.IsErrMilestoneNotExist(err) {
  56. ctx.Status(404)
  57. } else {
  58. ctx.Error(500, "GetMilestoneByRepoID", err)
  59. }
  60. return
  61. }
  62. if len(form.Title) > 0 {
  63. milestone.Name = form.Title
  64. }
  65. if form.Description != nil {
  66. milestone.Content = *form.Description
  67. }
  68. if form.Deadline != nil && !form.Deadline.IsZero() {
  69. milestone.Deadline = *form.Deadline
  70. }
  71. if form.State != nil {
  72. if err = milestone.ChangeStatus(api.STATE_CLOSED == api.StateType(*form.State)); err != nil {
  73. ctx.Error(500, "ChangeStatus", err)
  74. return
  75. }
  76. } else if err = models.UpdateMilestone(milestone); err != nil {
  77. ctx.Handle(500, "UpdateMilestone", err)
  78. return
  79. }
  80. ctx.JSON(200, milestone.APIFormat())
  81. }
  82. func DeleteMilestone(ctx *context.APIContext) {
  83. if err := models.DeleteMilestoneOfRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
  84. ctx.Error(500, "DeleteMilestoneByRepoID", err)
  85. return
  86. }
  87. ctx.Status(204)
  88. }