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 6.1KB

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