You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

collaborators.go 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "errors"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/convert"
  11. api "code.gitea.io/gitea/modules/structs"
  12. )
  13. // ListCollaborators list a repository's collaborators
  14. func ListCollaborators(ctx *context.APIContext) {
  15. // swagger:operation GET /repos/{owner}/{repo}/collaborators repository repoListCollaborators
  16. // ---
  17. // summary: List a repository's collaborators
  18. // produces:
  19. // - application/json
  20. // parameters:
  21. // - name: owner
  22. // in: path
  23. // description: owner of the repo
  24. // type: string
  25. // required: true
  26. // - name: repo
  27. // in: path
  28. // description: name of the repo
  29. // type: string
  30. // required: true
  31. // responses:
  32. // "200":
  33. // "$ref": "#/responses/UserList"
  34. collaborators, err := ctx.Repo.Repository.GetCollaborators()
  35. if err != nil {
  36. ctx.Error(500, "ListCollaborators", err)
  37. return
  38. }
  39. users := make([]*api.User, len(collaborators))
  40. for i, collaborator := range collaborators {
  41. users[i] = convert.ToUser(collaborator.User, ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin)
  42. }
  43. ctx.JSON(200, users)
  44. }
  45. // IsCollaborator check if a user is a collaborator of a repository
  46. func IsCollaborator(ctx *context.APIContext) {
  47. // swagger:operation GET /repos/{owner}/{repo}/collaborators/{collaborator} repository repoCheckCollaborator
  48. // ---
  49. // summary: Check if a user is a collaborator of a repository
  50. // produces:
  51. // - application/json
  52. // parameters:
  53. // - name: owner
  54. // in: path
  55. // description: owner of the repo
  56. // type: string
  57. // required: true
  58. // - name: repo
  59. // in: path
  60. // description: name of the repo
  61. // type: string
  62. // required: true
  63. // - name: collaborator
  64. // in: path
  65. // description: username of the collaborator
  66. // type: string
  67. // required: true
  68. // responses:
  69. // "204":
  70. // "$ref": "#/responses/empty"
  71. // "404":
  72. // "$ref": "#/responses/empty"
  73. user, err := models.GetUserByName(ctx.Params(":collaborator"))
  74. if err != nil {
  75. if models.IsErrUserNotExist(err) {
  76. ctx.Error(422, "", err)
  77. } else {
  78. ctx.Error(500, "GetUserByName", err)
  79. }
  80. return
  81. }
  82. isColab, err := ctx.Repo.Repository.IsCollaborator(user.ID)
  83. if err != nil {
  84. ctx.Error(500, "IsCollaborator", err)
  85. return
  86. }
  87. if isColab {
  88. ctx.Status(204)
  89. } else {
  90. ctx.NotFound()
  91. }
  92. }
  93. // AddCollaborator add a collaborator to a repository
  94. func AddCollaborator(ctx *context.APIContext, form api.AddCollaboratorOption) {
  95. // swagger:operation PUT /repos/{owner}/{repo}/collaborators/{collaborator} repository repoAddCollaborator
  96. // ---
  97. // summary: Add a collaborator to a repository
  98. // produces:
  99. // - application/json
  100. // parameters:
  101. // - name: owner
  102. // in: path
  103. // description: owner of the repo
  104. // type: string
  105. // required: true
  106. // - name: repo
  107. // in: path
  108. // description: name of the repo
  109. // type: string
  110. // required: true
  111. // - name: collaborator
  112. // in: path
  113. // description: username of the collaborator to add
  114. // type: string
  115. // required: true
  116. // - name: body
  117. // in: body
  118. // schema:
  119. // "$ref": "#/definitions/AddCollaboratorOption"
  120. // responses:
  121. // "204":
  122. // "$ref": "#/responses/empty"
  123. collaborator, err := models.GetUserByName(ctx.Params(":collaborator"))
  124. if err != nil {
  125. if models.IsErrUserNotExist(err) {
  126. ctx.Error(422, "", err)
  127. } else {
  128. ctx.Error(500, "GetUserByName", err)
  129. }
  130. return
  131. }
  132. if !collaborator.IsActive {
  133. ctx.Error(500, "InactiveCollaborator", errors.New("collaborator's account is inactive"))
  134. return
  135. }
  136. if err := ctx.Repo.Repository.AddCollaborator(collaborator); err != nil {
  137. ctx.Error(500, "AddCollaborator", err)
  138. return
  139. }
  140. if form.Permission != nil {
  141. if err := ctx.Repo.Repository.ChangeCollaborationAccessMode(collaborator.ID, models.ParseAccessMode(*form.Permission)); err != nil {
  142. ctx.Error(500, "ChangeCollaborationAccessMode", err)
  143. return
  144. }
  145. }
  146. ctx.Status(204)
  147. }
  148. // DeleteCollaborator delete a collaborator from a repository
  149. func DeleteCollaborator(ctx *context.APIContext) {
  150. // swagger:operation DELETE /repos/{owner}/{repo}/collaborators/{collaborator} repository repoDeleteCollaborator
  151. // ---
  152. // summary: Delete a collaborator from a repository
  153. // produces:
  154. // - application/json
  155. // parameters:
  156. // - name: owner
  157. // in: path
  158. // description: owner of the repo
  159. // type: string
  160. // required: true
  161. // - name: repo
  162. // in: path
  163. // description: name of the repo
  164. // type: string
  165. // required: true
  166. // - name: collaborator
  167. // in: path
  168. // description: username of the collaborator to delete
  169. // type: string
  170. // required: true
  171. // responses:
  172. // "204":
  173. // "$ref": "#/responses/empty"
  174. collaborator, err := models.GetUserByName(ctx.Params(":collaborator"))
  175. if err != nil {
  176. if models.IsErrUserNotExist(err) {
  177. ctx.Error(422, "", err)
  178. } else {
  179. ctx.Error(500, "GetUserByName", err)
  180. }
  181. return
  182. }
  183. if err := ctx.Repo.Repository.DeleteCollaboration(collaborator.ID); err != nil {
  184. ctx.Error(500, "DeleteCollaboration", err)
  185. return
  186. }
  187. ctx.Status(204)
  188. }