label.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. "github.com/Unknwon/com"
  8. api "github.com/gogs/go-gogs-client"
  9. "github.com/gogs/gogs/models"
  10. "github.com/gogs/gogs/pkg/context"
  11. )
  12. func ListLabels(c *context.APIContext) {
  13. labels, err := models.GetLabelsByRepoID(c.Repo.Repository.ID)
  14. if err != nil {
  15. c.ServerError("GetLabelsByRepoID", err)
  16. return
  17. }
  18. apiLabels := make([]*api.Label, len(labels))
  19. for i := range labels {
  20. apiLabels[i] = labels[i].APIFormat()
  21. }
  22. c.JSONSuccess(&apiLabels)
  23. }
  24. func GetLabel(c *context.APIContext) {
  25. var label *models.Label
  26. var err error
  27. idStr := c.Params(":id")
  28. if id := com.StrTo(idStr).MustInt64(); id > 0 {
  29. label, err = models.GetLabelOfRepoByID(c.Repo.Repository.ID, id)
  30. } else {
  31. label, err = models.GetLabelOfRepoByName(c.Repo.Repository.ID, idStr)
  32. }
  33. if err != nil {
  34. c.NotFoundOrServerError("GetLabel", models.IsErrLabelNotExist, err)
  35. return
  36. }
  37. c.JSONSuccess(label.APIFormat())
  38. }
  39. func CreateLabel(c *context.APIContext, form api.CreateLabelOption) {
  40. label := &models.Label{
  41. Name: form.Name,
  42. Color: form.Color,
  43. RepoID: c.Repo.Repository.ID,
  44. }
  45. if err := models.NewLabels(label); err != nil {
  46. c.ServerError("NewLabel", err)
  47. return
  48. }
  49. c.JSON(http.StatusCreated, label.APIFormat())
  50. }
  51. func EditLabel(c *context.APIContext, form api.EditLabelOption) {
  52. label, err := models.GetLabelOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
  53. if err != nil {
  54. c.NotFoundOrServerError("GetLabelOfRepoByID", models.IsErrLabelNotExist, err)
  55. return
  56. }
  57. if form.Name != nil {
  58. label.Name = *form.Name
  59. }
  60. if form.Color != nil {
  61. label.Color = *form.Color
  62. }
  63. if err := models.UpdateLabel(label); err != nil {
  64. c.ServerError("UpdateLabel", err)
  65. return
  66. }
  67. c.JSONSuccess(label.APIFormat())
  68. }
  69. func DeleteLabel(c *context.APIContext) {
  70. if err := models.DeleteLabel(c.Repo.Repository.ID, c.ParamsInt64(":id")); err != nil {
  71. c.ServerError("DeleteLabel", err)
  72. return
  73. }
  74. c.NoContent()
  75. }