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.

108 lines
2.6 KiB

  1. // Copyright 2016 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 repo
  5. import (
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/context"
  8. "code.gitea.io/gitea/routers/api/v1/utils"
  9. api "code.gitea.io/sdk/gitea"
  10. )
  11. // ListForks list a repository's forks
  12. func ListForks(ctx *context.APIContext) {
  13. // swagger:operation GET /repos/{owner}/{repo}/forks repository listForks
  14. // ---
  15. // summary: List a repository's forks
  16. // produces:
  17. // - application/json
  18. // parameters:
  19. // - name: owner
  20. // in: path
  21. // description: owner of the repo
  22. // type: string
  23. // required: true
  24. // - name: repo
  25. // in: path
  26. // description: name of the repo
  27. // type: string
  28. // required: true
  29. // responses:
  30. // "200":
  31. // "$ref": "#/responses/RepositoryList"
  32. forks, err := ctx.Repo.Repository.GetForks()
  33. if err != nil {
  34. ctx.Error(500, "GetForks", err)
  35. return
  36. }
  37. apiForks := make([]*api.Repository, len(forks))
  38. for i, fork := range forks {
  39. access, err := models.AccessLevel(utils.UserID(ctx), fork)
  40. if err != nil {
  41. ctx.Error(500, "AccessLevel", err)
  42. return
  43. }
  44. apiForks[i] = fork.APIFormat(access)
  45. }
  46. ctx.JSON(200, apiForks)
  47. }
  48. // CreateFork create a fork of a repo
  49. func CreateFork(ctx *context.APIContext, form api.CreateForkOption) {
  50. // swagger:operation POST /repos/{owner}/{repo}/forks repository createFork
  51. // ---
  52. // summary: Fork a repository
  53. // produces:
  54. // - application/json
  55. // parameters:
  56. // - name: owner
  57. // in: path
  58. // description: owner of the repo to fork
  59. // type: string
  60. // required: true
  61. // - name: repo
  62. // in: path
  63. // description: name of the repo to fork
  64. // type: string
  65. // required: true
  66. // - name: body
  67. // in: body
  68. // schema:
  69. // "$ref": "#/definitions/CreateForkOption"
  70. // responses:
  71. // "202":
  72. // "$ref": "#/responses/Repository"
  73. repo := ctx.Repo.Repository
  74. var forker *models.User // user/org that will own the fork
  75. if form.Organization == nil {
  76. forker = ctx.User
  77. } else {
  78. org, err := models.GetOrgByName(*form.Organization)
  79. if err != nil {
  80. if models.IsErrOrgNotExist(err) {
  81. ctx.Error(422, "", err)
  82. } else {
  83. ctx.Error(500, "GetOrgByName", err)
  84. }
  85. return
  86. }
  87. isMember, err := org.IsOrgMember(ctx.User.ID)
  88. if err != nil {
  89. ctx.ServerError("IsOrgMember", err)
  90. return
  91. } else if !isMember {
  92. ctx.Status(403)
  93. return
  94. }
  95. forker = org
  96. }
  97. fork, err := models.ForkRepository(ctx.User, forker, repo, repo.Name, repo.Description)
  98. if err != nil {
  99. ctx.Error(500, "ForkRepository", err)
  100. return
  101. }
  102. ctx.JSON(202, fork.APIFormat(models.AccessModeOwner))
  103. }