collaborators.go 1.9 KB

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