milestone.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "net/http"
  7. "time"
  8. api "github.com/gogs/go-gogs-client"
  9. "gogs.io/gogs/internal/context"
  10. "gogs.io/gogs/internal/db"
  11. )
  12. func ListMilestones(c *context.APIContext) {
  13. milestones, err := db.GetMilestonesByRepoID(c.Repo.Repository.ID)
  14. if err != nil {
  15. c.ServerError("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. c.JSONSuccess(&apiMilestones)
  23. }
  24. func GetMilestone(c *context.APIContext) {
  25. milestone, err := db.GetMilestoneByRepoID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
  26. if err != nil {
  27. c.NotFoundOrServerError("GetMilestoneByRepoID", db.IsErrMilestoneNotExist, err)
  28. return
  29. }
  30. c.JSONSuccess(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 := &db.Milestone{
  38. RepoID: c.Repo.Repository.ID,
  39. Name: form.Title,
  40. Content: form.Description,
  41. Deadline: *form.Deadline,
  42. }
  43. if err := db.NewMilestone(milestone); err != nil {
  44. c.ServerError("NewMilestone", err)
  45. return
  46. }
  47. c.JSON(http.StatusCreated, milestone.APIFormat())
  48. }
  49. func EditMilestone(c *context.APIContext, form api.EditMilestoneOption) {
  50. milestone, err := db.GetMilestoneByRepoID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
  51. if err != nil {
  52. c.NotFoundOrServerError("GetMilestoneByRepoID", db.IsErrMilestoneNotExist, err)
  53. return
  54. }
  55. if len(form.Title) > 0 {
  56. milestone.Name = form.Title
  57. }
  58. if form.Description != nil {
  59. milestone.Content = *form.Description
  60. }
  61. if form.Deadline != nil && !form.Deadline.IsZero() {
  62. milestone.Deadline = *form.Deadline
  63. }
  64. if form.State != nil {
  65. if err = milestone.ChangeStatus(api.STATE_CLOSED == api.StateType(*form.State)); err != nil {
  66. c.ServerError("ChangeStatus", err)
  67. return
  68. }
  69. } else if err = db.UpdateMilestone(milestone); err != nil {
  70. c.ServerError("UpdateMilestone", err)
  71. return
  72. }
  73. c.JSONSuccess(milestone.APIFormat())
  74. }
  75. func DeleteMilestone(c *context.APIContext) {
  76. if err := db.DeleteMilestoneOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id")); err != nil {
  77. c.ServerError("DeleteMilestoneByRepoID", err)
  78. return
  79. }
  80. c.NoContent()
  81. }