collaborators.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package repo
  7. import (
  8. "gitote/gitote/models"
  9. "gitote/gitote/models/errors"
  10. "gitote/gitote/pkg/context"
  11. api "gitlab.com/gitote/go-gitote-client"
  12. )
  13. // ListCollaborators list a repository's collaborators
  14. func ListCollaborators(c *context.APIContext) {
  15. collaborators, err := c.Repo.Repository.GetCollaborators()
  16. if err != nil {
  17. c.ServerError("GetCollaborators", err)
  18. }
  19. apiCollaborators := make([]*api.Collaborator, len(collaborators))
  20. for i := range collaborators {
  21. apiCollaborators[i] = collaborators[i].APIFormat()
  22. }
  23. c.JSONSuccess(&apiCollaborators)
  24. }
  25. // AddCollaborator add a collaborator to a repository
  26. func AddCollaborator(c *context.APIContext, form api.AddCollaboratorOption) {
  27. collaborator, err := models.GetUserByName(c.Params(":collaborator"))
  28. if err != nil {
  29. if errors.IsUserNotExist(err) {
  30. c.Error(422, "", err)
  31. } else {
  32. c.Error(500, "GetUserByName", err)
  33. }
  34. return
  35. }
  36. if err := c.Repo.Repository.AddCollaborator(collaborator); err != nil {
  37. c.Error(500, "AddCollaborator", err)
  38. return
  39. }
  40. if form.Permission != nil {
  41. if err := c.Repo.Repository.ChangeCollaborationAccessMode(collaborator.ID, models.ParseAccessMode(*form.Permission)); err != nil {
  42. c.Error(500, "ChangeCollaborationAccessMode", err)
  43. return
  44. }
  45. }
  46. c.Status(204)
  47. }
  48. // IsCollaborator check if a user is a collaborator of a repository
  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. // DeleteCollaborator delete a collaborator from a repository
  66. func DeleteCollaborator(c *context.APIContext) {
  67. collaborator, err := models.GetUserByName(c.Params(":collaborator"))
  68. if err != nil {
  69. if errors.IsUserNotExist(err) {
  70. c.Error(422, "", err)
  71. } else {
  72. c.Error(500, "GetUserByName", err)
  73. }
  74. return
  75. }
  76. if err := c.Repo.Repository.DeleteCollaboration(collaborator.ID); err != nil {
  77. c.Error(500, "DeleteCollaboration", err)
  78. return
  79. }
  80. c.Status(204)
  81. }