repo.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "path"
  7. "github.com/Unknwon/com"
  8. api "github.com/gogits/go-gogs-client"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/auth"
  11. "github.com/gogits/gogs/modules/log"
  12. "github.com/gogits/gogs/modules/middleware"
  13. "github.com/gogits/gogs/modules/setting"
  14. to "github.com/gogits/gogs/routers/api/v1/utils"
  15. )
  16. // https://github.com/gogits/go-gogs-client/wiki/Repositories#search-repositories
  17. func Search(ctx *middleware.Context) {
  18. opt := models.SearchOption{
  19. Keyword: path.Base(ctx.Query("q")),
  20. Uid: com.StrTo(ctx.Query("uid")).MustInt64(),
  21. Limit: com.StrTo(ctx.Query("limit")).MustInt(),
  22. }
  23. if opt.Limit == 0 {
  24. opt.Limit = 10
  25. }
  26. // Check visibility.
  27. if ctx.IsSigned && opt.Uid > 0 {
  28. if ctx.User.Id == opt.Uid {
  29. opt.Private = true
  30. } else {
  31. u, err := models.GetUserByID(opt.Uid)
  32. if err != nil {
  33. ctx.JSON(500, map[string]interface{}{
  34. "ok": false,
  35. "error": err.Error(),
  36. })
  37. return
  38. }
  39. if u.IsOrganization() && u.IsOwnedBy(ctx.User.Id) {
  40. opt.Private = true
  41. }
  42. // FIXME: how about collaborators?
  43. }
  44. }
  45. repos, err := models.SearchRepositoryByName(opt)
  46. if err != nil {
  47. ctx.JSON(500, map[string]interface{}{
  48. "ok": false,
  49. "error": err.Error(),
  50. })
  51. return
  52. }
  53. results := make([]*api.Repository, len(repos))
  54. for i := range repos {
  55. if err = repos[i].GetOwner(); err != nil {
  56. ctx.JSON(500, map[string]interface{}{
  57. "ok": false,
  58. "error": err.Error(),
  59. })
  60. return
  61. }
  62. results[i] = &api.Repository{
  63. Id: repos[i].ID,
  64. FullName: path.Join(repos[i].Owner.Name, repos[i].Name),
  65. }
  66. }
  67. ctx.JSON(200, map[string]interface{}{
  68. "ok": true,
  69. "data": results,
  70. })
  71. }
  72. // https://github.com/gogits/go-gogs-client/wiki/Repositories#list-your-repositories
  73. func ListMyRepos(ctx *middleware.Context) {
  74. ownRepos, err := models.GetRepositories(ctx.User.Id, true)
  75. if err != nil {
  76. ctx.APIError(500, "GetRepositories", err)
  77. return
  78. }
  79. numOwnRepos := len(ownRepos)
  80. accessibleRepos, err := ctx.User.GetRepositoryAccesses()
  81. if err != nil {
  82. ctx.APIError(500, "GetRepositoryAccesses", err)
  83. return
  84. }
  85. repos := make([]*api.Repository, numOwnRepos+len(accessibleRepos))
  86. for i := range ownRepos {
  87. repos[i] = to.ApiRepository(ctx.User, ownRepos[i], api.Permission{true, true, true})
  88. }
  89. i := numOwnRepos
  90. for repo, access := range accessibleRepos {
  91. repos[i] = to.ApiRepository(repo.Owner, repo, api.Permission{
  92. Admin: access >= models.ACCESS_MODE_ADMIN,
  93. Push: access >= models.ACCESS_MODE_WRITE,
  94. Pull: true,
  95. })
  96. i++
  97. }
  98. ctx.JSON(200, &repos)
  99. }
  100. func createRepo(ctx *middleware.Context, owner *models.User, opt api.CreateRepoOption) {
  101. repo, err := models.CreateRepository(owner, models.CreateRepoOptions{
  102. Name: opt.Name,
  103. Description: opt.Description,
  104. Gitignores: opt.Gitignores,
  105. License: opt.License,
  106. Readme: opt.Readme,
  107. IsPrivate: opt.Private,
  108. AutoInit: opt.AutoInit,
  109. })
  110. if err != nil {
  111. if models.IsErrRepoAlreadyExist(err) ||
  112. models.IsErrNameReserved(err) ||
  113. models.IsErrNamePatternNotAllowed(err) {
  114. ctx.APIError(422, "", err)
  115. } else {
  116. if repo != nil {
  117. if err = models.DeleteRepository(ctx.User.Id, repo.ID); err != nil {
  118. log.Error(4, "DeleteRepository: %v", err)
  119. }
  120. }
  121. ctx.APIError(500, "CreateRepository", err)
  122. }
  123. return
  124. }
  125. ctx.JSON(201, to.ApiRepository(owner, repo, api.Permission{true, true, true}))
  126. }
  127. // https://github.com/gogits/go-gogs-client/wiki/Repositories#create
  128. func Create(ctx *middleware.Context, opt api.CreateRepoOption) {
  129. // Shouldn't reach this condition, but just in case.
  130. if ctx.User.IsOrganization() {
  131. ctx.APIError(422, "", "not allowed creating repository for organization")
  132. return
  133. }
  134. createRepo(ctx, ctx.User, opt)
  135. }
  136. func CreateOrgRepo(ctx *middleware.Context, opt api.CreateRepoOption) {
  137. org, err := models.GetOrgByName(ctx.Params(":org"))
  138. if err != nil {
  139. if models.IsErrUserNotExist(err) {
  140. ctx.APIError(422, "", err)
  141. } else {
  142. ctx.APIError(500, "GetOrgByName", err)
  143. }
  144. return
  145. }
  146. if !org.IsOwnedBy(ctx.User.Id) {
  147. ctx.APIError(403, "", "Given user is not owner of organization.")
  148. return
  149. }
  150. createRepo(ctx, org, opt)
  151. }
  152. // https://github.com/gogits/go-gogs-client/wiki/Repositories#migrate
  153. func Migrate(ctx *middleware.Context, form auth.MigrateRepoForm) {
  154. ctxUser := ctx.User
  155. // Not equal means context user is an organization,
  156. // or is another user/organization if current user is admin.
  157. if form.Uid != ctxUser.Id {
  158. org, err := models.GetUserByID(form.Uid)
  159. if err != nil {
  160. if models.IsErrUserNotExist(err) {
  161. ctx.APIError(422, "", err)
  162. } else {
  163. ctx.APIError(500, "GetUserByID", err)
  164. }
  165. return
  166. }
  167. ctxUser = org
  168. }
  169. if ctx.HasError() {
  170. ctx.APIError(422, "", ctx.GetErrMsg())
  171. return
  172. }
  173. if ctxUser.IsOrganization() && !ctx.User.IsAdmin {
  174. // Check ownership of organization.
  175. if !ctxUser.IsOwnedBy(ctx.User.Id) {
  176. ctx.APIError(403, "", "Given user is not owner of organization.")
  177. return
  178. }
  179. }
  180. remoteAddr, err := form.ParseRemoteAddr(ctx.User)
  181. if err != nil {
  182. if models.IsErrInvalidCloneAddr(err) {
  183. addrErr := err.(models.ErrInvalidCloneAddr)
  184. switch {
  185. case addrErr.IsURLError:
  186. ctx.APIError(422, "", err)
  187. case addrErr.IsPermissionDenied:
  188. ctx.APIError(422, "", "You are not allowed to import local repositories.")
  189. case addrErr.IsInvalidPath:
  190. ctx.APIError(422, "", "Invalid local path, it does not exist or not a directory.")
  191. default:
  192. ctx.APIError(500, "ParseRemoteAddr", "Unknown error type (ErrInvalidCloneAddr): "+err.Error())
  193. }
  194. } else {
  195. ctx.APIError(500, "ParseRemoteAddr", err)
  196. }
  197. return
  198. }
  199. repo, err := models.MigrateRepository(ctxUser, models.MigrateRepoOptions{
  200. Name: form.RepoName,
  201. Description: form.Description,
  202. IsPrivate: form.Private || setting.Repository.ForcePrivate,
  203. IsMirror: form.Mirror,
  204. RemoteAddr: remoteAddr,
  205. })
  206. if err != nil {
  207. if repo != nil {
  208. if errDelete := models.DeleteRepository(ctxUser.Id, repo.ID); errDelete != nil {
  209. log.Error(4, "DeleteRepository: %v", errDelete)
  210. }
  211. }
  212. ctx.APIError(500, "MigrateRepository", err)
  213. return
  214. }
  215. log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
  216. ctx.JSON(201, to.ApiRepository(ctxUser, repo, api.Permission{true, true, true}))
  217. }
  218. func parseOwnerAndRepo(ctx *middleware.Context) (*models.User, *models.Repository) {
  219. owner, err := models.GetUserByName(ctx.Params(":username"))
  220. if err != nil {
  221. if models.IsErrUserNotExist(err) {
  222. ctx.APIError(422, "", err)
  223. } else {
  224. ctx.APIError(500, "GetUserByName", err)
  225. }
  226. return nil, nil
  227. }
  228. repo, err := models.GetRepositoryByName(owner.Id, ctx.Params(":reponame"))
  229. if err != nil {
  230. if models.IsErrRepoNotExist(err) {
  231. ctx.Error(404)
  232. } else {
  233. ctx.APIError(500, "GetRepositoryByName", err)
  234. }
  235. return nil, nil
  236. }
  237. return owner, repo
  238. }
  239. // https://github.com/gogits/go-gogs-client/wiki/Repositories#get
  240. func Get(ctx *middleware.Context) {
  241. owner, repo := parseOwnerAndRepo(ctx)
  242. if ctx.Written() {
  243. return
  244. }
  245. ctx.JSON(200, to.ApiRepository(owner, repo, api.Permission{true, true, true}))
  246. }
  247. // https://github.com/gogits/go-gogs-client/wiki/Repositories#delete
  248. func Delete(ctx *middleware.Context) {
  249. owner, repo := parseOwnerAndRepo(ctx)
  250. if ctx.Written() {
  251. return
  252. }
  253. if owner.IsOrganization() && !owner.IsOwnedBy(ctx.User.Id) {
  254. ctx.APIError(403, "", "Given user is not owner of organization.")
  255. return
  256. }
  257. if err := models.DeleteRepository(owner.Id, repo.ID); err != nil {
  258. ctx.APIError(500, "DeleteRepository", err)
  259. return
  260. }
  261. log.Trace("Repository deleted: %s/%s", owner.Name, repo.Name)
  262. ctx.Status(204)
  263. }