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

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