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.

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