repo.go 9.8 KB

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