label.go 2.0 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. "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.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 *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.NotFoundOrServerError("GetLabel", db.IsErrLabelNotExist, err)
  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.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 := db.GetLabelOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
  53. if err != nil {
  54. c.NotFoundOrServerError("GetLabelOfRepoByID", db.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 := db.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 := db.DeleteLabel(c.Repo.Repository.ID, c.ParamsInt64(":id")); err != nil {
  71. c.ServerError("DeleteLabel", err)
  72. return
  73. }
  74. c.NoContent()
  75. }