label.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. api "github.com/gogs/go-gogs-client"
  8. "github.com/unknwon/com"
  9. "gogs.io/gogs/internal/context"
  10. "gogs.io/gogs/internal/db"
  11. )
  12. func ListLabels(c *context.APIContext) {
  13. labels, err := db.GetLabelsByRepoID(c.Repo.Repository.ID)
  14. if err != nil {
  15. c.Error(err, "get labels by repository ID")
  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 *db.Label
  26. var err error
  27. idStr := c.Params(":id")
  28. if id := com.StrTo(idStr).MustInt64(); id > 0 {
  29. label, err = db.GetLabelOfRepoByID(c.Repo.Repository.ID, id)
  30. } else {
  31. label, err = db.GetLabelOfRepoByName(c.Repo.Repository.ID, idStr)
  32. }
  33. if err != nil {
  34. c.NotFoundOrError(err, "get label")
  35. return
  36. }
  37. c.JSONSuccess(label.APIFormat())
  38. }
  39. func CreateLabel(c *context.APIContext, form api.CreateLabelOption) {
  40. label := &db.Label{
  41. Name: form.Name,
  42. Color: form.Color,
  43. RepoID: c.Repo.Repository.ID,
  44. }
  45. if err := db.NewLabels(label); err != nil {
  46. c.Error(err, "new labels")
  47. return
  48. }
  49. c.JSON(http.StatusCreated, label.APIFormat())
  50. }
  51. func EditLabel(c *context.APIContext, form api.EditLabelOption) {
  52. label, err := db.GetLabelOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
  53. if err != nil {
  54. c.NotFoundOrError(err, "get label of repository by ID")
  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 := db.UpdateLabel(label); err != nil {
  64. c.Error(err, "update label")
  65. return
  66. }
  67. c.JSONSuccess(label.APIFormat())
  68. }
  69. func DeleteLabel(c *context.APIContext) {
  70. if err := db.DeleteLabel(c.Repo.Repository.ID, c.ParamsInt64(":id")); err != nil {
  71. c.Error(err, "delete label")
  72. return
  73. }
  74. c.NoContent()
  75. }