repo.go 9.3 KB

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