repos.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 v1
  5. import (
  6. "fmt"
  7. "path"
  8. "strings"
  9. "github.com/Unknwon/com"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/modules/auth"
  12. "github.com/gogits/gogs/modules/log"
  13. "github.com/gogits/gogs/modules/middleware"
  14. )
  15. type repo struct {
  16. RepoLink string `json:"repolink"`
  17. }
  18. func SearchRepos(ctx *middleware.Context) {
  19. opt := models.SearchOption{
  20. Keyword: path.Base(ctx.Query("q")),
  21. Uid: com.StrTo(ctx.Query("uid")).MustInt64(),
  22. Limit: com.StrTo(ctx.Query("limit")).MustInt(),
  23. }
  24. if opt.Limit == 0 {
  25. opt.Limit = 10
  26. }
  27. repos, err := models.SearchRepositoryByName(opt)
  28. if err != nil {
  29. ctx.JSON(500, map[string]interface{}{
  30. "ok": false,
  31. "error": err.Error(),
  32. })
  33. return
  34. }
  35. results := make([]*repo, len(repos))
  36. for i := range repos {
  37. if err = repos[i].GetOwner(); err != nil {
  38. ctx.JSON(500, map[string]interface{}{
  39. "ok": false,
  40. "error": err.Error(),
  41. })
  42. return
  43. }
  44. results[i] = &repo{
  45. RepoLink: path.Join(repos[i].Owner.Name, repos[i].Name),
  46. }
  47. }
  48. ctx.Render.JSON(200, map[string]interface{}{
  49. "ok": true,
  50. "data": results,
  51. })
  52. }
  53. func Migrate(ctx *middleware.Context, form auth.MigrateRepoForm) {
  54. ctxUser := ctx.User
  55. // Not equal means current user is an organization.
  56. if form.Uid != ctx.User.Id {
  57. org, err := models.GetUserById(form.Uid)
  58. if err != nil && err != models.ErrUserNotExist {
  59. ctx.JSON(500, map[string]interface{}{
  60. "ok": false,
  61. "data": err.Error(),
  62. })
  63. return
  64. }
  65. ctxUser = org
  66. }
  67. if err := ctx.User.GetOrganizations(); err != nil {
  68. ctx.JSON(500, map[string]interface{}{
  69. "ok": false,
  70. "data": err.Error(),
  71. })
  72. return
  73. }
  74. if ctx.HasError() {
  75. ctx.JSON(500, map[string]interface{}{
  76. "ok": false,
  77. "data": ctx.GetErrMsg(),
  78. })
  79. return
  80. }
  81. if ctxUser.IsOrganization() {
  82. // Check ownership of organization.
  83. if !ctxUser.IsOrgOwner(ctx.User.Id) {
  84. ctx.JSON(403, map[string]interface{}{
  85. "ok": false,
  86. "data": "Not allowed",
  87. })
  88. return
  89. }
  90. }
  91. authStr := strings.Replace(fmt.Sprintf("://%s:%s",
  92. form.AuthUserName, form.AuthPasswd), "@", "%40", -1)
  93. url := strings.Replace(form.HttpsUrl, "://", authStr+"@", 1)
  94. repo, err := models.MigrateRepository(ctxUser, form.RepoName, form.Description, form.Private,
  95. form.Mirror, url)
  96. if err == nil {
  97. log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
  98. ctx.JSON(200,
  99. map[string]interface{}{
  100. "ok": true,
  101. "data": "/" + ctxUser.Name + "/" + form.RepoName,
  102. })
  103. return
  104. } else if err == models.ErrRepoAlreadyExist {
  105. ctx.JSON(500,
  106. map[string]interface{}{
  107. "ok": false,
  108. "data": err.Error(),
  109. })
  110. return
  111. } else if err == models.ErrRepoNameIllegal {
  112. ctx.JSON(500, map[string]interface{}{
  113. "ok": false,
  114. "data": err.Error(),
  115. })
  116. return
  117. }
  118. if repo != nil {
  119. if errDelete := models.DeleteRepository(ctxUser.Id, repo.Id, ctxUser.Name); errDelete != nil {
  120. log.Error(4, "DeleteRepository: %v", errDelete)
  121. }
  122. }
  123. ctx.JSON(500, map[string]interface{}{
  124. "ok": false,
  125. "data": err.Error(),
  126. })
  127. }