repo.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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/auth"
  11. "github.com/gogits/gogs/modules/context"
  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. // https://github.com/gogits/go-gogs-client/wiki/Repositories#list-your-repositories
  70. func ListMyRepos(ctx *context.APIContext) {
  71. ownRepos, err := models.GetUserRepositories(&models.UserRepoOptions{
  72. UserID: ctx.User.ID,
  73. Private: true,
  74. Page: 1,
  75. PageSize: ctx.User.NumRepos,
  76. })
  77. if err != nil {
  78. ctx.Error(500, "GetRepositories", err)
  79. return
  80. }
  81. numOwnRepos := len(ownRepos)
  82. accessibleRepos, err := ctx.User.GetRepositoryAccesses()
  83. if err != nil {
  84. ctx.Error(500, "GetRepositoryAccesses", err)
  85. return
  86. }
  87. repos := make([]*api.Repository, numOwnRepos+len(accessibleRepos))
  88. for i := range ownRepos {
  89. repos[i] = ownRepos[i].APIFormat(&api.Permission{true, true, true})
  90. }
  91. i := numOwnRepos
  92. for repo, access := range accessibleRepos {
  93. repos[i] = repo.APIFormat(&api.Permission{
  94. Admin: access >= models.ACCESS_MODE_ADMIN,
  95. Push: access >= models.ACCESS_MODE_WRITE,
  96. Pull: true,
  97. })
  98. i++
  99. }
  100. ctx.JSON(200, &repos)
  101. }
  102. func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateRepoOption) {
  103. repo, err := models.CreateRepository(owner, models.CreateRepoOptions{
  104. Name: opt.Name,
  105. Description: opt.Description,
  106. Gitignores: opt.Gitignores,
  107. License: opt.License,
  108. Readme: opt.Readme,
  109. IsPrivate: opt.Private,
  110. AutoInit: opt.AutoInit,
  111. })
  112. if err != nil {
  113. if models.IsErrRepoAlreadyExist(err) ||
  114. models.IsErrNameReserved(err) ||
  115. models.IsErrNamePatternNotAllowed(err) {
  116. ctx.Error(422, "", err)
  117. } else {
  118. if repo != nil {
  119. if err = models.DeleteRepository(ctx.User.ID, repo.ID); err != nil {
  120. log.Error(4, "DeleteRepository: %v", err)
  121. }
  122. }
  123. ctx.Error(500, "CreateRepository", err)
  124. }
  125. return
  126. }
  127. ctx.JSON(201, repo.APIFormat(&api.Permission{true, true, true}))
  128. }
  129. // https://github.com/gogits/go-gogs-client/wiki/Repositories#create
  130. func Create(ctx *context.APIContext, opt api.CreateRepoOption) {
  131. // Shouldn't reach this condition, but just in case.
  132. if ctx.User.IsOrganization() {
  133. ctx.Error(422, "", "not allowed creating repository for organization")
  134. return
  135. }
  136. CreateUserRepo(ctx, ctx.User, opt)
  137. }
  138. func CreateOrgRepo(ctx *context.APIContext, opt api.CreateRepoOption) {
  139. org, err := models.GetOrgByName(ctx.Params(":org"))
  140. if err != nil {
  141. if models.IsErrUserNotExist(err) {
  142. ctx.Error(422, "", err)
  143. } else {
  144. ctx.Error(500, "GetOrgByName", err)
  145. }
  146. return
  147. }
  148. if !org.IsOwnedBy(ctx.User.ID) {
  149. ctx.Error(403, "", "Given user is not owner of organization.")
  150. return
  151. }
  152. CreateUserRepo(ctx, org, opt)
  153. }
  154. // https://github.com/gogits/go-gogs-client/wiki/Repositories#migrate
  155. func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
  156. ctxUser := ctx.User
  157. // Not equal means context user is an organization,
  158. // or is another user/organization if current user is admin.
  159. if form.Uid != ctxUser.ID {
  160. org, err := models.GetUserByID(form.Uid)
  161. if err != nil {
  162. if models.IsErrUserNotExist(err) {
  163. ctx.Error(422, "", err)
  164. } else {
  165. ctx.Error(500, "GetUserByID", err)
  166. }
  167. return
  168. }
  169. ctxUser = org
  170. }
  171. if ctx.HasError() {
  172. ctx.Error(422, "", ctx.GetErrMsg())
  173. return
  174. }
  175. if ctxUser.IsOrganization() && !ctx.User.IsAdmin {
  176. // Check ownership of organization.
  177. if !ctxUser.IsOwnedBy(ctx.User.ID) {
  178. ctx.Error(403, "", "Given user is not owner of organization.")
  179. return
  180. }
  181. }
  182. remoteAddr, err := form.ParseRemoteAddr(ctx.User)
  183. if err != nil {
  184. if models.IsErrInvalidCloneAddr(err) {
  185. addrErr := err.(models.ErrInvalidCloneAddr)
  186. switch {
  187. case addrErr.IsURLError:
  188. ctx.Error(422, "", err)
  189. case addrErr.IsPermissionDenied:
  190. ctx.Error(422, "", "You are not allowed to import local repositories.")
  191. case addrErr.IsInvalidPath:
  192. ctx.Error(422, "", "Invalid local path, it does not exist or not a directory.")
  193. default:
  194. ctx.Error(500, "ParseRemoteAddr", "Unknown error type (ErrInvalidCloneAddr): "+err.Error())
  195. }
  196. } else {
  197. ctx.Error(500, "ParseRemoteAddr", err)
  198. }
  199. return
  200. }
  201. repo, err := models.MigrateRepository(ctxUser, models.MigrateRepoOptions{
  202. Name: form.RepoName,
  203. Description: form.Description,
  204. IsPrivate: form.Private || setting.Repository.ForcePrivate,
  205. IsMirror: form.Mirror,
  206. RemoteAddr: remoteAddr,
  207. })
  208. if err != nil {
  209. if repo != nil {
  210. if errDelete := models.DeleteRepository(ctxUser.ID, repo.ID); errDelete != nil {
  211. log.Error(4, "DeleteRepository: %v", errDelete)
  212. }
  213. }
  214. ctx.Error(500, "MigrateRepository", models.HandleCloneUserCredentials(err.Error(), true))
  215. return
  216. }
  217. log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
  218. ctx.JSON(201, repo.APIFormat(&api.Permission{true, true, true}))
  219. }
  220. func parseOwnerAndRepo(ctx *context.APIContext) (*models.User, *models.Repository) {
  221. owner, err := models.GetUserByName(ctx.Params(":username"))
  222. if err != nil {
  223. if models.IsErrUserNotExist(err) {
  224. ctx.Error(422, "", err)
  225. } else {
  226. ctx.Error(500, "GetUserByName", err)
  227. }
  228. return nil, nil
  229. }
  230. repo, err := models.GetRepositoryByName(owner.ID, ctx.Params(":reponame"))
  231. if err != nil {
  232. if models.IsErrRepoNotExist(err) {
  233. ctx.Status(404)
  234. } else {
  235. ctx.Error(500, "GetRepositoryByName", err)
  236. }
  237. return nil, nil
  238. }
  239. return owner, repo
  240. }
  241. // https://github.com/gogits/go-gogs-client/wiki/Repositories#get
  242. func Get(ctx *context.APIContext) {
  243. _, repo := parseOwnerAndRepo(ctx)
  244. if ctx.Written() {
  245. return
  246. }
  247. ctx.JSON(200, repo.APIFormat(&api.Permission{true, true, true}))
  248. }
  249. // https://github.com/gogits/go-gogs-client/wiki/Repositories#delete
  250. func Delete(ctx *context.APIContext) {
  251. owner, repo := parseOwnerAndRepo(ctx)
  252. if ctx.Written() {
  253. return
  254. }
  255. if owner.IsOrganization() && !owner.IsOwnedBy(ctx.User.ID) {
  256. ctx.Error(403, "", "Given user is not owner of organization.")
  257. return
  258. }
  259. if err := models.DeleteRepository(owner.ID, repo.ID); err != nil {
  260. ctx.Error(500, "DeleteRepository", err)
  261. return
  262. }
  263. log.Trace("Repository deleted: %s/%s", owner.Name, repo.Name)
  264. ctx.Status(204)
  265. }
  266. func ListForks(ctx *context.APIContext) {
  267. forks, err := ctx.Repo.Repository.GetForks()
  268. if err != nil {
  269. ctx.Error(500, "GetForks", err)
  270. return
  271. }
  272. apiForks := make([]*api.Repository, len(forks))
  273. for i := range forks {
  274. if err := forks[i].GetOwner(); err != nil {
  275. ctx.Error(500, "GetOwner", err)
  276. return
  277. }
  278. apiForks[i] = forks[i].APIFormat(&api.Permission{
  279. Admin: ctx.User.IsAdminOfRepo(forks[i]),
  280. Push: ctx.User.IsWriterOfRepo(forks[i]),
  281. Pull: true,
  282. })
  283. }
  284. ctx.JSON(200, &apiForks)
  285. }