milestone.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/modules/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 err := models.UpdateMilestone(milestone); err != nil {
  72. ctx.Handle(500, "UpdateMilestone", err)
  73. return
  74. }
  75. ctx.JSON(200, milestone.APIFormat())
  76. }
  77. func DeleteMilestone(ctx *context.APIContext) {
  78. if err := models.DeleteMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
  79. ctx.Error(500, "DeleteMilestoneByRepoID", err)
  80. return
  81. }
  82. ctx.Status(204)
  83. }