milestone.go 2.9 KB

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