milestone.go 2.4 KB

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