repo.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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/pkg/context"
  12. "github.com/gogits/gogs/pkg/form"
  13. "github.com/gogits/gogs/pkg/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.IsLogged && 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(2, "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. } else if !org.IsOrganization() {
  198. ctx.Error(403, "", "Given user is not an organization")
  199. return
  200. }
  201. ctxUser = org
  202. }
  203. if ctx.HasError() {
  204. ctx.Error(422, "", ctx.GetErrMsg())
  205. return
  206. }
  207. if ctxUser.IsOrganization() && !ctx.User.IsAdmin {
  208. // Check ownership of organization.
  209. if !ctxUser.IsOwnedBy(ctx.User.ID) {
  210. ctx.Error(403, "", "Given user is not owner of organization")
  211. return
  212. }
  213. }
  214. remoteAddr, err := f.ParseRemoteAddr(ctx.User)
  215. if err != nil {
  216. if models.IsErrInvalidCloneAddr(err) {
  217. addrErr := err.(models.ErrInvalidCloneAddr)
  218. switch {
  219. case addrErr.IsURLError:
  220. ctx.Error(422, "", err)
  221. case addrErr.IsPermissionDenied:
  222. ctx.Error(422, "", "You are not allowed to import local repositories")
  223. case addrErr.IsInvalidPath:
  224. ctx.Error(422, "", "Invalid local path, it does not exist or not a directory")
  225. default:
  226. ctx.Error(500, "ParseRemoteAddr", "Unknown error type (ErrInvalidCloneAddr): "+err.Error())
  227. }
  228. } else {
  229. ctx.Error(500, "ParseRemoteAddr", err)
  230. }
  231. return
  232. }
  233. repo, err := models.MigrateRepository(ctx.User, ctxUser, models.MigrateRepoOptions{
  234. Name: f.RepoName,
  235. Description: f.Description,
  236. IsPrivate: f.Private || setting.Repository.ForcePrivate,
  237. IsMirror: f.Mirror,
  238. RemoteAddr: remoteAddr,
  239. })
  240. if err != nil {
  241. if repo != nil {
  242. if errDelete := models.DeleteRepository(ctxUser.ID, repo.ID); errDelete != nil {
  243. log.Error(2, "DeleteRepository: %v", errDelete)
  244. }
  245. }
  246. if errors.IsReachLimitOfRepo(err) {
  247. ctx.Error(422, "", err)
  248. } else {
  249. ctx.Error(500, "MigrateRepository", models.HandleMirrorCredentials(err.Error(), true))
  250. }
  251. return
  252. }
  253. log.Trace("Repository migrated: %s/%s", ctxUser.Name, f.RepoName)
  254. ctx.JSON(201, repo.APIFormat(&api.Permission{true, true, true}))
  255. }
  256. func parseOwnerAndRepo(ctx *context.APIContext) (*models.User, *models.Repository) {
  257. owner, err := models.GetUserByName(ctx.Params(":username"))
  258. if err != nil {
  259. if errors.IsUserNotExist(err) {
  260. ctx.Error(422, "", err)
  261. } else {
  262. ctx.Error(500, "GetUserByName", err)
  263. }
  264. return nil, nil
  265. }
  266. repo, err := models.GetRepositoryByName(owner.ID, ctx.Params(":reponame"))
  267. if err != nil {
  268. if errors.IsRepoNotExist(err) {
  269. ctx.Status(404)
  270. } else {
  271. ctx.Error(500, "GetRepositoryByName", err)
  272. }
  273. return nil, nil
  274. }
  275. return owner, repo
  276. }
  277. // https://github.com/gogits/go-gogs-client/wiki/Repositories#get
  278. func Get(ctx *context.APIContext) {
  279. _, repo := parseOwnerAndRepo(ctx)
  280. if ctx.Written() {
  281. return
  282. }
  283. ctx.JSON(200, repo.APIFormat(&api.Permission{
  284. Admin: ctx.Repo.IsAdmin(),
  285. Push: ctx.Repo.IsWriter(),
  286. Pull: true,
  287. }))
  288. }
  289. // https://github.com/gogits/go-gogs-client/wiki/Repositories#delete
  290. func Delete(ctx *context.APIContext) {
  291. owner, repo := parseOwnerAndRepo(ctx)
  292. if ctx.Written() {
  293. return
  294. }
  295. if owner.IsOrganization() && !owner.IsOwnedBy(ctx.User.ID) {
  296. ctx.Error(403, "", "Given user is not owner of organization.")
  297. return
  298. }
  299. if err := models.DeleteRepository(owner.ID, repo.ID); err != nil {
  300. ctx.Error(500, "DeleteRepository", err)
  301. return
  302. }
  303. log.Trace("Repository deleted: %s/%s", owner.Name, repo.Name)
  304. ctx.Status(204)
  305. }
  306. func ListForks(ctx *context.APIContext) {
  307. forks, err := ctx.Repo.Repository.GetForks()
  308. if err != nil {
  309. ctx.Error(500, "GetForks", err)
  310. return
  311. }
  312. apiForks := make([]*api.Repository, len(forks))
  313. for i := range forks {
  314. if err := forks[i].GetOwner(); err != nil {
  315. ctx.Error(500, "GetOwner", err)
  316. return
  317. }
  318. apiForks[i] = forks[i].APIFormat(&api.Permission{
  319. Admin: ctx.User.IsAdminOfRepo(forks[i]),
  320. Push: ctx.User.IsWriterOfRepo(forks[i]),
  321. Pull: true,
  322. })
  323. }
  324. ctx.JSON(200, &apiForks)
  325. }
  326. func MirrorSync(ctx *context.APIContext) {
  327. _, repo := parseOwnerAndRepo(ctx)
  328. if ctx.Written() {
  329. return
  330. } else if !repo.IsMirror {
  331. ctx.Status(404)
  332. return
  333. }
  334. go models.MirrorQueue.Add(repo.ID)
  335. ctx.Status(202)
  336. }