collaborators.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. api "github.com/gogs/go-gogs-client"
  7. "github.com/gogs/gogs/models"
  8. "github.com/gogs/gogs/models/errors"
  9. "github.com/gogs/gogs/pkg/context"
  10. )
  11. func ListCollaborators(c *context.APIContext) {
  12. collaborators, err := c.Repo.Repository.GetCollaborators()
  13. if err != nil {
  14. if errors.IsUserNotExist(err) {
  15. c.Error(422, "", err)
  16. } else {
  17. c.Error(500, "GetCollaborators", err)
  18. }
  19. return
  20. }
  21. apiCollaborators := make([]*api.Collaborator, len(collaborators))
  22. for i := range collaborators {
  23. apiCollaborators[i] = collaborators[i].APIFormat()
  24. }
  25. c.JSON(200, &apiCollaborators)
  26. }
  27. func AddCollaborator(c *context.APIContext, form api.AddCollaboratorOption) {
  28. collaborator, err := models.GetUserByName(c.Params(":collaborator"))
  29. if err != nil {
  30. if errors.IsUserNotExist(err) {
  31. c.Error(422, "", err)
  32. } else {
  33. c.Error(500, "GetUserByName", err)
  34. }
  35. return
  36. }
  37. if err := c.Repo.Repository.AddCollaborator(collaborator); err != nil {
  38. c.Error(500, "AddCollaborator", err)
  39. return
  40. }
  41. if form.Permission != nil {
  42. if err := c.Repo.Repository.ChangeCollaborationAccessMode(collaborator.ID, models.ParseAccessMode(*form.Permission)); err != nil {
  43. c.Error(500, "ChangeCollaborationAccessMode", err)
  44. return
  45. }
  46. }
  47. c.Status(204)
  48. }
  49. func IsCollaborator(c *context.APIContext) {
  50. collaborator, err := models.GetUserByName(c.Params(":collaborator"))
  51. if err != nil {
  52. if errors.IsUserNotExist(err) {
  53. c.Error(422, "", err)
  54. } else {
  55. c.Error(500, "GetUserByName", err)
  56. }
  57. return
  58. }
  59. if !c.Repo.Repository.IsCollaborator(collaborator.ID) {
  60. c.Status(404)
  61. } else {
  62. c.Status(204)
  63. }
  64. }
  65. func DeleteCollaborator(c *context.APIContext) {
  66. collaborator, err := models.GetUserByName(c.Params(":collaborator"))
  67. if err != nil {
  68. if errors.IsUserNotExist(err) {
  69. c.Error(422, "", err)
  70. } else {
  71. c.Error(500, "GetUserByName", err)
  72. }
  73. return
  74. }
  75. if err := c.Repo.Repository.DeleteCollaboration(collaborator.ID); err != nil {
  76. c.Error(500, "DeleteCollaboration", err)
  77. return
  78. }
  79. c.Status(204)
  80. }