本站源代码
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.0KB

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