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