collaborators.go 2.1 KB

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