gitea源码

adopt.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package admin
  4. import (
  5. "net/http"
  6. repo_model "code.gitea.io/gitea/models/repo"
  7. user_model "code.gitea.io/gitea/models/user"
  8. "code.gitea.io/gitea/modules/util"
  9. "code.gitea.io/gitea/routers/api/v1/utils"
  10. "code.gitea.io/gitea/services/context"
  11. repo_service "code.gitea.io/gitea/services/repository"
  12. )
  13. // ListUnadoptedRepositories lists the unadopted repositories that match the provided names
  14. func ListUnadoptedRepositories(ctx *context.APIContext) {
  15. // swagger:operation GET /admin/unadopted admin adminUnadoptedList
  16. // ---
  17. // summary: List unadopted repositories
  18. // produces:
  19. // - application/json
  20. // parameters:
  21. // - name: page
  22. // in: query
  23. // description: page number of results to return (1-based)
  24. // type: integer
  25. // - name: limit
  26. // in: query
  27. // description: page size of results
  28. // type: integer
  29. // - name: pattern
  30. // in: query
  31. // description: pattern of repositories to search for
  32. // type: string
  33. // responses:
  34. // "200":
  35. // "$ref": "#/responses/StringSlice"
  36. // "403":
  37. // "$ref": "#/responses/forbidden"
  38. listOptions := utils.GetListOptions(ctx)
  39. if listOptions.Page == 0 {
  40. listOptions.Page = 1
  41. }
  42. repoNames, count, err := repo_service.ListUnadoptedRepositories(ctx, ctx.FormString("query"), &listOptions)
  43. if err != nil {
  44. ctx.APIErrorInternal(err)
  45. return
  46. }
  47. ctx.SetTotalCountHeader(int64(count))
  48. ctx.JSON(http.StatusOK, repoNames)
  49. }
  50. // AdoptRepository will adopt an unadopted repository
  51. func AdoptRepository(ctx *context.APIContext) {
  52. // swagger:operation POST /admin/unadopted/{owner}/{repo} admin adminAdoptRepository
  53. // ---
  54. // summary: Adopt unadopted files as a repository
  55. // produces:
  56. // - application/json
  57. // parameters:
  58. // - name: owner
  59. // in: path
  60. // description: owner of the repo
  61. // type: string
  62. // required: true
  63. // - name: repo
  64. // in: path
  65. // description: name of the repo
  66. // type: string
  67. // required: true
  68. // responses:
  69. // "204":
  70. // "$ref": "#/responses/empty"
  71. // "404":
  72. // "$ref": "#/responses/notFound"
  73. // "403":
  74. // "$ref": "#/responses/forbidden"
  75. ownerName := ctx.PathParam("username")
  76. repoName := ctx.PathParam("reponame")
  77. ctxUser, err := user_model.GetUserByName(ctx, ownerName)
  78. if err != nil {
  79. if user_model.IsErrUserNotExist(err) {
  80. ctx.APIErrorNotFound()
  81. return
  82. }
  83. ctx.APIErrorInternal(err)
  84. return
  85. }
  86. // check not a repo
  87. has, err := repo_model.IsRepositoryModelExist(ctx, ctxUser, repoName)
  88. if err != nil {
  89. ctx.APIErrorInternal(err)
  90. return
  91. }
  92. isDir, err := util.IsDir(repo_model.RepoPath(ctxUser.Name, repoName))
  93. if err != nil {
  94. ctx.APIErrorInternal(err)
  95. return
  96. }
  97. if has || !isDir {
  98. ctx.APIErrorNotFound()
  99. return
  100. }
  101. if _, err := repo_service.AdoptRepository(ctx, ctx.Doer, ctxUser, repo_service.CreateRepoOptions{
  102. Name: repoName,
  103. IsPrivate: true,
  104. }); err != nil {
  105. ctx.APIErrorInternal(err)
  106. return
  107. }
  108. ctx.Status(http.StatusNoContent)
  109. }
  110. // DeleteUnadoptedRepository will delete an unadopted repository
  111. func DeleteUnadoptedRepository(ctx *context.APIContext) {
  112. // swagger:operation DELETE /admin/unadopted/{owner}/{repo} admin adminDeleteUnadoptedRepository
  113. // ---
  114. // summary: Delete unadopted files
  115. // produces:
  116. // - application/json
  117. // parameters:
  118. // - name: owner
  119. // in: path
  120. // description: owner of the repo
  121. // type: string
  122. // required: true
  123. // - name: repo
  124. // in: path
  125. // description: name of the repo
  126. // type: string
  127. // required: true
  128. // responses:
  129. // "204":
  130. // "$ref": "#/responses/empty"
  131. // "403":
  132. // "$ref": "#/responses/forbidden"
  133. ownerName := ctx.PathParam("username")
  134. repoName := ctx.PathParam("reponame")
  135. ctxUser, err := user_model.GetUserByName(ctx, ownerName)
  136. if err != nil {
  137. if user_model.IsErrUserNotExist(err) {
  138. ctx.APIErrorNotFound()
  139. return
  140. }
  141. ctx.APIErrorInternal(err)
  142. return
  143. }
  144. // check not a repo
  145. has, err := repo_model.IsRepositoryModelExist(ctx, ctxUser, repoName)
  146. if err != nil {
  147. ctx.APIErrorInternal(err)
  148. return
  149. }
  150. isDir, err := util.IsDir(repo_model.RepoPath(ctxUser.Name, repoName))
  151. if err != nil {
  152. ctx.APIErrorInternal(err)
  153. return
  154. }
  155. if has || !isDir {
  156. ctx.APIErrorNotFound()
  157. return
  158. }
  159. if err := repo_service.DeleteUnadoptedRepository(ctx, ctx.Doer, ctxUser, repoName); err != nil {
  160. ctx.APIErrorInternal(err)
  161. return
  162. }
  163. ctx.Status(http.StatusNoContent)
  164. }