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.

branch.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "net/http"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/convert"
  10. "code.gitea.io/gitea/modules/git"
  11. api "code.gitea.io/gitea/modules/structs"
  12. )
  13. // GetBranch get a branch of a repository
  14. func GetBranch(ctx *context.APIContext) {
  15. // swagger:operation GET /repos/{owner}/{repo}/branches/{branch} repository repoGetBranch
  16. // ---
  17. // summary: Retrieve a specific branch from a repository, including its effective branch protection
  18. // produces:
  19. // - application/json
  20. // parameters:
  21. // - name: owner
  22. // in: path
  23. // description: owner of the repo
  24. // type: string
  25. // required: true
  26. // - name: repo
  27. // in: path
  28. // description: name of the repo
  29. // type: string
  30. // required: true
  31. // - name: branch
  32. // in: path
  33. // description: branch to get
  34. // type: string
  35. // required: true
  36. // responses:
  37. // "200":
  38. // "$ref": "#/responses/Branch"
  39. if ctx.Repo.TreePath != "" {
  40. // if TreePath != "", then URL contained extra slashes
  41. // (i.e. "master/subbranch" instead of "master"), so branch does
  42. // not exist
  43. ctx.NotFound()
  44. return
  45. }
  46. branch, err := ctx.Repo.Repository.GetBranch(ctx.Repo.BranchName)
  47. if err != nil {
  48. if git.IsErrBranchNotExist(err) {
  49. ctx.NotFound(err)
  50. } else {
  51. ctx.Error(http.StatusInternalServerError, "GetBranch", err)
  52. }
  53. return
  54. }
  55. c, err := branch.GetCommit()
  56. if err != nil {
  57. ctx.Error(http.StatusInternalServerError, "GetCommit", err)
  58. return
  59. }
  60. branchProtection, err := ctx.Repo.Repository.GetBranchProtection(ctx.Repo.BranchName)
  61. if err != nil {
  62. ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
  63. return
  64. }
  65. br, err := convert.ToBranch(ctx.Repo.Repository, branch, c, branchProtection, ctx.User)
  66. if err != nil {
  67. ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
  68. return
  69. }
  70. ctx.JSON(http.StatusOK, br)
  71. }
  72. // ListBranches list all the branches of a repository
  73. func ListBranches(ctx *context.APIContext) {
  74. // swagger:operation GET /repos/{owner}/{repo}/branches repository repoListBranches
  75. // ---
  76. // summary: List a repository's branches
  77. // produces:
  78. // - application/json
  79. // parameters:
  80. // - name: owner
  81. // in: path
  82. // description: owner of the repo
  83. // type: string
  84. // required: true
  85. // - name: repo
  86. // in: path
  87. // description: name of the repo
  88. // type: string
  89. // required: true
  90. // responses:
  91. // "200":
  92. // "$ref": "#/responses/BranchList"
  93. branches, err := ctx.Repo.Repository.GetBranches()
  94. if err != nil {
  95. ctx.Error(http.StatusInternalServerError, "GetBranches", err)
  96. return
  97. }
  98. apiBranches := make([]*api.Branch, len(branches))
  99. for i := range branches {
  100. c, err := branches[i].GetCommit()
  101. if err != nil {
  102. ctx.Error(http.StatusInternalServerError, "GetCommit", err)
  103. return
  104. }
  105. branchProtection, err := ctx.Repo.Repository.GetBranchProtection(branches[i].Name)
  106. if err != nil {
  107. ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
  108. return
  109. }
  110. br, err := convert.ToBranch(ctx.Repo.Repository, branches[i], c, branchProtection, ctx.User)
  111. if err != nil {
  112. ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
  113. return
  114. }
  115. apiBranches[i] = br
  116. }
  117. ctx.JSON(http.StatusOK, &apiBranches)
  118. }