repo.go 9.4 KB

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