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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package repo
  5. import (
  6. "errors"
  7. "net/http"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/models/perm"
  10. access_model "code.gitea.io/gitea/models/perm/access"
  11. repo_model "code.gitea.io/gitea/models/repo"
  12. user_model "code.gitea.io/gitea/models/user"
  13. "code.gitea.io/gitea/modules/context"
  14. repo_module "code.gitea.io/gitea/modules/repository"
  15. api "code.gitea.io/gitea/modules/structs"
  16. "code.gitea.io/gitea/modules/web"
  17. "code.gitea.io/gitea/routers/api/v1/utils"
  18. "code.gitea.io/gitea/services/convert"
  19. )
  20. // ListCollaborators list a repository's collaborators
  21. func ListCollaborators(ctx *context.APIContext) {
  22. // swagger:operation GET /repos/{owner}/{repo}/collaborators repository repoListCollaborators
  23. // ---
  24. // summary: List a repository's collaborators
  25. // produces:
  26. // - application/json
  27. // parameters:
  28. // - name: owner
  29. // in: path
  30. // description: owner of the repo
  31. // type: string
  32. // required: true
  33. // - name: repo
  34. // in: path
  35. // description: name of the repo
  36. // type: string
  37. // required: true
  38. // - name: page
  39. // in: query
  40. // description: page number of results to return (1-based)
  41. // type: integer
  42. // - name: limit
  43. // in: query
  44. // description: page size of results
  45. // type: integer
  46. // responses:
  47. // "200":
  48. // "$ref": "#/responses/UserList"
  49. count, err := repo_model.CountCollaborators(ctx.Repo.Repository.ID)
  50. if err != nil {
  51. ctx.InternalServerError(err)
  52. return
  53. }
  54. collaborators, err := repo_model.GetCollaborators(ctx, ctx.Repo.Repository.ID, utils.GetListOptions(ctx))
  55. if err != nil {
  56. ctx.Error(http.StatusInternalServerError, "ListCollaborators", err)
  57. return
  58. }
  59. users := make([]*api.User, len(collaborators))
  60. for i, collaborator := range collaborators {
  61. users[i] = convert.ToUser(ctx, collaborator.User, ctx.Doer)
  62. }
  63. ctx.SetTotalCountHeader(count)
  64. ctx.JSON(http.StatusOK, users)
  65. }
  66. // IsCollaborator check if a user is a collaborator of a repository
  67. func IsCollaborator(ctx *context.APIContext) {
  68. // swagger:operation GET /repos/{owner}/{repo}/collaborators/{collaborator} repository repoCheckCollaborator
  69. // ---
  70. // summary: Check if a user is a collaborator of a repository
  71. // produces:
  72. // - application/json
  73. // parameters:
  74. // - name: owner
  75. // in: path
  76. // description: owner of the repo
  77. // type: string
  78. // required: true
  79. // - name: repo
  80. // in: path
  81. // description: name of the repo
  82. // type: string
  83. // required: true
  84. // - name: collaborator
  85. // in: path
  86. // description: username of the collaborator
  87. // type: string
  88. // required: true
  89. // responses:
  90. // "204":
  91. // "$ref": "#/responses/empty"
  92. // "404":
  93. // "$ref": "#/responses/notFound"
  94. // "422":
  95. // "$ref": "#/responses/validationError"
  96. user, err := user_model.GetUserByName(ctx, ctx.Params(":collaborator"))
  97. if err != nil {
  98. if user_model.IsErrUserNotExist(err) {
  99. ctx.Error(http.StatusUnprocessableEntity, "", err)
  100. } else {
  101. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  102. }
  103. return
  104. }
  105. isColab, err := repo_model.IsCollaborator(ctx, ctx.Repo.Repository.ID, user.ID)
  106. if err != nil {
  107. ctx.Error(http.StatusInternalServerError, "IsCollaborator", err)
  108. return
  109. }
  110. if isColab {
  111. ctx.Status(http.StatusNoContent)
  112. } else {
  113. ctx.NotFound()
  114. }
  115. }
  116. // AddCollaborator add a collaborator to a repository
  117. func AddCollaborator(ctx *context.APIContext) {
  118. // swagger:operation PUT /repos/{owner}/{repo}/collaborators/{collaborator} repository repoAddCollaborator
  119. // ---
  120. // summary: Add a collaborator to a repository
  121. // produces:
  122. // - application/json
  123. // parameters:
  124. // - name: owner
  125. // in: path
  126. // description: owner of the repo
  127. // type: string
  128. // required: true
  129. // - name: repo
  130. // in: path
  131. // description: name of the repo
  132. // type: string
  133. // required: true
  134. // - name: collaborator
  135. // in: path
  136. // description: username of the collaborator to add
  137. // type: string
  138. // required: true
  139. // - name: body
  140. // in: body
  141. // schema:
  142. // "$ref": "#/definitions/AddCollaboratorOption"
  143. // responses:
  144. // "204":
  145. // "$ref": "#/responses/empty"
  146. // "422":
  147. // "$ref": "#/responses/validationError"
  148. form := web.GetForm(ctx).(*api.AddCollaboratorOption)
  149. collaborator, err := user_model.GetUserByName(ctx, ctx.Params(":collaborator"))
  150. if err != nil {
  151. if user_model.IsErrUserNotExist(err) {
  152. ctx.Error(http.StatusUnprocessableEntity, "", err)
  153. } else {
  154. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  155. }
  156. return
  157. }
  158. if !collaborator.IsActive {
  159. ctx.Error(http.StatusInternalServerError, "InactiveCollaborator", errors.New("collaborator's account is inactive"))
  160. return
  161. }
  162. if err := repo_module.AddCollaborator(ctx, ctx.Repo.Repository, collaborator); err != nil {
  163. ctx.Error(http.StatusInternalServerError, "AddCollaborator", err)
  164. return
  165. }
  166. if form.Permission != nil {
  167. if err := repo_model.ChangeCollaborationAccessMode(ctx, ctx.Repo.Repository, collaborator.ID, perm.ParseAccessMode(*form.Permission)); err != nil {
  168. ctx.Error(http.StatusInternalServerError, "ChangeCollaborationAccessMode", err)
  169. return
  170. }
  171. }
  172. ctx.Status(http.StatusNoContent)
  173. }
  174. // DeleteCollaborator delete a collaborator from a repository
  175. func DeleteCollaborator(ctx *context.APIContext) {
  176. // swagger:operation DELETE /repos/{owner}/{repo}/collaborators/{collaborator} repository repoDeleteCollaborator
  177. // ---
  178. // summary: Delete a collaborator from a repository
  179. // produces:
  180. // - application/json
  181. // parameters:
  182. // - name: owner
  183. // in: path
  184. // description: owner of the repo
  185. // type: string
  186. // required: true
  187. // - name: repo
  188. // in: path
  189. // description: name of the repo
  190. // type: string
  191. // required: true
  192. // - name: collaborator
  193. // in: path
  194. // description: username of the collaborator to delete
  195. // type: string
  196. // required: true
  197. // responses:
  198. // "204":
  199. // "$ref": "#/responses/empty"
  200. // "422":
  201. // "$ref": "#/responses/validationError"
  202. collaborator, err := user_model.GetUserByName(ctx, ctx.Params(":collaborator"))
  203. if err != nil {
  204. if user_model.IsErrUserNotExist(err) {
  205. ctx.Error(http.StatusUnprocessableEntity, "", err)
  206. } else {
  207. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  208. }
  209. return
  210. }
  211. if err := models.DeleteCollaboration(ctx.Repo.Repository, collaborator.ID); err != nil {
  212. ctx.Error(http.StatusInternalServerError, "DeleteCollaboration", err)
  213. return
  214. }
  215. ctx.Status(http.StatusNoContent)
  216. }
  217. // GetRepoPermissions gets repository permissions for a user
  218. func GetRepoPermissions(ctx *context.APIContext) {
  219. // swagger:operation GET /repos/{owner}/{repo}/collaborators/{collaborator}/permission repository repoGetRepoPermissions
  220. // ---
  221. // summary: Get repository permissions for a user
  222. // produces:
  223. // - application/json
  224. // parameters:
  225. // - name: owner
  226. // in: path
  227. // description: owner of the repo
  228. // type: string
  229. // required: true
  230. // - name: repo
  231. // in: path
  232. // description: name of the repo
  233. // type: string
  234. // required: true
  235. // - name: collaborator
  236. // in: path
  237. // description: username of the collaborator
  238. // type: string
  239. // required: true
  240. // responses:
  241. // "200":
  242. // "$ref": "#/responses/RepoCollaboratorPermission"
  243. // "404":
  244. // "$ref": "#/responses/notFound"
  245. // "403":
  246. // "$ref": "#/responses/forbidden"
  247. if !ctx.Doer.IsAdmin && ctx.Doer.LoginName != ctx.Params(":collaborator") && !ctx.IsUserRepoAdmin() {
  248. ctx.Error(http.StatusForbidden, "User", "Only admins can query all permissions, repo admins can query all repo permissions, collaborators can query only their own")
  249. return
  250. }
  251. collaborator, err := user_model.GetUserByName(ctx, ctx.Params(":collaborator"))
  252. if err != nil {
  253. if user_model.IsErrUserNotExist(err) {
  254. ctx.Error(http.StatusNotFound, "GetUserByName", err)
  255. } else {
  256. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  257. }
  258. return
  259. }
  260. permission, err := access_model.GetUserRepoPermission(ctx, ctx.Repo.Repository, collaborator)
  261. if err != nil {
  262. ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err)
  263. return
  264. }
  265. ctx.JSON(http.StatusOK, convert.ToUserAndPermission(ctx, collaborator, ctx.ContextUser, permission.AccessMode))
  266. }
  267. // GetReviewers return all users that can be requested to review in this repo
  268. func GetReviewers(ctx *context.APIContext) {
  269. // swagger:operation GET /repos/{owner}/{repo}/reviewers repository repoGetReviewers
  270. // ---
  271. // summary: Return all users that can be requested to review in this repo
  272. // produces:
  273. // - application/json
  274. // parameters:
  275. // - name: owner
  276. // in: path
  277. // description: owner of the repo
  278. // type: string
  279. // required: true
  280. // - name: repo
  281. // in: path
  282. // description: name of the repo
  283. // type: string
  284. // required: true
  285. // responses:
  286. // "200":
  287. // "$ref": "#/responses/UserList"
  288. reviewers, err := repo_model.GetReviewers(ctx, ctx.Repo.Repository, ctx.Doer.ID, 0)
  289. if err != nil {
  290. ctx.Error(http.StatusInternalServerError, "ListCollaborators", err)
  291. return
  292. }
  293. ctx.JSON(http.StatusOK, convert.ToUsers(ctx, ctx.Doer, reviewers))
  294. }
  295. // GetAssignees return all users that have write access and can be assigned to issues
  296. func GetAssignees(ctx *context.APIContext) {
  297. // swagger:operation GET /repos/{owner}/{repo}/assignees repository repoGetAssignees
  298. // ---
  299. // summary: Return all users that have write access and can be assigned to issues
  300. // produces:
  301. // - application/json
  302. // parameters:
  303. // - name: owner
  304. // in: path
  305. // description: owner of the repo
  306. // type: string
  307. // required: true
  308. // - name: repo
  309. // in: path
  310. // description: name of the repo
  311. // type: string
  312. // required: true
  313. // responses:
  314. // "200":
  315. // "$ref": "#/responses/UserList"
  316. assignees, err := repo_model.GetRepoAssignees(ctx, ctx.Repo.Repository)
  317. if err != nil {
  318. ctx.Error(http.StatusInternalServerError, "ListCollaborators", err)
  319. return
  320. }
  321. ctx.JSON(http.StatusOK, convert.ToUsers(ctx, ctx.Doer, assignees))
  322. }