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.

branch.go 19 kB

Improve listing performance by using go-git (#6478) * Use go-git for tree reading and commit info lookup. Signed-off-by: Filip Navara <navara@emclient.com> * Use TreeEntry.IsRegular() instead of ObjectType that was removed. Signed-off-by: Filip Navara <navara@emclient.com> * Use the treePath to optimize commit info search. Signed-off-by: Filip Navara <navara@emclient.com> * Extract the latest commit at treePath along with the other commits. Signed-off-by: Filip Navara <navara@emclient.com> * Fix listing commit info for a directory that was created in one commit and never modified after. Signed-off-by: Filip Navara <navara@emclient.com> * Avoid nearly all external 'git' invocations when doing directory listing (.editorconfig code path is still hit). Signed-off-by: Filip Navara <navara@emclient.com> * Use go-git for reading blobs. Signed-off-by: Filip Navara <navara@emclient.com> * Make SHA1 type alias for plumbing.Hash in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Make Signature type alias for object.Signature in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Fix GetCommitsInfo for repository with only one commit. Signed-off-by: Filip Navara <navara@emclient.com> * Fix PGP signature verification. Signed-off-by: Filip Navara <navara@emclient.com> * Fix issues with walking commit graph across merges. Signed-off-by: Filip Navara <navara@emclient.com> * Fix typo in condition. Signed-off-by: Filip Navara <navara@emclient.com> * Speed up loading branch list by keeping the repository reference (and thus all the loaded packfile indexes). Signed-off-by: Filip Navara <navara@emclient.com> * Fix lising submodules. Signed-off-by: Filip Navara <navara@emclient.com> * Fix build Signed-off-by: Filip Navara <navara@emclient.com> * Add back commit cache because of name-rev Signed-off-by: Filip Navara <navara@emclient.com> * Fix tests Signed-off-by: Filip Navara <navara@emclient.com> * Fix code style * Fix spelling * Address PR feedback Signed-off-by: Filip Navara <navara@emclient.com> * Update vendor module list Signed-off-by: Filip Navara <navara@emclient.com> * Fix getting trees by commit id Signed-off-by: Filip Navara <navara@emclient.com> * Fix remaining unit test failures * Fix GetTreeBySHA * Avoid running `git name-rev` if not necessary Signed-off-by: Filip Navara <navara@emclient.com> * Move Branch code to git module * Clean up GPG signature verification and fix it for tagged commits * Address PR feedback (import formatting, copyright headers) * Make blob lookup by SHA working * Update tests to use public API * Allow getting content from any type of object through the blob interface * Change test to actually expect the object content that is in the GIT repository * Change one more test to actually expect the object content that is in the GIT repository * Add comments
6 years ago
9 years ago
Improve listing performance by using go-git (#6478) * Use go-git for tree reading and commit info lookup. Signed-off-by: Filip Navara <navara@emclient.com> * Use TreeEntry.IsRegular() instead of ObjectType that was removed. Signed-off-by: Filip Navara <navara@emclient.com> * Use the treePath to optimize commit info search. Signed-off-by: Filip Navara <navara@emclient.com> * Extract the latest commit at treePath along with the other commits. Signed-off-by: Filip Navara <navara@emclient.com> * Fix listing commit info for a directory that was created in one commit and never modified after. Signed-off-by: Filip Navara <navara@emclient.com> * Avoid nearly all external 'git' invocations when doing directory listing (.editorconfig code path is still hit). Signed-off-by: Filip Navara <navara@emclient.com> * Use go-git for reading blobs. Signed-off-by: Filip Navara <navara@emclient.com> * Make SHA1 type alias for plumbing.Hash in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Make Signature type alias for object.Signature in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Fix GetCommitsInfo for repository with only one commit. Signed-off-by: Filip Navara <navara@emclient.com> * Fix PGP signature verification. Signed-off-by: Filip Navara <navara@emclient.com> * Fix issues with walking commit graph across merges. Signed-off-by: Filip Navara <navara@emclient.com> * Fix typo in condition. Signed-off-by: Filip Navara <navara@emclient.com> * Speed up loading branch list by keeping the repository reference (and thus all the loaded packfile indexes). Signed-off-by: Filip Navara <navara@emclient.com> * Fix lising submodules. Signed-off-by: Filip Navara <navara@emclient.com> * Fix build Signed-off-by: Filip Navara <navara@emclient.com> * Add back commit cache because of name-rev Signed-off-by: Filip Navara <navara@emclient.com> * Fix tests Signed-off-by: Filip Navara <navara@emclient.com> * Fix code style * Fix spelling * Address PR feedback Signed-off-by: Filip Navara <navara@emclient.com> * Update vendor module list Signed-off-by: Filip Navara <navara@emclient.com> * Fix getting trees by commit id Signed-off-by: Filip Navara <navara@emclient.com> * Fix remaining unit test failures * Fix GetTreeBySHA * Avoid running `git name-rev` if not necessary Signed-off-by: Filip Navara <navara@emclient.com> * Move Branch code to git module * Clean up GPG signature verification and fix it for tagged commits * Address PR feedback (import formatting, copyright headers) * Make blob lookup by SHA working * Update tests to use public API * Allow getting content from any type of object through the blob interface * Change test to actually expect the object content that is in the GIT repository * Change one more test to actually expect the object content that is in the GIT repository * Add comments
6 years ago
9 years ago
9 years ago
Improve listing performance by using go-git (#6478) * Use go-git for tree reading and commit info lookup. Signed-off-by: Filip Navara <navara@emclient.com> * Use TreeEntry.IsRegular() instead of ObjectType that was removed. Signed-off-by: Filip Navara <navara@emclient.com> * Use the treePath to optimize commit info search. Signed-off-by: Filip Navara <navara@emclient.com> * Extract the latest commit at treePath along with the other commits. Signed-off-by: Filip Navara <navara@emclient.com> * Fix listing commit info for a directory that was created in one commit and never modified after. Signed-off-by: Filip Navara <navara@emclient.com> * Avoid nearly all external 'git' invocations when doing directory listing (.editorconfig code path is still hit). Signed-off-by: Filip Navara <navara@emclient.com> * Use go-git for reading blobs. Signed-off-by: Filip Navara <navara@emclient.com> * Make SHA1 type alias for plumbing.Hash in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Make Signature type alias for object.Signature in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Fix GetCommitsInfo for repository with only one commit. Signed-off-by: Filip Navara <navara@emclient.com> * Fix PGP signature verification. Signed-off-by: Filip Navara <navara@emclient.com> * Fix issues with walking commit graph across merges. Signed-off-by: Filip Navara <navara@emclient.com> * Fix typo in condition. Signed-off-by: Filip Navara <navara@emclient.com> * Speed up loading branch list by keeping the repository reference (and thus all the loaded packfile indexes). Signed-off-by: Filip Navara <navara@emclient.com> * Fix lising submodules. Signed-off-by: Filip Navara <navara@emclient.com> * Fix build Signed-off-by: Filip Navara <navara@emclient.com> * Add back commit cache because of name-rev Signed-off-by: Filip Navara <navara@emclient.com> * Fix tests Signed-off-by: Filip Navara <navara@emclient.com> * Fix code style * Fix spelling * Address PR feedback Signed-off-by: Filip Navara <navara@emclient.com> * Update vendor module list Signed-off-by: Filip Navara <navara@emclient.com> * Fix getting trees by commit id Signed-off-by: Filip Navara <navara@emclient.com> * Fix remaining unit test failures * Fix GetTreeBySHA * Avoid running `git name-rev` if not necessary Signed-off-by: Filip Navara <navara@emclient.com> * Move Branch code to git module * Clean up GPG signature verification and fix it for tagged commits * Address PR feedback (import formatting, copyright headers) * Make blob lookup by SHA working * Update tests to use public API * Allow getting content from any type of object through the blob interface * Change test to actually expect the object content that is in the GIT repository * Change one more test to actually expect the object content that is in the GIT repository * Add comments
6 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/convert"
  11. "code.gitea.io/gitea/modules/git"
  12. repo_module "code.gitea.io/gitea/modules/repository"
  13. api "code.gitea.io/gitea/modules/structs"
  14. )
  15. // GetBranch get a branch of a repository
  16. func GetBranch(ctx *context.APIContext) {
  17. // swagger:operation GET /repos/{owner}/{repo}/branches/{branch} repository repoGetBranch
  18. // ---
  19. // summary: Retrieve a specific branch from a repository, including its effective branch protection
  20. // produces:
  21. // - application/json
  22. // parameters:
  23. // - name: owner
  24. // in: path
  25. // description: owner of the repo
  26. // type: string
  27. // required: true
  28. // - name: repo
  29. // in: path
  30. // description: name of the repo
  31. // type: string
  32. // required: true
  33. // - name: branch
  34. // in: path
  35. // description: branch to get
  36. // type: string
  37. // required: true
  38. // responses:
  39. // "200":
  40. // "$ref": "#/responses/Branch"
  41. if ctx.Repo.TreePath != "" {
  42. // if TreePath != "", then URL contained extra slashes
  43. // (i.e. "master/subbranch" instead of "master"), so branch does
  44. // not exist
  45. ctx.NotFound()
  46. return
  47. }
  48. branch, err := repo_module.GetBranch(ctx.Repo.Repository, ctx.Repo.BranchName)
  49. if err != nil {
  50. if git.IsErrBranchNotExist(err) {
  51. ctx.NotFound(err)
  52. } else {
  53. ctx.Error(http.StatusInternalServerError, "GetBranch", err)
  54. }
  55. return
  56. }
  57. c, err := branch.GetCommit()
  58. if err != nil {
  59. ctx.Error(http.StatusInternalServerError, "GetCommit", err)
  60. return
  61. }
  62. branchProtection, err := ctx.Repo.Repository.GetBranchProtection(ctx.Repo.BranchName)
  63. if err != nil {
  64. ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
  65. return
  66. }
  67. br, err := convert.ToBranch(ctx.Repo.Repository, branch, c, branchProtection, ctx.User, ctx.Repo.IsAdmin())
  68. if err != nil {
  69. ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
  70. return
  71. }
  72. ctx.JSON(http.StatusOK, br)
  73. }
  74. // ListBranches list all the branches of a repository
  75. func ListBranches(ctx *context.APIContext) {
  76. // swagger:operation GET /repos/{owner}/{repo}/branches repository repoListBranches
  77. // ---
  78. // summary: List a repository's branches
  79. // produces:
  80. // - application/json
  81. // parameters:
  82. // - name: owner
  83. // in: path
  84. // description: owner of the repo
  85. // type: string
  86. // required: true
  87. // - name: repo
  88. // in: path
  89. // description: name of the repo
  90. // type: string
  91. // required: true
  92. // responses:
  93. // "200":
  94. // "$ref": "#/responses/BranchList"
  95. branches, err := repo_module.GetBranches(ctx.Repo.Repository)
  96. if err != nil {
  97. ctx.Error(http.StatusInternalServerError, "GetBranches", err)
  98. return
  99. }
  100. apiBranches := make([]*api.Branch, len(branches))
  101. for i := range branches {
  102. c, err := branches[i].GetCommit()
  103. if err != nil {
  104. ctx.Error(http.StatusInternalServerError, "GetCommit", err)
  105. return
  106. }
  107. branchProtection, err := ctx.Repo.Repository.GetBranchProtection(branches[i].Name)
  108. if err != nil {
  109. ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
  110. return
  111. }
  112. apiBranches[i], err = convert.ToBranch(ctx.Repo.Repository, branches[i], c, branchProtection, ctx.User, ctx.Repo.IsAdmin())
  113. if err != nil {
  114. ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
  115. return
  116. }
  117. }
  118. ctx.JSON(http.StatusOK, &apiBranches)
  119. }
  120. // GetBranchProtection gets a branch protection
  121. func GetBranchProtection(ctx *context.APIContext) {
  122. // swagger:operation GET /repos/{owner}/{repo}/branch_protections/{name} repository repoGetBranchProtection
  123. // ---
  124. // summary: Get a specific branch protection for the repository
  125. // produces:
  126. // - application/json
  127. // parameters:
  128. // - name: owner
  129. // in: path
  130. // description: owner of the repo
  131. // type: string
  132. // required: true
  133. // - name: repo
  134. // in: path
  135. // description: name of the repo
  136. // type: string
  137. // required: true
  138. // - name: name
  139. // in: path
  140. // description: name of protected branch
  141. // type: string
  142. // required: true
  143. // responses:
  144. // "200":
  145. // "$ref": "#/responses/BranchProtection"
  146. // "404":
  147. // "$ref": "#/responses/notFound"
  148. repo := ctx.Repo.Repository
  149. bpName := ctx.Params(":name")
  150. bp, err := models.GetProtectedBranchBy(repo.ID, bpName)
  151. if err != nil {
  152. ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
  153. return
  154. }
  155. if bp == nil || bp.RepoID != repo.ID {
  156. ctx.NotFound()
  157. return
  158. }
  159. ctx.JSON(http.StatusOK, convert.ToBranchProtection(bp))
  160. }
  161. // ListBranchProtections list branch protections for a repo
  162. func ListBranchProtections(ctx *context.APIContext) {
  163. // swagger:operation GET /repos/{owner}/{repo}/branch_protections repository repoListBranchProtection
  164. // ---
  165. // summary: List branch protections for a repository
  166. // produces:
  167. // - application/json
  168. // parameters:
  169. // - name: owner
  170. // in: path
  171. // description: owner of the repo
  172. // type: string
  173. // required: true
  174. // - name: repo
  175. // in: path
  176. // description: name of the repo
  177. // type: string
  178. // required: true
  179. // responses:
  180. // "200":
  181. // "$ref": "#/responses/BranchProtectionList"
  182. repo := ctx.Repo.Repository
  183. bps, err := repo.GetProtectedBranches()
  184. if err != nil {
  185. ctx.Error(http.StatusInternalServerError, "GetProtectedBranches", err)
  186. return
  187. }
  188. apiBps := make([]*api.BranchProtection, len(bps))
  189. for i := range bps {
  190. apiBps[i] = convert.ToBranchProtection(bps[i])
  191. }
  192. ctx.JSON(http.StatusOK, apiBps)
  193. }
  194. // CreateBranchProtection creates a branch protection for a repo
  195. func CreateBranchProtection(ctx *context.APIContext, form api.CreateBranchProtectionOption) {
  196. // swagger:operation POST /repos/{owner}/{repo}/branch_protections repository repoCreateBranchProtection
  197. // ---
  198. // summary: Create a branch protections for a repository
  199. // consumes:
  200. // - application/json
  201. // produces:
  202. // - application/json
  203. // parameters:
  204. // - name: owner
  205. // in: path
  206. // description: owner of the repo
  207. // type: string
  208. // required: true
  209. // - name: repo
  210. // in: path
  211. // description: name of the repo
  212. // type: string
  213. // required: true
  214. // - name: body
  215. // in: body
  216. // schema:
  217. // "$ref": "#/definitions/CreateBranchProtectionOption"
  218. // responses:
  219. // "201":
  220. // "$ref": "#/responses/BranchProtection"
  221. // "403":
  222. // "$ref": "#/responses/forbidden"
  223. // "404":
  224. // "$ref": "#/responses/notFound"
  225. // "422":
  226. // "$ref": "#/responses/validationError"
  227. repo := ctx.Repo.Repository
  228. // Currently protection must match an actual branch
  229. if !git.IsBranchExist(ctx.Repo.Repository.RepoPath(), form.BranchName) {
  230. ctx.NotFound()
  231. return
  232. }
  233. protectBranch, err := models.GetProtectedBranchBy(repo.ID, form.BranchName)
  234. if err != nil {
  235. ctx.Error(http.StatusInternalServerError, "GetProtectBranchOfRepoByName", err)
  236. return
  237. } else if protectBranch != nil {
  238. ctx.Error(http.StatusForbidden, "Create branch protection", "Branch protection already exist")
  239. return
  240. }
  241. var requiredApprovals int64
  242. if form.RequiredApprovals > 0 {
  243. requiredApprovals = form.RequiredApprovals
  244. }
  245. whitelistUsers, err := models.GetUserIDsByNames(form.PushWhitelistUsernames, false)
  246. if err != nil {
  247. if models.IsErrUserNotExist(err) {
  248. ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
  249. return
  250. }
  251. ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
  252. return
  253. }
  254. mergeWhitelistUsers, err := models.GetUserIDsByNames(form.MergeWhitelistUsernames, false)
  255. if err != nil {
  256. if models.IsErrUserNotExist(err) {
  257. ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
  258. return
  259. }
  260. ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
  261. return
  262. }
  263. approvalsWhitelistUsers, err := models.GetUserIDsByNames(form.ApprovalsWhitelistUsernames, false)
  264. if err != nil {
  265. if models.IsErrUserNotExist(err) {
  266. ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
  267. return
  268. }
  269. ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
  270. return
  271. }
  272. var whitelistTeams, mergeWhitelistTeams, approvalsWhitelistTeams []int64
  273. if repo.Owner.IsOrganization() {
  274. whitelistTeams, err = models.GetTeamIDsByNames(repo.OwnerID, form.PushWhitelistTeams, false)
  275. if err != nil {
  276. if models.IsErrTeamNotExist(err) {
  277. ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
  278. return
  279. }
  280. ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
  281. return
  282. }
  283. mergeWhitelistTeams, err = models.GetTeamIDsByNames(repo.OwnerID, form.MergeWhitelistTeams, false)
  284. if err != nil {
  285. if models.IsErrTeamNotExist(err) {
  286. ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
  287. return
  288. }
  289. ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
  290. return
  291. }
  292. approvalsWhitelistTeams, err = models.GetTeamIDsByNames(repo.OwnerID, form.ApprovalsWhitelistTeams, false)
  293. if err != nil {
  294. if models.IsErrTeamNotExist(err) {
  295. ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
  296. return
  297. }
  298. ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
  299. return
  300. }
  301. }
  302. protectBranch = &models.ProtectedBranch{
  303. RepoID: ctx.Repo.Repository.ID,
  304. BranchName: form.BranchName,
  305. CanPush: form.EnablePush,
  306. EnableWhitelist: form.EnablePush && form.EnablePushWhitelist,
  307. EnableMergeWhitelist: form.EnableMergeWhitelist,
  308. WhitelistDeployKeys: form.EnablePush && form.EnablePushWhitelist && form.PushWhitelistDeployKeys,
  309. EnableStatusCheck: form.EnableStatusCheck,
  310. StatusCheckContexts: form.StatusCheckContexts,
  311. EnableApprovalsWhitelist: form.EnableApprovalsWhitelist,
  312. RequiredApprovals: requiredApprovals,
  313. BlockOnRejectedReviews: form.BlockOnRejectedReviews,
  314. DismissStaleApprovals: form.DismissStaleApprovals,
  315. RequireSignedCommits: form.RequireSignedCommits,
  316. ProtectedFilePatterns: form.ProtectedFilePatterns,
  317. }
  318. err = models.UpdateProtectBranch(ctx.Repo.Repository, protectBranch, models.WhitelistOptions{
  319. UserIDs: whitelistUsers,
  320. TeamIDs: whitelistTeams,
  321. MergeUserIDs: mergeWhitelistUsers,
  322. MergeTeamIDs: mergeWhitelistTeams,
  323. ApprovalsUserIDs: approvalsWhitelistUsers,
  324. ApprovalsTeamIDs: approvalsWhitelistTeams,
  325. })
  326. if err != nil {
  327. ctx.Error(http.StatusInternalServerError, "UpdateProtectBranch", err)
  328. return
  329. }
  330. // Reload from db to get all whitelists
  331. bp, err := models.GetProtectedBranchBy(ctx.Repo.Repository.ID, form.BranchName)
  332. if err != nil {
  333. ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
  334. return
  335. }
  336. if bp == nil || bp.RepoID != ctx.Repo.Repository.ID {
  337. ctx.Error(http.StatusInternalServerError, "New branch protection not found", err)
  338. return
  339. }
  340. ctx.JSON(http.StatusCreated, convert.ToBranchProtection(bp))
  341. }
  342. // EditBranchProtection edits a branch protection for a repo
  343. func EditBranchProtection(ctx *context.APIContext, form api.EditBranchProtectionOption) {
  344. // swagger:operation PATCH /repos/{owner}/{repo}/branch_protections/{name} repository repoEditBranchProtection
  345. // ---
  346. // summary: Edit a branch protections for a repository. Only fields that are set will be changed
  347. // consumes:
  348. // - application/json
  349. // produces:
  350. // - application/json
  351. // parameters:
  352. // - name: owner
  353. // in: path
  354. // description: owner of the repo
  355. // type: string
  356. // required: true
  357. // - name: repo
  358. // in: path
  359. // description: name of the repo
  360. // type: string
  361. // required: true
  362. // - name: name
  363. // in: path
  364. // description: name of protected branch
  365. // type: string
  366. // required: true
  367. // - name: body
  368. // in: body
  369. // schema:
  370. // "$ref": "#/definitions/EditBranchProtectionOption"
  371. // responses:
  372. // "200":
  373. // "$ref": "#/responses/BranchProtection"
  374. // "404":
  375. // "$ref": "#/responses/notFound"
  376. // "422":
  377. // "$ref": "#/responses/validationError"
  378. repo := ctx.Repo.Repository
  379. bpName := ctx.Params(":name")
  380. protectBranch, err := models.GetProtectedBranchBy(repo.ID, bpName)
  381. if err != nil {
  382. ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
  383. return
  384. }
  385. if protectBranch == nil || protectBranch.RepoID != repo.ID {
  386. ctx.NotFound()
  387. return
  388. }
  389. if form.EnablePush != nil {
  390. if !*form.EnablePush {
  391. protectBranch.CanPush = false
  392. protectBranch.EnableWhitelist = false
  393. protectBranch.WhitelistDeployKeys = false
  394. } else {
  395. protectBranch.CanPush = true
  396. if form.EnablePushWhitelist != nil {
  397. if !*form.EnablePushWhitelist {
  398. protectBranch.EnableWhitelist = false
  399. protectBranch.WhitelistDeployKeys = false
  400. } else {
  401. protectBranch.EnableWhitelist = true
  402. if form.PushWhitelistDeployKeys != nil {
  403. protectBranch.WhitelistDeployKeys = *form.PushWhitelistDeployKeys
  404. }
  405. }
  406. }
  407. }
  408. }
  409. if form.EnableMergeWhitelist != nil {
  410. protectBranch.EnableMergeWhitelist = *form.EnableMergeWhitelist
  411. }
  412. if form.EnableStatusCheck != nil {
  413. protectBranch.EnableStatusCheck = *form.EnableStatusCheck
  414. }
  415. if protectBranch.EnableStatusCheck {
  416. protectBranch.StatusCheckContexts = form.StatusCheckContexts
  417. }
  418. if form.RequiredApprovals != nil && *form.RequiredApprovals >= 0 {
  419. protectBranch.RequiredApprovals = *form.RequiredApprovals
  420. }
  421. if form.EnableApprovalsWhitelist != nil {
  422. protectBranch.EnableApprovalsWhitelist = *form.EnableApprovalsWhitelist
  423. }
  424. if form.BlockOnRejectedReviews != nil {
  425. protectBranch.BlockOnRejectedReviews = *form.BlockOnRejectedReviews
  426. }
  427. if form.DismissStaleApprovals != nil {
  428. protectBranch.DismissStaleApprovals = *form.DismissStaleApprovals
  429. }
  430. if form.RequireSignedCommits != nil {
  431. protectBranch.RequireSignedCommits = *form.RequireSignedCommits
  432. }
  433. if form.ProtectedFilePatterns != nil {
  434. protectBranch.ProtectedFilePatterns = *form.ProtectedFilePatterns
  435. }
  436. var whitelistUsers []int64
  437. if form.PushWhitelistUsernames != nil {
  438. whitelistUsers, err = models.GetUserIDsByNames(form.PushWhitelistUsernames, false)
  439. if err != nil {
  440. if models.IsErrUserNotExist(err) {
  441. ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
  442. return
  443. }
  444. ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
  445. return
  446. }
  447. } else {
  448. whitelistUsers = protectBranch.WhitelistUserIDs
  449. }
  450. var mergeWhitelistUsers []int64
  451. if form.MergeWhitelistUsernames != nil {
  452. mergeWhitelistUsers, err = models.GetUserIDsByNames(form.MergeWhitelistUsernames, false)
  453. if err != nil {
  454. if models.IsErrUserNotExist(err) {
  455. ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
  456. return
  457. }
  458. ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
  459. return
  460. }
  461. } else {
  462. mergeWhitelistUsers = protectBranch.MergeWhitelistUserIDs
  463. }
  464. var approvalsWhitelistUsers []int64
  465. if form.ApprovalsWhitelistUsernames != nil {
  466. approvalsWhitelistUsers, err = models.GetUserIDsByNames(form.ApprovalsWhitelistUsernames, false)
  467. if err != nil {
  468. if models.IsErrUserNotExist(err) {
  469. ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
  470. return
  471. }
  472. ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
  473. return
  474. }
  475. } else {
  476. approvalsWhitelistUsers = protectBranch.ApprovalsWhitelistUserIDs
  477. }
  478. var whitelistTeams, mergeWhitelistTeams, approvalsWhitelistTeams []int64
  479. if repo.Owner.IsOrganization() {
  480. if form.PushWhitelistTeams != nil {
  481. whitelistTeams, err = models.GetTeamIDsByNames(repo.OwnerID, form.PushWhitelistTeams, false)
  482. if err != nil {
  483. if models.IsErrTeamNotExist(err) {
  484. ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
  485. return
  486. }
  487. ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
  488. return
  489. }
  490. } else {
  491. whitelistTeams = protectBranch.WhitelistTeamIDs
  492. }
  493. if form.MergeWhitelistTeams != nil {
  494. mergeWhitelistTeams, err = models.GetTeamIDsByNames(repo.OwnerID, form.MergeWhitelistTeams, false)
  495. if err != nil {
  496. if models.IsErrTeamNotExist(err) {
  497. ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
  498. return
  499. }
  500. ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
  501. return
  502. }
  503. } else {
  504. mergeWhitelistTeams = protectBranch.MergeWhitelistTeamIDs
  505. }
  506. if form.ApprovalsWhitelistTeams != nil {
  507. approvalsWhitelistTeams, err = models.GetTeamIDsByNames(repo.OwnerID, form.ApprovalsWhitelistTeams, false)
  508. if err != nil {
  509. if models.IsErrTeamNotExist(err) {
  510. ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
  511. return
  512. }
  513. ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
  514. return
  515. }
  516. } else {
  517. approvalsWhitelistTeams = protectBranch.ApprovalsWhitelistTeamIDs
  518. }
  519. }
  520. err = models.UpdateProtectBranch(ctx.Repo.Repository, protectBranch, models.WhitelistOptions{
  521. UserIDs: whitelistUsers,
  522. TeamIDs: whitelistTeams,
  523. MergeUserIDs: mergeWhitelistUsers,
  524. MergeTeamIDs: mergeWhitelistTeams,
  525. ApprovalsUserIDs: approvalsWhitelistUsers,
  526. ApprovalsTeamIDs: approvalsWhitelistTeams,
  527. })
  528. if err != nil {
  529. ctx.Error(http.StatusInternalServerError, "UpdateProtectBranch", err)
  530. return
  531. }
  532. // Reload from db to ensure get all whitelists
  533. bp, err := models.GetProtectedBranchBy(repo.ID, bpName)
  534. if err != nil {
  535. ctx.Error(http.StatusInternalServerError, "GetProtectedBranchBy", err)
  536. return
  537. }
  538. if bp == nil || bp.RepoID != ctx.Repo.Repository.ID {
  539. ctx.Error(http.StatusInternalServerError, "New branch protection not found", err)
  540. return
  541. }
  542. ctx.JSON(http.StatusOK, convert.ToBranchProtection(bp))
  543. }
  544. // DeleteBranchProtection deletes a branch protection for a repo
  545. func DeleteBranchProtection(ctx *context.APIContext) {
  546. // swagger:operation DELETE /repos/{owner}/{repo}/branch_protections/{name} repository repoDeleteBranchProtection
  547. // ---
  548. // summary: Delete a specific branch protection for the repository
  549. // produces:
  550. // - application/json
  551. // parameters:
  552. // - name: owner
  553. // in: path
  554. // description: owner of the repo
  555. // type: string
  556. // required: true
  557. // - name: repo
  558. // in: path
  559. // description: name of the repo
  560. // type: string
  561. // required: true
  562. // - name: name
  563. // in: path
  564. // description: name of protected branch
  565. // type: string
  566. // required: true
  567. // responses:
  568. // "204":
  569. // "$ref": "#/responses/empty"
  570. // "404":
  571. // "$ref": "#/responses/notFound"
  572. repo := ctx.Repo.Repository
  573. bpName := ctx.Params(":name")
  574. bp, err := models.GetProtectedBranchBy(repo.ID, bpName)
  575. if err != nil {
  576. ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
  577. return
  578. }
  579. if bp == nil || bp.RepoID != repo.ID {
  580. ctx.NotFound()
  581. return
  582. }
  583. if err := ctx.Repo.Repository.DeleteProtectedBranch(bp.ID); err != nil {
  584. ctx.Error(http.StatusInternalServerError, "DeleteProtectedBranch", err)
  585. return
  586. }
  587. ctx.Status(http.StatusNoContent)
  588. }