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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "code.gitea.io/gitea/modules/context"
  8. "code.gitea.io/gitea/modules/convert"
  9. "code.gitea.io/gitea/modules/git"
  10. api "code.gitea.io/gitea/modules/structs"
  11. )
  12. // GetBranch get a branch of a repository
  13. func GetBranch(ctx *context.APIContext) {
  14. // swagger:operation GET /repos/{owner}/{repo}/branches/{branch} repository repoGetBranch
  15. // ---
  16. // summary: Retrieve a specific branch from a repository, including its effective branch protection
  17. // produces:
  18. // - application/json
  19. // parameters:
  20. // - name: owner
  21. // in: path
  22. // description: owner of the repo
  23. // type: string
  24. // required: true
  25. // - name: repo
  26. // in: path
  27. // description: name of the repo
  28. // type: string
  29. // required: true
  30. // - name: branch
  31. // in: path
  32. // description: branch to get
  33. // type: string
  34. // required: true
  35. // responses:
  36. // "200":
  37. // "$ref": "#/responses/Branch"
  38. if ctx.Repo.TreePath != "" {
  39. // if TreePath != "", then URL contained extra slashes
  40. // (i.e. "master/subbranch" instead of "master"), so branch does
  41. // not exist
  42. ctx.NotFound()
  43. return
  44. }
  45. branch, err := ctx.Repo.Repository.GetBranch(ctx.Repo.BranchName)
  46. if err != nil {
  47. if git.IsErrBranchNotExist(err) {
  48. ctx.NotFound(err)
  49. } else {
  50. ctx.Error(500, "GetBranch", err)
  51. }
  52. return
  53. }
  54. c, err := branch.GetCommit()
  55. if err != nil {
  56. ctx.Error(500, "GetCommit", err)
  57. return
  58. }
  59. branchProtection, err := ctx.Repo.Repository.GetBranchProtection(ctx.Repo.BranchName)
  60. if err != nil {
  61. ctx.Error(500, "GetBranchProtection", err)
  62. return
  63. }
  64. ctx.JSON(200, convert.ToBranch(ctx.Repo.Repository, branch, c, branchProtection, ctx.User))
  65. }
  66. // ListBranches list all the branches of a repository
  67. func ListBranches(ctx *context.APIContext) {
  68. // swagger:operation GET /repos/{owner}/{repo}/branches repository repoListBranches
  69. // ---
  70. // summary: List a repository's branches
  71. // produces:
  72. // - application/json
  73. // parameters:
  74. // - name: owner
  75. // in: path
  76. // description: owner of the repo
  77. // type: string
  78. // required: true
  79. // - name: repo
  80. // in: path
  81. // description: name of the repo
  82. // type: string
  83. // required: true
  84. // responses:
  85. // "200":
  86. // "$ref": "#/responses/BranchList"
  87. branches, err := ctx.Repo.Repository.GetBranches()
  88. if err != nil {
  89. ctx.Error(500, "GetBranches", err)
  90. return
  91. }
  92. apiBranches := make([]*api.Branch, len(branches))
  93. for i := range branches {
  94. c, err := branches[i].GetCommit()
  95. if err != nil {
  96. ctx.Error(500, "GetCommit", err)
  97. return
  98. }
  99. branchProtection, err := ctx.Repo.Repository.GetBranchProtection(branches[i].Name)
  100. if err != nil {
  101. ctx.Error(500, "GetBranchProtection", err)
  102. return
  103. }
  104. apiBranches[i] = convert.ToBranch(ctx.Repo.Repository, branches[i], c, branchProtection, ctx.User)
  105. }
  106. ctx.JSON(200, &apiBranches)
  107. }