repos.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // Check visibility.
  28. if ctx.IsSigned && opt.Uid > 0 {
  29. if ctx.User.Id == opt.Uid {
  30. opt.Private = true
  31. } else {
  32. u, err := models.GetUserById(opt.Uid)
  33. if err != nil {
  34. ctx.JSON(500, map[string]interface{}{
  35. "ok": false,
  36. "error": err.Error(),
  37. })
  38. return
  39. }
  40. if u.IsOrganization() && u.IsOrgOwner(ctx.User.Id) {
  41. opt.Private = true
  42. }
  43. // FIXME: how about collaborators?
  44. }
  45. }
  46. repos, err := models.SearchRepositoryByName(opt)
  47. if err != nil {
  48. ctx.JSON(500, map[string]interface{}{
  49. "ok": false,
  50. "error": err.Error(),
  51. })
  52. return
  53. }
  54. results := make([]*repo, len(repos))
  55. for i := range repos {
  56. if err = repos[i].GetOwner(); err != nil {
  57. ctx.JSON(500, map[string]interface{}{
  58. "ok": false,
  59. "error": err.Error(),
  60. })
  61. return
  62. }
  63. results[i] = &repo{
  64. RepoLink: path.Join(repos[i].Owner.Name, repos[i].Name),
  65. }
  66. }
  67. ctx.Render.JSON(200, map[string]interface{}{
  68. "ok": true,
  69. "data": results,
  70. })
  71. }
  72. func Migrate(ctx *middleware.Context, form auth.MigrateRepoForm) {
  73. u, err := models.GetUserByName(ctx.Query("username"))
  74. if err != nil {
  75. ctx.JSON(500, map[string]interface{}{
  76. "ok": false,
  77. "error": err.Error(),
  78. })
  79. return
  80. }
  81. if !u.ValidtePassword(ctx.Query("password")) {
  82. ctx.JSON(500, map[string]interface{}{
  83. "ok": false,
  84. "error": "username or password is not correct",
  85. })
  86. return
  87. }
  88. ctxUser := u
  89. // Not equal means current user is an organization.
  90. if form.Uid != u.Id {
  91. org, err := models.GetUserById(form.Uid)
  92. if err != nil {
  93. ctx.JSON(500, map[string]interface{}{
  94. "ok": false,
  95. "error": err.Error(),
  96. })
  97. return
  98. }
  99. ctxUser = org
  100. }
  101. if ctx.HasError() {
  102. ctx.JSON(500, map[string]interface{}{
  103. "ok": false,
  104. "error": ctx.GetErrMsg(),
  105. })
  106. return
  107. }
  108. if ctxUser.IsOrganization() {
  109. // Check ownership of organization.
  110. if !ctxUser.IsOrgOwner(u.Id) {
  111. ctx.JSON(403, map[string]interface{}{
  112. "ok": false,
  113. "error": "given user is not owner of organization",
  114. })
  115. return
  116. }
  117. }
  118. authStr := strings.Replace(fmt.Sprintf("://%s:%s",
  119. form.AuthUserName, form.AuthPasswd), "@", "%40", -1)
  120. url := strings.Replace(form.HttpsUrl, "://", authStr+"@", 1)
  121. repo, err := models.MigrateRepository(ctxUser, form.RepoName, form.Description, form.Private,
  122. form.Mirror, url)
  123. if err == nil {
  124. log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
  125. ctx.JSON(200, map[string]interface{}{
  126. "ok": true,
  127. "data": "/" + ctxUser.Name + "/" + form.RepoName,
  128. })
  129. return
  130. }
  131. if repo != nil {
  132. if errDelete := models.DeleteRepository(ctxUser.Id, repo.Id, ctxUser.Name); errDelete != nil {
  133. log.Error(4, "DeleteRepository: %v", errDelete)
  134. }
  135. }
  136. ctx.JSON(500, map[string]interface{}{
  137. "ok": false,
  138. "error": err.Error(),
  139. })
  140. }