milestone.go 2.5 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/gogs/go-gogs-client"
  8. "github.com/gogs/gogs/models"
  9. "github.com/gogs/gogs/pkg/context"
  10. )
  11. func ListMilestones(c *context.APIContext) {
  12. milestones, err := models.GetMilestonesByRepoID(c.Repo.Repository.ID)
  13. if err != nil {
  14. c.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. c.JSON(200, &apiMilestones)
  22. }
  23. func GetMilestone(c *context.APIContext) {
  24. milestone, err := models.GetMilestoneByRepoID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
  25. if err != nil {
  26. if models.IsErrMilestoneNotExist(err) {
  27. c.Status(404)
  28. } else {
  29. c.Error(500, "GetMilestoneByRepoID", err)
  30. }
  31. return
  32. }
  33. c.JSON(200, milestone.APIFormat())
  34. }
  35. func CreateMilestone(c *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: c.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. c.Error(500, "NewMilestone", err)
  48. return
  49. }
  50. c.JSON(201, milestone.APIFormat())
  51. }
  52. func EditMilestone(c *context.APIContext, form api.EditMilestoneOption) {
  53. milestone, err := models.GetMilestoneByRepoID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
  54. if err != nil {
  55. if models.IsErrMilestoneNotExist(err) {
  56. c.Status(404)
  57. } else {
  58. c.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. c.Error(500, "ChangeStatus", err)
  74. return
  75. }
  76. } else if err = models.UpdateMilestone(milestone); err != nil {
  77. c.Handle(500, "UpdateMilestone", err)
  78. return
  79. }
  80. c.JSON(200, milestone.APIFormat())
  81. }
  82. func DeleteMilestone(c *context.APIContext) {
  83. if err := models.DeleteMilestoneOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id")); err != nil {
  84. c.Error(500, "DeleteMilestoneByRepoID", err)
  85. return
  86. }
  87. c.Status(204)
  88. }