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.2 kB

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