collaborators.go 2.1 KB

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