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

repo.go 3.2 kB

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