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.

209 lines
5.3 KiB

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