repo.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. log "gopkg.in/clog.v1"
  8. api "github.com/gogits/go-gogs-client"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/context"
  11. "github.com/gogits/gogs/modules/form"
  12. "github.com/gogits/gogs/modules/setting"
  13. "github.com/gogits/gogs/routers/api/v1/convert"
  14. )
  15. // https://github.com/gogits/go-gogs-client/wiki/Repositories#search-repositories
  16. func Search(ctx *context.APIContext) {
  17. opts := &models.SearchRepoOptions{
  18. Keyword: path.Base(ctx.Query("q")),
  19. OwnerID: ctx.QueryInt64("uid"),
  20. PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")),
  21. }
  22. // Check visibility.
  23. if ctx.IsSigned && opts.OwnerID > 0 {
  24. if ctx.User.ID == opts.OwnerID {
  25. opts.Private = true
  26. } else {
  27. u, err := models.GetUserByID(opts.OwnerID)
  28. if err != nil {
  29. ctx.JSON(500, map[string]interface{}{
  30. "ok": false,
  31. "error": err.Error(),
  32. })
  33. return
  34. }
  35. if u.IsOrganization() && u.IsOwnedBy(ctx.User.ID) {
  36. opts.Private = true
  37. }
  38. // FIXME: how about collaborators?
  39. }
  40. }
  41. repos, count, err := models.SearchRepositoryByName(opts)
  42. if err != nil {
  43. ctx.JSON(500, map[string]interface{}{
  44. "ok": false,
  45. "error": err.Error(),
  46. })
  47. return
  48. }
  49. results := make([]*api.Repository, len(repos))
  50. for i := range repos {
  51. if err = repos[i].GetOwner(); err != nil {
  52. ctx.JSON(500, map[string]interface{}{
  53. "ok": false,
  54. "error": err.Error(),
  55. })
  56. return
  57. }
  58. results[i] = &api.Repository{
  59. ID: repos[i].ID,
  60. FullName: path.Join(repos[i].Owner.Name, repos[i].Name),
  61. }
  62. }
  63. ctx.SetLinkHeader(int(count), setting.API.MaxResponseItems)
  64. ctx.JSON(200, map[string]interface{}{
  65. "ok": true,
  66. "data": results,
  67. })
  68. }
  69. func listUserRepositories(ctx *context.APIContext, username string) {
  70. user, err := models.GetUserByName(username)
  71. if err != nil {
  72. ctx.NotFoundOrServerError("GetUserByName", models.IsErrUserNotExist, err)
  73. return
  74. }
  75. // Only list public repositories if user requests someone else's repository list,
  76. // or an organization isn't a member of.
  77. var ownRepos []*models.Repository
  78. if user.IsOrganization() {
  79. ownRepos, _, err = user.GetUserRepositories(ctx.User.ID, 1, user.NumRepos)
  80. } else {
  81. ownRepos, err = models.GetUserRepositories(&models.UserRepoOptions{
  82. UserID: user.ID,
  83. Private: ctx.User.ID == user.ID,
  84. Page: 1,
  85. PageSize: user.NumRepos,
  86. })
  87. }
  88. if err != nil {
  89. ctx.Error(500, "GetUserRepositories", err)
  90. return
  91. }
  92. if ctx.User.ID != user.ID {
  93. repos := make([]*api.Repository, len(ownRepos))
  94. for i := range ownRepos {
  95. repos[i] = ownRepos[i].APIFormat(&api.Permission{true, true, true})
  96. }
  97. ctx.JSON(200, &repos)
  98. return
  99. }
  100. accessibleRepos, err := user.GetRepositoryAccesses()
  101. if err != nil {
  102. ctx.Error(500, "GetRepositoryAccesses", err)
  103. return
  104. }
  105. numOwnRepos := len(ownRepos)
  106. repos := make([]*api.Repository, numOwnRepos+len(accessibleRepos))
  107. for i := range ownRepos {
  108. repos[i] = ownRepos[i].APIFormat(&api.Permission{true, true, true})
  109. }
  110. i := numOwnRepos
  111. for repo, access := range accessibleRepos {
  112. repos[i] = repo.APIFormat(&api.Permission{
  113. Admin: access >= models.ACCESS_MODE_ADMIN,
  114. Push: access >= models.ACCESS_MODE_WRITE,
  115. Pull: true,
  116. })
  117. i++
  118. }
  119. ctx.JSON(200, &repos)
  120. }
  121. func ListMyRepos(ctx *context.APIContext) {
  122. listUserRepositories(ctx, ctx.User.Name)
  123. }
  124. func ListUserRepositories(ctx *context.APIContext) {
  125. listUserRepositories(ctx, ctx.Params(":username"))
  126. }
  127. func ListOrgRepositories(ctx *context.APIContext) {
  128. listUserRepositories(ctx, ctx.Params(":org"))
  129. }
  130. func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateRepoOption) {
  131. repo, err := models.CreateRepository(ctx.User, owner, models.CreateRepoOptions{
  132. Name: opt.Name,
  133. Description: opt.Description,
  134. Gitignores: opt.Gitignores,
  135. License: opt.License,
  136. Readme: opt.Readme,
  137. IsPrivate: opt.Private,
  138. AutoInit: opt.AutoInit,
  139. })
  140. if err != nil {
  141. if models.IsErrRepoAlreadyExist(err) ||
  142. models.IsErrNameReserved(err) ||
  143. models.IsErrNamePatternNotAllowed(err) {
  144. ctx.Error(422, "", err)
  145. } else {
  146. if repo != nil {
  147. if err = models.DeleteRepository(ctx.User.ID, repo.ID); err != nil {
  148. log.Error(4, "DeleteRepository: %v", err)
  149. }
  150. }
  151. ctx.Error(500, "CreateRepository", err)
  152. }
  153. return
  154. }
  155. ctx.JSON(201, repo.APIFormat(&api.Permission{true, true, true}))
  156. }
  157. // https://github.com/gogits/go-gogs-client/wiki/Repositories#create
  158. func Create(ctx *context.APIContext, opt api.CreateRepoOption) {
  159. // Shouldn't reach this condition, but just in case.
  160. if ctx.User.IsOrganization() {
  161. ctx.Error(422, "", "not allowed creating repository for organization")
  162. return
  163. }
  164. CreateUserRepo(ctx, ctx.User, opt)
  165. }
  166. func CreateOrgRepo(ctx *context.APIContext, opt api.CreateRepoOption) {
  167. org, err := models.GetOrgByName(ctx.Params(":org"))
  168. if err != nil {
  169. if models.IsErrUserNotExist(err) {
  170. ctx.Error(422, "", err)
  171. } else {
  172. ctx.Error(500, "GetOrgByName", err)
  173. }
  174. return
  175. }
  176. if !org.IsOwnedBy(ctx.User.ID) {
  177. ctx.Error(403, "", "Given user is not owner of organization.")
  178. return
  179. }
  180. CreateUserRepo(ctx, org, opt)
  181. }
  182. // https://github.com/gogits/go-gogs-client/wiki/Repositories#migrate
  183. func Migrate(ctx *context.APIContext, f form.MigrateRepo) {
  184. ctxUser := ctx.User
  185. // Not equal means context user is an organization,
  186. // or is another user/organization if current user is admin.
  187. if f.Uid != ctxUser.ID {
  188. org, err := models.GetUserByID(f.Uid)
  189. if err != nil {
  190. if models.IsErrUserNotExist(err) {
  191. ctx.Error(422, "", err)
  192. } else {
  193. ctx.Error(500, "GetUserByID", err)
  194. }
  195. return
  196. }
  197. ctxUser = org
  198. }
  199. if ctx.HasError() {
  200. ctx.Error(422, "", ctx.GetErrMsg())
  201. return
  202. }
  203. if ctxUser.IsOrganization() && !ctx.User.IsAdmin {
  204. // Check ownership of organization.
  205. if !ctxUser.IsOwnedBy(ctx.User.ID) {
  206. ctx.Error(403, "", "Given user is not owner of organization.")
  207. return
  208. }
  209. }
  210. remoteAddr, err := f.ParseRemoteAddr(ctx.User)
  211. if err != nil {
  212. if models.IsErrInvalidCloneAddr(err) {
  213. addrErr := err.(models.ErrInvalidCloneAddr)
  214. switch {
  215. case addrErr.IsURLError:
  216. ctx.Error(422, "", err)
  217. case addrErr.IsPermissionDenied:
  218. ctx.Error(422, "", "You are not allowed to import local repositories.")
  219. case addrErr.IsInvalidPath:
  220. ctx.Error(422, "", "Invalid local path, it does not exist or not a directory.")
  221. default:
  222. ctx.Error(500, "ParseRemoteAddr", "Unknown error type (ErrInvalidCloneAddr): "+err.Error())
  223. }
  224. } else {
  225. ctx.Error(500, "ParseRemoteAddr", err)
  226. }
  227. return
  228. }
  229. repo, err := models.MigrateRepository(ctx.User, ctxUser, models.MigrateRepoOptions{
  230. Name: f.RepoName,
  231. Description: f.Description,
  232. IsPrivate: f.Private || setting.Repository.ForcePrivate,
  233. IsMirror: f.Mirror,
  234. RemoteAddr: remoteAddr,
  235. })
  236. if err != nil {
  237. if repo != nil {
  238. if errDelete := models.DeleteRepository(ctxUser.ID, repo.ID); errDelete != nil {
  239. log.Error(4, "DeleteRepository: %v", errDelete)
  240. }
  241. }
  242. ctx.Error(500, "MigrateRepository", models.HandleCloneUserCredentials(err.Error(), true))
  243. return
  244. }
  245. log.Trace("Repository migrated: %s/%s", ctxUser.Name, f.RepoName)
  246. ctx.JSON(201, repo.APIFormat(&api.Permission{true, true, true}))
  247. }
  248. func parseOwnerAndRepo(ctx *context.APIContext) (*models.User, *models.Repository) {
  249. owner, err := models.GetUserByName(ctx.Params(":username"))
  250. if err != nil {
  251. if models.IsErrUserNotExist(err) {
  252. ctx.Error(422, "", err)
  253. } else {
  254. ctx.Error(500, "GetUserByName", err)
  255. }
  256. return nil, nil
  257. }
  258. repo, err := models.GetRepositoryByName(owner.ID, ctx.Params(":reponame"))
  259. if err != nil {
  260. if models.IsErrRepoNotExist(err) {
  261. ctx.Status(404)
  262. } else {
  263. ctx.Error(500, "GetRepositoryByName", err)
  264. }
  265. return nil, nil
  266. }
  267. return owner, repo
  268. }
  269. // https://github.com/gogits/go-gogs-client/wiki/Repositories#get
  270. func Get(ctx *context.APIContext) {
  271. _, repo := parseOwnerAndRepo(ctx)
  272. if ctx.Written() {
  273. return
  274. }
  275. ctx.JSON(200, repo.APIFormat(&api.Permission{true, true, true}))
  276. }
  277. // https://github.com/gogits/go-gogs-client/wiki/Repositories#delete
  278. func Delete(ctx *context.APIContext) {
  279. owner, repo := parseOwnerAndRepo(ctx)
  280. if ctx.Written() {
  281. return
  282. }
  283. if owner.IsOrganization() && !owner.IsOwnedBy(ctx.User.ID) {
  284. ctx.Error(403, "", "Given user is not owner of organization.")
  285. return
  286. }
  287. if err := models.DeleteRepository(owner.ID, repo.ID); err != nil {
  288. ctx.Error(500, "DeleteRepository", err)
  289. return
  290. }
  291. log.Trace("Repository deleted: %s/%s", owner.Name, repo.Name)
  292. ctx.Status(204)
  293. }
  294. func ListForks(ctx *context.APIContext) {
  295. forks, err := ctx.Repo.Repository.GetForks()
  296. if err != nil {
  297. ctx.Error(500, "GetForks", err)
  298. return
  299. }
  300. apiForks := make([]*api.Repository, len(forks))
  301. for i := range forks {
  302. if err := forks[i].GetOwner(); err != nil {
  303. ctx.Error(500, "GetOwner", err)
  304. return
  305. }
  306. apiForks[i] = forks[i].APIFormat(&api.Permission{
  307. Admin: ctx.User.IsAdminOfRepo(forks[i]),
  308. Push: ctx.User.IsWriterOfRepo(forks[i]),
  309. Pull: true,
  310. })
  311. }
  312. ctx.JSON(200, &apiForks)
  313. }