You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.0 KiB

  1. // Copyright 2017 The Gitea 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 user
  5. import (
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/context"
  8. api "code.gitea.io/sdk/gitea"
  9. )
  10. // listUserRepos - List the repositories owned by the given user.
  11. func listUserRepos(ctx *context.APIContext, u *models.User) {
  12. showPrivateRepos := ctx.IsSigned && (ctx.User.ID == u.ID || ctx.User.IsAdmin)
  13. repos, err := models.GetUserRepositories(u.ID, showPrivateRepos, 1, u.NumRepos, "")
  14. if err != nil {
  15. ctx.Error(500, "GetUserRepositories", err)
  16. return
  17. }
  18. apiRepos := make([]*api.Repository, len(repos))
  19. var ctxUserID int64
  20. if ctx.User != nil {
  21. ctxUserID = ctx.User.ID
  22. }
  23. for i := range repos {
  24. access, err := models.AccessLevel(ctxUserID, repos[i])
  25. if err != nil {
  26. ctx.Error(500, "AccessLevel", err)
  27. return
  28. }
  29. apiRepos[i] = repos[i].APIFormat(access)
  30. }
  31. ctx.JSON(200, &apiRepos)
  32. }
  33. // ListUserRepos - list the repos owned by the given user.
  34. func ListUserRepos(ctx *context.APIContext) {
  35. // swagger:operation GET /users/{username}/repos user userListRepos
  36. // ---
  37. // summary: List the repos owned by the given user
  38. // produces:
  39. // - application/json
  40. // parameters:
  41. // - name: username
  42. // in: path
  43. // description: username of user
  44. // type: string
  45. // required: true
  46. // responses:
  47. // "200":
  48. // "$ref": "#/responses/RepositoryList"
  49. user := GetUserByParams(ctx)
  50. if ctx.Written() {
  51. return
  52. }
  53. listUserRepos(ctx, user)
  54. }
  55. // ListMyRepos - list the repositories you own or have access to.
  56. func ListMyRepos(ctx *context.APIContext) {
  57. // swagger:operation GET /user/repos user userCurrentListRepos
  58. // ---
  59. // summary: List the repos that the authenticated user owns or has access to
  60. // produces:
  61. // - application/json
  62. // responses:
  63. // "200":
  64. // "$ref": "#/responses/RepositoryList"
  65. ownRepos, err := models.GetUserRepositories(ctx.User.ID, true, 1, ctx.User.NumRepos, "")
  66. if err != nil {
  67. ctx.Error(500, "GetUserRepositories", err)
  68. return
  69. }
  70. accessibleReposMap, err := ctx.User.GetRepositoryAccesses()
  71. if err != nil {
  72. ctx.Error(500, "GetRepositoryAccesses", err)
  73. return
  74. }
  75. apiRepos := make([]*api.Repository, len(ownRepos)+len(accessibleReposMap))
  76. for i := range ownRepos {
  77. apiRepos[i] = ownRepos[i].APIFormat(models.AccessModeOwner)
  78. }
  79. i := len(ownRepos)
  80. for repo, access := range accessibleReposMap {
  81. apiRepos[i] = repo.APIFormat(access)
  82. i++
  83. }
  84. ctx.JSON(200, &apiRepos)
  85. }
  86. // ListOrgRepos - list the repositories of an organization.
  87. func ListOrgRepos(ctx *context.APIContext) {
  88. // swagger:operation GET /orgs/{org}/repos organization orgListRepos
  89. // ---
  90. // summary: List an organization's repos
  91. // produces:
  92. // - application/json
  93. // parameters:
  94. // - name: org
  95. // in: path
  96. // description: name of the organization
  97. // type: string
  98. // required: true
  99. // responses:
  100. // "200":
  101. // "$ref": "#/responses/RepositoryList"
  102. listUserRepos(ctx, ctx.Org.Organization)
  103. }