You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.7 KiB

  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 "code.gitea.io/sdk/gitea"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. )
  11. // ListMilestones list all the milestones for a repository
  12. func ListMilestones(ctx *context.APIContext) {
  13. milestones, err := models.GetMilestonesByRepoID(ctx.Repo.Repository.ID)
  14. if err != nil {
  15. ctx.Error(500, "GetMilestonesByRepoID", err)
  16. return
  17. }
  18. apiMilestones := make([]*api.Milestone, len(milestones))
  19. for i := range milestones {
  20. apiMilestones[i] = milestones[i].APIFormat()
  21. }
  22. ctx.JSON(200, &apiMilestones)
  23. }
  24. // GetMilestone get a milestone for a repository
  25. func GetMilestone(ctx *context.APIContext) {
  26. milestone, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  27. if err != nil {
  28. if models.IsErrMilestoneNotExist(err) {
  29. ctx.Status(404)
  30. } else {
  31. ctx.Error(500, "GetMilestoneByRepoID", err)
  32. }
  33. return
  34. }
  35. ctx.JSON(200, milestone.APIFormat())
  36. }
  37. // CreateMilestone create a milestone for a repository
  38. func CreateMilestone(ctx *context.APIContext, form api.CreateMilestoneOption) {
  39. if form.Deadline == nil {
  40. defaultDeadline, _ := time.ParseInLocation("2006-01-02", "9999-12-31", time.Local)
  41. form.Deadline = &defaultDeadline
  42. }
  43. milestone := &models.Milestone{
  44. RepoID: ctx.Repo.Repository.ID,
  45. Name: form.Title,
  46. Content: form.Description,
  47. Deadline: *form.Deadline,
  48. }
  49. if err := models.NewMilestone(milestone); err != nil {
  50. ctx.Error(500, "NewMilestone", err)
  51. return
  52. }
  53. ctx.JSON(201, milestone.APIFormat())
  54. }
  55. // EditMilestone modify a milestone for a repository
  56. func EditMilestone(ctx *context.APIContext, form api.EditMilestoneOption) {
  57. milestone, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  58. if err != nil {
  59. if models.IsErrMilestoneNotExist(err) {
  60. ctx.Status(404)
  61. } else {
  62. ctx.Error(500, "GetMilestoneByRepoID", err)
  63. }
  64. return
  65. }
  66. if len(form.Title) > 0 {
  67. milestone.Name = form.Title
  68. }
  69. if form.Description != nil {
  70. milestone.Content = *form.Description
  71. }
  72. if form.Deadline != nil && !form.Deadline.IsZero() {
  73. milestone.Deadline = *form.Deadline
  74. }
  75. if err := models.UpdateMilestone(milestone); err != nil {
  76. ctx.Handle(500, "UpdateMilestone", err)
  77. return
  78. }
  79. ctx.JSON(200, milestone.APIFormat())
  80. }
  81. // DeleteMilestone delete a milestone for a repository
  82. func DeleteMilestone(ctx *context.APIContext) {
  83. if err := models.DeleteMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
  84. ctx.Error(500, "DeleteMilestoneByRepoID", err)
  85. return
  86. }
  87. ctx.Status(204)
  88. }