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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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/modules/web"
  14. "code.gitea.io/gitea/routers/api/v1/utils"
  15. )
  16. // ListCollaborators list a repository's collaborators
  17. func ListCollaborators(ctx *context.APIContext) {
  18. // swagger:operation GET /repos/{owner}/{repo}/collaborators repository repoListCollaborators
  19. // ---
  20. // summary: List a repository's collaborators
  21. // produces:
  22. // - application/json
  23. // parameters:
  24. // - name: owner
  25. // in: path
  26. // description: owner of the repo
  27. // type: string
  28. // required: true
  29. // - name: repo
  30. // in: path
  31. // description: name of the repo
  32. // type: string
  33. // required: true
  34. // - name: page
  35. // in: query
  36. // description: page number of results to return (1-based)
  37. // type: integer
  38. // - name: limit
  39. // in: query
  40. // description: page size of results
  41. // type: integer
  42. // responses:
  43. // "200":
  44. // "$ref": "#/responses/UserList"
  45. count, err := ctx.Repo.Repository.CountCollaborators()
  46. if err != nil {
  47. ctx.InternalServerError(err)
  48. return
  49. }
  50. collaborators, err := ctx.Repo.Repository.GetCollaborators(utils.GetListOptions(ctx))
  51. if err != nil {
  52. ctx.Error(http.StatusInternalServerError, "ListCollaborators", err)
  53. return
  54. }
  55. users := make([]*api.User, len(collaborators))
  56. for i, collaborator := range collaborators {
  57. users[i] = convert.ToUser(collaborator.User, ctx.User)
  58. }
  59. ctx.SetTotalCountHeader(count)
  60. ctx.JSON(http.StatusOK, users)
  61. }
  62. // IsCollaborator check if a user is a collaborator of a repository
  63. func IsCollaborator(ctx *context.APIContext) {
  64. // swagger:operation GET /repos/{owner}/{repo}/collaborators/{collaborator} repository repoCheckCollaborator
  65. // ---
  66. // summary: Check if a user is a collaborator of a repository
  67. // produces:
  68. // - application/json
  69. // parameters:
  70. // - name: owner
  71. // in: path
  72. // description: owner of the repo
  73. // type: string
  74. // required: true
  75. // - name: repo
  76. // in: path
  77. // description: name of the repo
  78. // type: string
  79. // required: true
  80. // - name: collaborator
  81. // in: path
  82. // description: username of the collaborator
  83. // type: string
  84. // required: true
  85. // responses:
  86. // "204":
  87. // "$ref": "#/responses/empty"
  88. // "404":
  89. // "$ref": "#/responses/notFound"
  90. // "422":
  91. // "$ref": "#/responses/validationError"
  92. user, err := models.GetUserByName(ctx.Params(":collaborator"))
  93. if err != nil {
  94. if models.IsErrUserNotExist(err) {
  95. ctx.Error(http.StatusUnprocessableEntity, "", err)
  96. } else {
  97. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  98. }
  99. return
  100. }
  101. isColab, err := ctx.Repo.Repository.IsCollaborator(user.ID)
  102. if err != nil {
  103. ctx.Error(http.StatusInternalServerError, "IsCollaborator", err)
  104. return
  105. }
  106. if isColab {
  107. ctx.Status(http.StatusNoContent)
  108. } else {
  109. ctx.NotFound()
  110. }
  111. }
  112. // AddCollaborator add a collaborator to a repository
  113. func AddCollaborator(ctx *context.APIContext) {
  114. // swagger:operation PUT /repos/{owner}/{repo}/collaborators/{collaborator} repository repoAddCollaborator
  115. // ---
  116. // summary: Add a collaborator to a repository
  117. // produces:
  118. // - application/json
  119. // parameters:
  120. // - name: owner
  121. // in: path
  122. // description: owner of the repo
  123. // type: string
  124. // required: true
  125. // - name: repo
  126. // in: path
  127. // description: name of the repo
  128. // type: string
  129. // required: true
  130. // - name: collaborator
  131. // in: path
  132. // description: username of the collaborator to add
  133. // type: string
  134. // required: true
  135. // - name: body
  136. // in: body
  137. // schema:
  138. // "$ref": "#/definitions/AddCollaboratorOption"
  139. // responses:
  140. // "204":
  141. // "$ref": "#/responses/empty"
  142. // "422":
  143. // "$ref": "#/responses/validationError"
  144. form := web.GetForm(ctx).(*api.AddCollaboratorOption)
  145. collaborator, err := models.GetUserByName(ctx.Params(":collaborator"))
  146. if err != nil {
  147. if models.IsErrUserNotExist(err) {
  148. ctx.Error(http.StatusUnprocessableEntity, "", err)
  149. } else {
  150. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  151. }
  152. return
  153. }
  154. if !collaborator.IsActive {
  155. ctx.Error(http.StatusInternalServerError, "InactiveCollaborator", errors.New("collaborator's account is inactive"))
  156. return
  157. }
  158. if err := ctx.Repo.Repository.AddCollaborator(collaborator); err != nil {
  159. ctx.Error(http.StatusInternalServerError, "AddCollaborator", err)
  160. return
  161. }
  162. if form.Permission != nil {
  163. if err := ctx.Repo.Repository.ChangeCollaborationAccessMode(collaborator.ID, models.ParseAccessMode(*form.Permission)); err != nil {
  164. ctx.Error(http.StatusInternalServerError, "ChangeCollaborationAccessMode", err)
  165. return
  166. }
  167. }
  168. ctx.Status(http.StatusNoContent)
  169. }
  170. // DeleteCollaborator delete a collaborator from a repository
  171. func DeleteCollaborator(ctx *context.APIContext) {
  172. // swagger:operation DELETE /repos/{owner}/{repo}/collaborators/{collaborator} repository repoDeleteCollaborator
  173. // ---
  174. // summary: Delete a collaborator from a repository
  175. // produces:
  176. // - application/json
  177. // parameters:
  178. // - name: owner
  179. // in: path
  180. // description: owner of the repo
  181. // type: string
  182. // required: true
  183. // - name: repo
  184. // in: path
  185. // description: name of the repo
  186. // type: string
  187. // required: true
  188. // - name: collaborator
  189. // in: path
  190. // description: username of the collaborator to delete
  191. // type: string
  192. // required: true
  193. // responses:
  194. // "204":
  195. // "$ref": "#/responses/empty"
  196. // "422":
  197. // "$ref": "#/responses/validationError"
  198. collaborator, err := models.GetUserByName(ctx.Params(":collaborator"))
  199. if err != nil {
  200. if models.IsErrUserNotExist(err) {
  201. ctx.Error(http.StatusUnprocessableEntity, "", err)
  202. } else {
  203. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  204. }
  205. return
  206. }
  207. if err := ctx.Repo.Repository.DeleteCollaboration(collaborator.ID); err != nil {
  208. ctx.Error(http.StatusInternalServerError, "DeleteCollaboration", err)
  209. return
  210. }
  211. ctx.Status(http.StatusNoContent)
  212. }
  213. // GetReviewers return all users that can be requested to review in this repo
  214. func GetReviewers(ctx *context.APIContext) {
  215. // swagger:operation GET /repos/{owner}/{repo}/reviewers repository repoGetReviewers
  216. // ---
  217. // summary: Return all users that can be requested to review in this repo
  218. // produces:
  219. // - application/json
  220. // parameters:
  221. // - name: owner
  222. // in: path
  223. // description: owner of the repo
  224. // type: string
  225. // required: true
  226. // - name: repo
  227. // in: path
  228. // description: name of the repo
  229. // type: string
  230. // required: true
  231. // responses:
  232. // "200":
  233. // "$ref": "#/responses/UserList"
  234. reviewers, err := ctx.Repo.Repository.GetReviewers(ctx.User.ID, 0)
  235. if err != nil {
  236. ctx.Error(http.StatusInternalServerError, "ListCollaborators", err)
  237. return
  238. }
  239. ctx.JSON(http.StatusOK, convert.ToUsers(ctx.User, reviewers))
  240. }
  241. // GetAssignees return all users that have write access and can be assigned to issues
  242. func GetAssignees(ctx *context.APIContext) {
  243. // swagger:operation GET /repos/{owner}/{repo}/assignees repository repoGetAssignees
  244. // ---
  245. // summary: Return all users that have write access and can be assigned to issues
  246. // produces:
  247. // - application/json
  248. // parameters:
  249. // - name: owner
  250. // in: path
  251. // description: owner of the repo
  252. // type: string
  253. // required: true
  254. // - name: repo
  255. // in: path
  256. // description: name of the repo
  257. // type: string
  258. // required: true
  259. // responses:
  260. // "200":
  261. // "$ref": "#/responses/UserList"
  262. assignees, err := ctx.Repo.Repository.GetAssignees()
  263. if err != nil {
  264. ctx.Error(http.StatusInternalServerError, "ListCollaborators", err)
  265. return
  266. }
  267. ctx.JSON(http.StatusOK, convert.ToUsers(ctx.User, assignees))
  268. }