repo.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. "net/url"
  7. "path"
  8. "strings"
  9. "github.com/Unknwon/com"
  10. api "github.com/gogits/go-gogs-client"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/auth"
  13. "github.com/gogits/gogs/modules/base"
  14. "github.com/gogits/gogs/modules/log"
  15. "github.com/gogits/gogs/modules/middleware"
  16. "github.com/gogits/gogs/modules/setting"
  17. )
  18. // ToApiRepository converts repository to API format.
  19. func ToApiRepository(owner *models.User, repo *models.Repository, permission api.Permission) *api.Repository {
  20. cl, err := repo.CloneLink()
  21. if err != nil {
  22. log.Error(4, "CloneLink: %v", err)
  23. }
  24. return &api.Repository{
  25. Id: repo.ID,
  26. Owner: *ToApiUser(owner),
  27. FullName: owner.Name + "/" + repo.Name,
  28. Private: repo.IsPrivate,
  29. Fork: repo.IsFork,
  30. HtmlUrl: setting.AppUrl + owner.Name + "/" + repo.Name,
  31. CloneUrl: cl.HTTPS,
  32. SshUrl: cl.SSH,
  33. Permissions: permission,
  34. }
  35. }
  36. func SearchRepos(ctx *middleware.Context) {
  37. opt := models.SearchOption{
  38. Keyword: path.Base(ctx.Query("q")),
  39. Uid: com.StrTo(ctx.Query("uid")).MustInt64(),
  40. Limit: com.StrTo(ctx.Query("limit")).MustInt(),
  41. }
  42. if opt.Limit == 0 {
  43. opt.Limit = 10
  44. }
  45. // Check visibility.
  46. if ctx.IsSigned && opt.Uid > 0 {
  47. if ctx.User.Id == opt.Uid {
  48. opt.Private = true
  49. } else {
  50. u, err := models.GetUserByID(opt.Uid)
  51. if err != nil {
  52. ctx.JSON(500, map[string]interface{}{
  53. "ok": false,
  54. "error": err.Error(),
  55. })
  56. return
  57. }
  58. if u.IsOrganization() && u.IsOwnedBy(ctx.User.Id) {
  59. opt.Private = true
  60. }
  61. // FIXME: how about collaborators?
  62. }
  63. }
  64. repos, err := models.SearchRepositoryByName(opt)
  65. if err != nil {
  66. ctx.JSON(500, map[string]interface{}{
  67. "ok": false,
  68. "error": err.Error(),
  69. })
  70. return
  71. }
  72. results := make([]*api.Repository, len(repos))
  73. for i := range repos {
  74. if err = repos[i].GetOwner(); err != nil {
  75. ctx.JSON(500, map[string]interface{}{
  76. "ok": false,
  77. "error": err.Error(),
  78. })
  79. return
  80. }
  81. results[i] = &api.Repository{
  82. Id: repos[i].ID,
  83. FullName: path.Join(repos[i].Owner.Name, repos[i].Name),
  84. }
  85. }
  86. ctx.JSON(200, map[string]interface{}{
  87. "ok": true,
  88. "data": results,
  89. })
  90. }
  91. // https://github.com/gogits/go-gogs-client/wiki/Repositories#list-your-repositories
  92. func ListMyRepos(ctx *middleware.Context) {
  93. ownRepos, err := models.GetRepositories(ctx.User.Id, true)
  94. if err != nil {
  95. ctx.JSON(500, &base.ApiJsonErr{"GetRepositories: " + err.Error(), base.DOC_URL})
  96. return
  97. }
  98. numOwnRepos := len(ownRepos)
  99. accessibleRepos, err := ctx.User.GetAccessibleRepositories()
  100. if err != nil {
  101. ctx.JSON(500, &base.ApiJsonErr{"GetAccessibleRepositories: " + err.Error(), base.DOC_URL})
  102. return
  103. }
  104. repos := make([]*api.Repository, numOwnRepos+len(accessibleRepos))
  105. for i := range ownRepos {
  106. repos[i] = ToApiRepository(ctx.User, ownRepos[i], api.Permission{true, true, true})
  107. }
  108. i := numOwnRepos
  109. for repo, access := range accessibleRepos {
  110. repos[i] = ToApiRepository(repo.Owner, repo, api.Permission{
  111. Admin: access >= models.ACCESS_MODE_ADMIN,
  112. Push: access >= models.ACCESS_MODE_WRITE,
  113. Pull: true,
  114. })
  115. i++
  116. }
  117. ctx.JSON(200, &repos)
  118. }
  119. func createRepo(ctx *middleware.Context, owner *models.User, opt api.CreateRepoOption) {
  120. repo, err := models.CreateRepository(owner, models.CreateRepoOptions{
  121. Name: opt.Name,
  122. Description: opt.Description,
  123. Gitignores: opt.Gitignores,
  124. License: opt.License,
  125. Readme: opt.Readme,
  126. IsPrivate: opt.Private,
  127. AutoInit: opt.AutoInit,
  128. })
  129. if err != nil {
  130. if models.IsErrRepoAlreadyExist(err) ||
  131. models.IsErrNameReserved(err) ||
  132. models.IsErrNamePatternNotAllowed(err) {
  133. ctx.JSON(422, &base.ApiJsonErr{err.Error(), base.DOC_URL})
  134. } else {
  135. log.Error(4, "CreateRepository: %v", err)
  136. if repo != nil {
  137. if err = models.DeleteRepository(ctx.User.Id, repo.ID); err != nil {
  138. log.Error(4, "DeleteRepository: %v", err)
  139. }
  140. }
  141. ctx.Error(500)
  142. }
  143. return
  144. }
  145. ctx.JSON(201, ToApiRepository(owner, repo, api.Permission{true, true, true}))
  146. }
  147. // https://github.com/gogits/go-gogs-client/wiki/Repositories#create
  148. func CreateRepo(ctx *middleware.Context, opt api.CreateRepoOption) {
  149. // Shouldn't reach this condition, but just in case.
  150. if ctx.User.IsOrganization() {
  151. ctx.JSON(422, "not allowed creating repository for organization")
  152. return
  153. }
  154. createRepo(ctx, ctx.User, opt)
  155. }
  156. func CreateOrgRepo(ctx *middleware.Context, opt api.CreateRepoOption) {
  157. org, err := models.GetOrgByName(ctx.Params(":org"))
  158. if err != nil {
  159. if models.IsErrUserNotExist(err) {
  160. ctx.Error(404)
  161. } else {
  162. ctx.Error(500)
  163. }
  164. return
  165. }
  166. if !org.IsOwnedBy(ctx.User.Id) {
  167. ctx.Error(403)
  168. return
  169. }
  170. createRepo(ctx, org, opt)
  171. }
  172. func MigrateRepo(ctx *middleware.Context, form auth.MigrateRepoForm) {
  173. ctxUser := ctx.User
  174. // Not equal means current user is an organization.
  175. if form.Uid != ctxUser.Id {
  176. org, err := models.GetUserByID(form.Uid)
  177. if err != nil {
  178. if models.IsErrUserNotExist(err) {
  179. ctx.HandleAPI(422, err)
  180. } else {
  181. ctx.HandleAPI(500, err)
  182. }
  183. return
  184. }
  185. ctxUser = org
  186. }
  187. if ctx.HasError() {
  188. ctx.HandleAPI(422, ctx.GetErrMsg())
  189. return
  190. }
  191. if ctxUser.IsOrganization() {
  192. // Check ownership of organization.
  193. if !ctxUser.IsOwnedBy(ctx.User.Id) {
  194. ctx.HandleAPI(403, "Given user is not owner of organization.")
  195. return
  196. }
  197. }
  198. // Remote address can be HTTP/HTTPS/Git URL or local path.
  199. remoteAddr := form.CloneAddr
  200. if strings.HasPrefix(form.CloneAddr, "http://") ||
  201. strings.HasPrefix(form.CloneAddr, "https://") ||
  202. strings.HasPrefix(form.CloneAddr, "git://") {
  203. u, err := url.Parse(form.CloneAddr)
  204. if err != nil {
  205. ctx.HandleAPI(422, err)
  206. return
  207. }
  208. if len(form.AuthUsername) > 0 || len(form.AuthPassword) > 0 {
  209. u.User = url.UserPassword(form.AuthUsername, form.AuthPassword)
  210. }
  211. remoteAddr = u.String()
  212. } else if !com.IsDir(remoteAddr) {
  213. ctx.HandleAPI(422, "Invalid local path, it does not exist or not a directory.")
  214. return
  215. }
  216. repo, err := models.MigrateRepository(ctxUser, form.RepoName, form.Description, form.Private, form.Mirror, remoteAddr)
  217. if err != nil {
  218. if repo != nil {
  219. if errDelete := models.DeleteRepository(ctxUser.Id, repo.ID); errDelete != nil {
  220. log.Error(4, "DeleteRepository: %v", errDelete)
  221. }
  222. }
  223. ctx.HandleAPI(500, err)
  224. return
  225. }
  226. log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
  227. ctx.JSON(201, ToApiRepository(ctxUser, repo, api.Permission{true, true, true}))
  228. }