collaborators.go 2.3 KB

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