repos.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. u, err := models.GetUserByName(ctx.Query("username"))
  55. if err != nil {
  56. ctx.JSON(500, map[string]interface{}{
  57. "ok": false,
  58. "error": err.Error(),
  59. })
  60. return
  61. }
  62. if !u.ValidtePassword(ctx.Query("password")) {
  63. ctx.JSON(500, map[string]interface{}{
  64. "ok": false,
  65. "error": "username or password is not correct",
  66. })
  67. return
  68. }
  69. ctxUser := u
  70. // Not equal means current user is an organization.
  71. if form.Uid != u.Id {
  72. org, err := models.GetUserById(form.Uid)
  73. if err != nil {
  74. ctx.JSON(500, map[string]interface{}{
  75. "ok": false,
  76. "error": err.Error(),
  77. })
  78. return
  79. }
  80. ctxUser = org
  81. }
  82. if ctx.HasError() {
  83. ctx.JSON(500, map[string]interface{}{
  84. "ok": false,
  85. "error": ctx.GetErrMsg(),
  86. })
  87. return
  88. }
  89. if ctxUser.IsOrganization() {
  90. // Check ownership of organization.
  91. if !ctxUser.IsOrgOwner(u.Id) {
  92. ctx.JSON(403, map[string]interface{}{
  93. "ok": false,
  94. "error": "given user is not owner of organization",
  95. })
  96. return
  97. }
  98. }
  99. authStr := strings.Replace(fmt.Sprintf("://%s:%s",
  100. form.AuthUserName, form.AuthPasswd), "@", "%40", -1)
  101. url := strings.Replace(form.HttpsUrl, "://", authStr+"@", 1)
  102. repo, err := models.MigrateRepository(ctxUser, form.RepoName, form.Description, form.Private,
  103. form.Mirror, url)
  104. if err == nil {
  105. log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
  106. ctx.JSON(200, map[string]interface{}{
  107. "ok": true,
  108. "data": "/" + ctxUser.Name + "/" + form.RepoName,
  109. })
  110. return
  111. }
  112. if repo != nil {
  113. if errDelete := models.DeleteRepository(ctxUser.Id, repo.Id, ctxUser.Name); errDelete != nil {
  114. log.Error(4, "DeleteRepository: %v", errDelete)
  115. }
  116. }
  117. ctx.JSON(500, map[string]interface{}{
  118. "ok": false,
  119. "error": err.Error(),
  120. })
  121. }