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

collaborators.go 5.5 kB

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