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 30KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package repo
  5. import (
  6. "errors"
  7. "fmt"
  8. "net/http"
  9. "code.gitea.io/gitea/models"
  10. git_model "code.gitea.io/gitea/models/git"
  11. "code.gitea.io/gitea/models/organization"
  12. user_model "code.gitea.io/gitea/models/user"
  13. "code.gitea.io/gitea/modules/context"
  14. "code.gitea.io/gitea/modules/git"
  15. repo_module "code.gitea.io/gitea/modules/repository"
  16. api "code.gitea.io/gitea/modules/structs"
  17. "code.gitea.io/gitea/modules/util"
  18. "code.gitea.io/gitea/modules/web"
  19. "code.gitea.io/gitea/routers/api/v1/utils"
  20. "code.gitea.io/gitea/services/convert"
  21. pull_service "code.gitea.io/gitea/services/pull"
  22. repo_service "code.gitea.io/gitea/services/repository"
  23. )
  24. // GetBranch get a branch of a repository
  25. func GetBranch(ctx *context.APIContext) {
  26. // swagger:operation GET /repos/{owner}/{repo}/branches/{branch} repository repoGetBranch
  27. // ---
  28. // summary: Retrieve a specific branch from a repository, including its effective branch protection
  29. // produces:
  30. // - application/json
  31. // parameters:
  32. // - name: owner
  33. // in: path
  34. // description: owner of the repo
  35. // type: string
  36. // required: true
  37. // - name: repo
  38. // in: path
  39. // description: name of the repo
  40. // type: string
  41. // required: true
  42. // - name: branch
  43. // in: path
  44. // description: branch to get
  45. // type: string
  46. // required: true
  47. // responses:
  48. // "200":
  49. // "$ref": "#/responses/Branch"
  50. // "404":
  51. // "$ref": "#/responses/notFound"
  52. branchName := ctx.Params("*")
  53. branch, err := ctx.Repo.GitRepo.GetBranch(branchName)
  54. if err != nil {
  55. if git.IsErrBranchNotExist(err) {
  56. ctx.NotFound(err)
  57. } else {
  58. ctx.Error(http.StatusInternalServerError, "GetBranch", err)
  59. }
  60. return
  61. }
  62. c, err := branch.GetCommit()
  63. if err != nil {
  64. ctx.Error(http.StatusInternalServerError, "GetCommit", err)
  65. return
  66. }
  67. branchProtection, err := git_model.GetFirstMatchProtectedBranchRule(ctx, ctx.Repo.Repository.ID, branchName)
  68. if err != nil {
  69. ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
  70. return
  71. }
  72. br, err := convert.ToBranch(ctx, ctx.Repo.Repository, branch.Name, c, branchProtection, ctx.Doer, ctx.Repo.IsAdmin())
  73. if err != nil {
  74. ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
  75. return
  76. }
  77. ctx.JSON(http.StatusOK, br)
  78. }
  79. // DeleteBranch get a branch of a repository
  80. func DeleteBranch(ctx *context.APIContext) {
  81. // swagger:operation DELETE /repos/{owner}/{repo}/branches/{branch} repository repoDeleteBranch
  82. // ---
  83. // summary: Delete a specific branch from a repository
  84. // produces:
  85. // - application/json
  86. // parameters:
  87. // - name: owner
  88. // in: path
  89. // description: owner of the repo
  90. // type: string
  91. // required: true
  92. // - name: repo
  93. // in: path
  94. // description: name of the repo
  95. // type: string
  96. // required: true
  97. // - name: branch
  98. // in: path
  99. // description: branch to delete
  100. // type: string
  101. // required: true
  102. // responses:
  103. // "204":
  104. // "$ref": "#/responses/empty"
  105. // "403":
  106. // "$ref": "#/responses/error"
  107. // "404":
  108. // "$ref": "#/responses/notFound"
  109. if ctx.Repo.Repository.IsEmpty {
  110. ctx.Error(http.StatusNotFound, "", "Git Repository is empty.")
  111. return
  112. }
  113. if ctx.Repo.Repository.IsArchived {
  114. ctx.Error(http.StatusForbidden, "", "Git Repository is archived.")
  115. return
  116. }
  117. if ctx.Repo.Repository.IsMirror {
  118. ctx.Error(http.StatusForbidden, "", "Git Repository is a mirror.")
  119. return
  120. }
  121. branchName := ctx.Params("*")
  122. if ctx.Repo.Repository.IsEmpty {
  123. ctx.Error(http.StatusForbidden, "", "Git Repository is empty.")
  124. return
  125. }
  126. // check whether branches of this repository has been synced
  127. totalNumOfBranches, err := git_model.CountBranches(ctx, git_model.FindBranchOptions{
  128. RepoID: ctx.Repo.Repository.ID,
  129. IsDeletedBranch: util.OptionalBoolFalse,
  130. })
  131. if err != nil {
  132. ctx.Error(http.StatusInternalServerError, "CountBranches", err)
  133. return
  134. }
  135. if totalNumOfBranches == 0 { // sync branches immediately because non-empty repository should have at least 1 branch
  136. _, err = repo_module.SyncRepoBranches(ctx, ctx.Repo.Repository.ID, 0)
  137. if err != nil {
  138. ctx.ServerError("SyncRepoBranches", err)
  139. return
  140. }
  141. }
  142. if ctx.Repo.Repository.IsArchived {
  143. ctx.Error(http.StatusForbidden, "IsArchived", fmt.Errorf("can not delete branch of an archived repository"))
  144. return
  145. }
  146. if ctx.Repo.Repository.IsMirror {
  147. ctx.Error(http.StatusForbidden, "IsMirrored", fmt.Errorf("can not delete branch of an mirror repository"))
  148. return
  149. }
  150. if err := repo_service.DeleteBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName); err != nil {
  151. switch {
  152. case git.IsErrBranchNotExist(err):
  153. ctx.NotFound(err)
  154. case errors.Is(err, repo_service.ErrBranchIsDefault):
  155. ctx.Error(http.StatusForbidden, "DefaultBranch", fmt.Errorf("can not delete default branch"))
  156. case errors.Is(err, git_model.ErrBranchIsProtected):
  157. ctx.Error(http.StatusForbidden, "IsProtectedBranch", fmt.Errorf("branch protected"))
  158. default:
  159. ctx.Error(http.StatusInternalServerError, "DeleteBranch", err)
  160. }
  161. return
  162. }
  163. ctx.Status(http.StatusNoContent)
  164. }
  165. // CreateBranch creates a branch for a user's repository
  166. func CreateBranch(ctx *context.APIContext) {
  167. // swagger:operation POST /repos/{owner}/{repo}/branches repository repoCreateBranch
  168. // ---
  169. // summary: Create a branch
  170. // consumes:
  171. // - application/json
  172. // produces:
  173. // - application/json
  174. // parameters:
  175. // - name: owner
  176. // in: path
  177. // description: owner of the repo
  178. // type: string
  179. // required: true
  180. // - name: repo
  181. // in: path
  182. // description: name of the repo
  183. // type: string
  184. // required: true
  185. // - name: body
  186. // in: body
  187. // schema:
  188. // "$ref": "#/definitions/CreateBranchRepoOption"
  189. // responses:
  190. // "201":
  191. // "$ref": "#/responses/Branch"
  192. // "403":
  193. // description: The branch is archived or a mirror.
  194. // "404":
  195. // description: The old branch does not exist.
  196. // "409":
  197. // description: The branch with the same name already exists.
  198. if ctx.Repo.Repository.IsEmpty {
  199. ctx.Error(http.StatusNotFound, "", "Git Repository is empty.")
  200. return
  201. }
  202. if ctx.Repo.Repository.IsArchived {
  203. ctx.Error(http.StatusForbidden, "", "Git Repository is archived.")
  204. return
  205. }
  206. if ctx.Repo.Repository.IsMirror {
  207. ctx.Error(http.StatusForbidden, "", "Git Repository is a mirror.")
  208. return
  209. }
  210. opt := web.GetForm(ctx).(*api.CreateBranchRepoOption)
  211. var oldCommit *git.Commit
  212. var err error
  213. if len(opt.OldRefName) > 0 {
  214. oldCommit, err = ctx.Repo.GitRepo.GetCommit(opt.OldRefName)
  215. if err != nil {
  216. ctx.Error(http.StatusInternalServerError, "GetCommit", err)
  217. return
  218. }
  219. } else if len(opt.OldBranchName) > 0 { //nolint
  220. if ctx.Repo.GitRepo.IsBranchExist(opt.OldBranchName) { //nolint
  221. oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(opt.OldBranchName) //nolint
  222. if err != nil {
  223. ctx.Error(http.StatusInternalServerError, "GetBranchCommit", err)
  224. return
  225. }
  226. } else {
  227. ctx.Error(http.StatusNotFound, "", "The old branch does not exist")
  228. return
  229. }
  230. } else {
  231. oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
  232. if err != nil {
  233. ctx.Error(http.StatusInternalServerError, "GetBranchCommit", err)
  234. return
  235. }
  236. }
  237. err = repo_service.CreateNewBranchFromCommit(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, oldCommit.ID.String(), opt.BranchName)
  238. if err != nil {
  239. if git_model.IsErrBranchNotExist(err) {
  240. ctx.Error(http.StatusNotFound, "", "The old branch does not exist")
  241. } else if models.IsErrTagAlreadyExists(err) {
  242. ctx.Error(http.StatusConflict, "", "The branch with the same tag already exists.")
  243. } else if git_model.IsErrBranchAlreadyExists(err) || git.IsErrPushOutOfDate(err) {
  244. ctx.Error(http.StatusConflict, "", "The branch already exists.")
  245. } else if git_model.IsErrBranchNameConflict(err) {
  246. ctx.Error(http.StatusConflict, "", "The branch with the same name already exists.")
  247. } else {
  248. ctx.Error(http.StatusInternalServerError, "CreateNewBranchFromCommit", err)
  249. }
  250. return
  251. }
  252. branch, err := ctx.Repo.GitRepo.GetBranch(opt.BranchName)
  253. if err != nil {
  254. ctx.Error(http.StatusInternalServerError, "GetBranch", err)
  255. return
  256. }
  257. commit, err := branch.GetCommit()
  258. if err != nil {
  259. ctx.Error(http.StatusInternalServerError, "GetCommit", err)
  260. return
  261. }
  262. branchProtection, err := git_model.GetFirstMatchProtectedBranchRule(ctx, ctx.Repo.Repository.ID, branch.Name)
  263. if err != nil {
  264. ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
  265. return
  266. }
  267. br, err := convert.ToBranch(ctx, ctx.Repo.Repository, branch.Name, commit, branchProtection, ctx.Doer, ctx.Repo.IsAdmin())
  268. if err != nil {
  269. ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
  270. return
  271. }
  272. ctx.JSON(http.StatusCreated, br)
  273. }
  274. // ListBranches list all the branches of a repository
  275. func ListBranches(ctx *context.APIContext) {
  276. // swagger:operation GET /repos/{owner}/{repo}/branches repository repoListBranches
  277. // ---
  278. // summary: List a repository's branches
  279. // produces:
  280. // - application/json
  281. // parameters:
  282. // - name: owner
  283. // in: path
  284. // description: owner of the repo
  285. // type: string
  286. // required: true
  287. // - name: repo
  288. // in: path
  289. // description: name of the repo
  290. // type: string
  291. // required: true
  292. // - name: page
  293. // in: query
  294. // description: page number of results to return (1-based)
  295. // type: integer
  296. // - name: limit
  297. // in: query
  298. // description: page size of results
  299. // type: integer
  300. // responses:
  301. // "200":
  302. // "$ref": "#/responses/BranchList"
  303. var totalNumOfBranches int64
  304. var apiBranches []*api.Branch
  305. listOptions := utils.GetListOptions(ctx)
  306. if !ctx.Repo.Repository.IsEmpty {
  307. if ctx.Repo.GitRepo == nil {
  308. ctx.Error(http.StatusInternalServerError, "Load git repository failed", nil)
  309. return
  310. }
  311. branchOpts := git_model.FindBranchOptions{
  312. ListOptions: listOptions,
  313. RepoID: ctx.Repo.Repository.ID,
  314. IsDeletedBranch: util.OptionalBoolFalse,
  315. }
  316. var err error
  317. totalNumOfBranches, err = git_model.CountBranches(ctx, branchOpts)
  318. if err != nil {
  319. ctx.Error(http.StatusInternalServerError, "CountBranches", err)
  320. return
  321. }
  322. if totalNumOfBranches == 0 { // sync branches immediately because non-empty repository should have at least 1 branch
  323. totalNumOfBranches, err = repo_module.SyncRepoBranches(ctx, ctx.Repo.Repository.ID, 0)
  324. if err != nil {
  325. ctx.ServerError("SyncRepoBranches", err)
  326. return
  327. }
  328. }
  329. rules, err := git_model.FindRepoProtectedBranchRules(ctx, ctx.Repo.Repository.ID)
  330. if err != nil {
  331. ctx.Error(http.StatusInternalServerError, "FindMatchedProtectedBranchRules", err)
  332. return
  333. }
  334. branches, err := git_model.FindBranches(ctx, branchOpts)
  335. if err != nil {
  336. ctx.Error(http.StatusInternalServerError, "GetBranches", err)
  337. return
  338. }
  339. apiBranches = make([]*api.Branch, 0, len(branches))
  340. for i := range branches {
  341. c, err := ctx.Repo.GitRepo.GetBranchCommit(branches[i].Name)
  342. if err != nil {
  343. // Skip if this branch doesn't exist anymore.
  344. if git.IsErrNotExist(err) {
  345. totalNumOfBranches--
  346. continue
  347. }
  348. ctx.Error(http.StatusInternalServerError, "GetCommit", err)
  349. return
  350. }
  351. branchProtection := rules.GetFirstMatched(branches[i].Name)
  352. apiBranch, err := convert.ToBranch(ctx, ctx.Repo.Repository, branches[i].Name, c, branchProtection, ctx.Doer, ctx.Repo.IsAdmin())
  353. if err != nil {
  354. ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
  355. return
  356. }
  357. apiBranches = append(apiBranches, apiBranch)
  358. }
  359. }
  360. ctx.SetLinkHeader(int(totalNumOfBranches), listOptions.PageSize)
  361. ctx.SetTotalCountHeader(totalNumOfBranches)
  362. ctx.JSON(http.StatusOK, apiBranches)
  363. }
  364. // GetBranchProtection gets a branch protection
  365. func GetBranchProtection(ctx *context.APIContext) {
  366. // swagger:operation GET /repos/{owner}/{repo}/branch_protections/{name} repository repoGetBranchProtection
  367. // ---
  368. // summary: Get a specific branch protection for the repository
  369. // produces:
  370. // - application/json
  371. // parameters:
  372. // - name: owner
  373. // in: path
  374. // description: owner of the repo
  375. // type: string
  376. // required: true
  377. // - name: repo
  378. // in: path
  379. // description: name of the repo
  380. // type: string
  381. // required: true
  382. // - name: name
  383. // in: path
  384. // description: name of protected branch
  385. // type: string
  386. // required: true
  387. // responses:
  388. // "200":
  389. // "$ref": "#/responses/BranchProtection"
  390. // "404":
  391. // "$ref": "#/responses/notFound"
  392. repo := ctx.Repo.Repository
  393. bpName := ctx.Params(":name")
  394. bp, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, bpName)
  395. if err != nil {
  396. ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
  397. return
  398. }
  399. if bp == nil || bp.RepoID != repo.ID {
  400. ctx.NotFound()
  401. return
  402. }
  403. ctx.JSON(http.StatusOK, convert.ToBranchProtection(ctx, bp))
  404. }
  405. // ListBranchProtections list branch protections for a repo
  406. func ListBranchProtections(ctx *context.APIContext) {
  407. // swagger:operation GET /repos/{owner}/{repo}/branch_protections repository repoListBranchProtection
  408. // ---
  409. // summary: List branch protections for a repository
  410. // produces:
  411. // - application/json
  412. // parameters:
  413. // - name: owner
  414. // in: path
  415. // description: owner of the repo
  416. // type: string
  417. // required: true
  418. // - name: repo
  419. // in: path
  420. // description: name of the repo
  421. // type: string
  422. // required: true
  423. // responses:
  424. // "200":
  425. // "$ref": "#/responses/BranchProtectionList"
  426. repo := ctx.Repo.Repository
  427. bps, err := git_model.FindRepoProtectedBranchRules(ctx, repo.ID)
  428. if err != nil {
  429. ctx.Error(http.StatusInternalServerError, "GetProtectedBranches", err)
  430. return
  431. }
  432. apiBps := make([]*api.BranchProtection, len(bps))
  433. for i := range bps {
  434. apiBps[i] = convert.ToBranchProtection(ctx, bps[i])
  435. }
  436. ctx.JSON(http.StatusOK, apiBps)
  437. }
  438. // CreateBranchProtection creates a branch protection for a repo
  439. func CreateBranchProtection(ctx *context.APIContext) {
  440. // swagger:operation POST /repos/{owner}/{repo}/branch_protections repository repoCreateBranchProtection
  441. // ---
  442. // summary: Create a branch protections for a repository
  443. // consumes:
  444. // - application/json
  445. // produces:
  446. // - application/json
  447. // parameters:
  448. // - name: owner
  449. // in: path
  450. // description: owner of the repo
  451. // type: string
  452. // required: true
  453. // - name: repo
  454. // in: path
  455. // description: name of the repo
  456. // type: string
  457. // required: true
  458. // - name: body
  459. // in: body
  460. // schema:
  461. // "$ref": "#/definitions/CreateBranchProtectionOption"
  462. // responses:
  463. // "201":
  464. // "$ref": "#/responses/BranchProtection"
  465. // "403":
  466. // "$ref": "#/responses/forbidden"
  467. // "404":
  468. // "$ref": "#/responses/notFound"
  469. // "422":
  470. // "$ref": "#/responses/validationError"
  471. form := web.GetForm(ctx).(*api.CreateBranchProtectionOption)
  472. repo := ctx.Repo.Repository
  473. ruleName := form.RuleName
  474. if ruleName == "" {
  475. ruleName = form.BranchName //nolint
  476. }
  477. if len(ruleName) == 0 {
  478. ctx.Error(http.StatusBadRequest, "both rule_name and branch_name are empty", "both rule_name and branch_name are empty")
  479. return
  480. }
  481. isPlainRule := !git_model.IsRuleNameSpecial(ruleName)
  482. var isBranchExist bool
  483. if isPlainRule {
  484. isBranchExist = git.IsBranchExist(ctx.Req.Context(), ctx.Repo.Repository.RepoPath(), ruleName)
  485. }
  486. protectBranch, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, ruleName)
  487. if err != nil {
  488. ctx.Error(http.StatusInternalServerError, "GetProtectBranchOfRepoByName", err)
  489. return
  490. } else if protectBranch != nil {
  491. ctx.Error(http.StatusForbidden, "Create branch protection", "Branch protection already exist")
  492. return
  493. }
  494. var requiredApprovals int64
  495. if form.RequiredApprovals > 0 {
  496. requiredApprovals = form.RequiredApprovals
  497. }
  498. whitelistUsers, err := user_model.GetUserIDsByNames(ctx, form.PushWhitelistUsernames, false)
  499. if err != nil {
  500. if user_model.IsErrUserNotExist(err) {
  501. ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
  502. return
  503. }
  504. ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
  505. return
  506. }
  507. mergeWhitelistUsers, err := user_model.GetUserIDsByNames(ctx, form.MergeWhitelistUsernames, false)
  508. if err != nil {
  509. if user_model.IsErrUserNotExist(err) {
  510. ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
  511. return
  512. }
  513. ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
  514. return
  515. }
  516. approvalsWhitelistUsers, err := user_model.GetUserIDsByNames(ctx, form.ApprovalsWhitelistUsernames, false)
  517. if err != nil {
  518. if user_model.IsErrUserNotExist(err) {
  519. ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
  520. return
  521. }
  522. ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
  523. return
  524. }
  525. var whitelistTeams, mergeWhitelistTeams, approvalsWhitelistTeams []int64
  526. if repo.Owner.IsOrganization() {
  527. whitelistTeams, err = organization.GetTeamIDsByNames(repo.OwnerID, form.PushWhitelistTeams, false)
  528. if err != nil {
  529. if organization.IsErrTeamNotExist(err) {
  530. ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
  531. return
  532. }
  533. ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
  534. return
  535. }
  536. mergeWhitelistTeams, err = organization.GetTeamIDsByNames(repo.OwnerID, form.MergeWhitelistTeams, false)
  537. if err != nil {
  538. if organization.IsErrTeamNotExist(err) {
  539. ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
  540. return
  541. }
  542. ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
  543. return
  544. }
  545. approvalsWhitelistTeams, err = organization.GetTeamIDsByNames(repo.OwnerID, form.ApprovalsWhitelistTeams, false)
  546. if err != nil {
  547. if organization.IsErrTeamNotExist(err) {
  548. ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
  549. return
  550. }
  551. ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
  552. return
  553. }
  554. }
  555. protectBranch = &git_model.ProtectedBranch{
  556. RepoID: ctx.Repo.Repository.ID,
  557. RuleName: ruleName,
  558. CanPush: form.EnablePush,
  559. EnableWhitelist: form.EnablePush && form.EnablePushWhitelist,
  560. EnableMergeWhitelist: form.EnableMergeWhitelist,
  561. WhitelistDeployKeys: form.EnablePush && form.EnablePushWhitelist && form.PushWhitelistDeployKeys,
  562. EnableStatusCheck: form.EnableStatusCheck,
  563. StatusCheckContexts: form.StatusCheckContexts,
  564. EnableApprovalsWhitelist: form.EnableApprovalsWhitelist,
  565. RequiredApprovals: requiredApprovals,
  566. BlockOnRejectedReviews: form.BlockOnRejectedReviews,
  567. BlockOnOfficialReviewRequests: form.BlockOnOfficialReviewRequests,
  568. DismissStaleApprovals: form.DismissStaleApprovals,
  569. RequireSignedCommits: form.RequireSignedCommits,
  570. ProtectedFilePatterns: form.ProtectedFilePatterns,
  571. UnprotectedFilePatterns: form.UnprotectedFilePatterns,
  572. BlockOnOutdatedBranch: form.BlockOnOutdatedBranch,
  573. }
  574. err = git_model.UpdateProtectBranch(ctx, ctx.Repo.Repository, protectBranch, git_model.WhitelistOptions{
  575. UserIDs: whitelistUsers,
  576. TeamIDs: whitelistTeams,
  577. MergeUserIDs: mergeWhitelistUsers,
  578. MergeTeamIDs: mergeWhitelistTeams,
  579. ApprovalsUserIDs: approvalsWhitelistUsers,
  580. ApprovalsTeamIDs: approvalsWhitelistTeams,
  581. })
  582. if err != nil {
  583. ctx.Error(http.StatusInternalServerError, "UpdateProtectBranch", err)
  584. return
  585. }
  586. if isBranchExist {
  587. if err = pull_service.CheckPRsForBaseBranch(ctx, ctx.Repo.Repository, ruleName); err != nil {
  588. ctx.Error(http.StatusInternalServerError, "CheckPRsForBaseBranch", err)
  589. return
  590. }
  591. } else {
  592. if !isPlainRule {
  593. if ctx.Repo.GitRepo == nil {
  594. ctx.Repo.GitRepo, err = git.OpenRepository(ctx, ctx.Repo.Repository.RepoPath())
  595. if err != nil {
  596. ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
  597. return
  598. }
  599. defer func() {
  600. ctx.Repo.GitRepo.Close()
  601. ctx.Repo.GitRepo = nil
  602. }()
  603. }
  604. // FIXME: since we only need to recheck files protected rules, we could improve this
  605. matchedBranches, err := git_model.FindAllMatchedBranches(ctx, ctx.Repo.Repository.ID, ruleName)
  606. if err != nil {
  607. ctx.Error(http.StatusInternalServerError, "FindAllMatchedBranches", err)
  608. return
  609. }
  610. for _, branchName := range matchedBranches {
  611. if err = pull_service.CheckPRsForBaseBranch(ctx, ctx.Repo.Repository, branchName); err != nil {
  612. ctx.Error(http.StatusInternalServerError, "CheckPRsForBaseBranch", err)
  613. return
  614. }
  615. }
  616. }
  617. }
  618. // Reload from db to get all whitelists
  619. bp, err := git_model.GetProtectedBranchRuleByName(ctx, ctx.Repo.Repository.ID, ruleName)
  620. if err != nil {
  621. ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
  622. return
  623. }
  624. if bp == nil || bp.RepoID != ctx.Repo.Repository.ID {
  625. ctx.Error(http.StatusInternalServerError, "New branch protection not found", err)
  626. return
  627. }
  628. ctx.JSON(http.StatusCreated, convert.ToBranchProtection(ctx, bp))
  629. }
  630. // EditBranchProtection edits a branch protection for a repo
  631. func EditBranchProtection(ctx *context.APIContext) {
  632. // swagger:operation PATCH /repos/{owner}/{repo}/branch_protections/{name} repository repoEditBranchProtection
  633. // ---
  634. // summary: Edit a branch protections for a repository. Only fields that are set will be changed
  635. // consumes:
  636. // - application/json
  637. // produces:
  638. // - application/json
  639. // parameters:
  640. // - name: owner
  641. // in: path
  642. // description: owner of the repo
  643. // type: string
  644. // required: true
  645. // - name: repo
  646. // in: path
  647. // description: name of the repo
  648. // type: string
  649. // required: true
  650. // - name: name
  651. // in: path
  652. // description: name of protected branch
  653. // type: string
  654. // required: true
  655. // - name: body
  656. // in: body
  657. // schema:
  658. // "$ref": "#/definitions/EditBranchProtectionOption"
  659. // responses:
  660. // "200":
  661. // "$ref": "#/responses/BranchProtection"
  662. // "404":
  663. // "$ref": "#/responses/notFound"
  664. // "422":
  665. // "$ref": "#/responses/validationError"
  666. form := web.GetForm(ctx).(*api.EditBranchProtectionOption)
  667. repo := ctx.Repo.Repository
  668. bpName := ctx.Params(":name")
  669. protectBranch, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, bpName)
  670. if err != nil {
  671. ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
  672. return
  673. }
  674. if protectBranch == nil || protectBranch.RepoID != repo.ID {
  675. ctx.NotFound()
  676. return
  677. }
  678. if form.EnablePush != nil {
  679. if !*form.EnablePush {
  680. protectBranch.CanPush = false
  681. protectBranch.EnableWhitelist = false
  682. protectBranch.WhitelistDeployKeys = false
  683. } else {
  684. protectBranch.CanPush = true
  685. if form.EnablePushWhitelist != nil {
  686. if !*form.EnablePushWhitelist {
  687. protectBranch.EnableWhitelist = false
  688. protectBranch.WhitelistDeployKeys = false
  689. } else {
  690. protectBranch.EnableWhitelist = true
  691. if form.PushWhitelistDeployKeys != nil {
  692. protectBranch.WhitelistDeployKeys = *form.PushWhitelistDeployKeys
  693. }
  694. }
  695. }
  696. }
  697. }
  698. if form.EnableMergeWhitelist != nil {
  699. protectBranch.EnableMergeWhitelist = *form.EnableMergeWhitelist
  700. }
  701. if form.EnableStatusCheck != nil {
  702. protectBranch.EnableStatusCheck = *form.EnableStatusCheck
  703. }
  704. if form.StatusCheckContexts != nil {
  705. protectBranch.StatusCheckContexts = form.StatusCheckContexts
  706. }
  707. if form.RequiredApprovals != nil && *form.RequiredApprovals >= 0 {
  708. protectBranch.RequiredApprovals = *form.RequiredApprovals
  709. }
  710. if form.EnableApprovalsWhitelist != nil {
  711. protectBranch.EnableApprovalsWhitelist = *form.EnableApprovalsWhitelist
  712. }
  713. if form.BlockOnRejectedReviews != nil {
  714. protectBranch.BlockOnRejectedReviews = *form.BlockOnRejectedReviews
  715. }
  716. if form.BlockOnOfficialReviewRequests != nil {
  717. protectBranch.BlockOnOfficialReviewRequests = *form.BlockOnOfficialReviewRequests
  718. }
  719. if form.DismissStaleApprovals != nil {
  720. protectBranch.DismissStaleApprovals = *form.DismissStaleApprovals
  721. }
  722. if form.RequireSignedCommits != nil {
  723. protectBranch.RequireSignedCommits = *form.RequireSignedCommits
  724. }
  725. if form.ProtectedFilePatterns != nil {
  726. protectBranch.ProtectedFilePatterns = *form.ProtectedFilePatterns
  727. }
  728. if form.UnprotectedFilePatterns != nil {
  729. protectBranch.UnprotectedFilePatterns = *form.UnprotectedFilePatterns
  730. }
  731. if form.BlockOnOutdatedBranch != nil {
  732. protectBranch.BlockOnOutdatedBranch = *form.BlockOnOutdatedBranch
  733. }
  734. var whitelistUsers []int64
  735. if form.PushWhitelistUsernames != nil {
  736. whitelistUsers, err = user_model.GetUserIDsByNames(ctx, form.PushWhitelistUsernames, false)
  737. if err != nil {
  738. if user_model.IsErrUserNotExist(err) {
  739. ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
  740. return
  741. }
  742. ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
  743. return
  744. }
  745. } else {
  746. whitelistUsers = protectBranch.WhitelistUserIDs
  747. }
  748. var mergeWhitelistUsers []int64
  749. if form.MergeWhitelistUsernames != nil {
  750. mergeWhitelistUsers, err = user_model.GetUserIDsByNames(ctx, form.MergeWhitelistUsernames, false)
  751. if err != nil {
  752. if user_model.IsErrUserNotExist(err) {
  753. ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
  754. return
  755. }
  756. ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
  757. return
  758. }
  759. } else {
  760. mergeWhitelistUsers = protectBranch.MergeWhitelistUserIDs
  761. }
  762. var approvalsWhitelistUsers []int64
  763. if form.ApprovalsWhitelistUsernames != nil {
  764. approvalsWhitelistUsers, err = user_model.GetUserIDsByNames(ctx, form.ApprovalsWhitelistUsernames, false)
  765. if err != nil {
  766. if user_model.IsErrUserNotExist(err) {
  767. ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
  768. return
  769. }
  770. ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
  771. return
  772. }
  773. } else {
  774. approvalsWhitelistUsers = protectBranch.ApprovalsWhitelistUserIDs
  775. }
  776. var whitelistTeams, mergeWhitelistTeams, approvalsWhitelistTeams []int64
  777. if repo.Owner.IsOrganization() {
  778. if form.PushWhitelistTeams != nil {
  779. whitelistTeams, err = organization.GetTeamIDsByNames(repo.OwnerID, form.PushWhitelistTeams, false)
  780. if err != nil {
  781. if organization.IsErrTeamNotExist(err) {
  782. ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
  783. return
  784. }
  785. ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
  786. return
  787. }
  788. } else {
  789. whitelistTeams = protectBranch.WhitelistTeamIDs
  790. }
  791. if form.MergeWhitelistTeams != nil {
  792. mergeWhitelistTeams, err = organization.GetTeamIDsByNames(repo.OwnerID, form.MergeWhitelistTeams, false)
  793. if err != nil {
  794. if organization.IsErrTeamNotExist(err) {
  795. ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
  796. return
  797. }
  798. ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
  799. return
  800. }
  801. } else {
  802. mergeWhitelistTeams = protectBranch.MergeWhitelistTeamIDs
  803. }
  804. if form.ApprovalsWhitelistTeams != nil {
  805. approvalsWhitelistTeams, err = organization.GetTeamIDsByNames(repo.OwnerID, form.ApprovalsWhitelistTeams, false)
  806. if err != nil {
  807. if organization.IsErrTeamNotExist(err) {
  808. ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
  809. return
  810. }
  811. ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
  812. return
  813. }
  814. } else {
  815. approvalsWhitelistTeams = protectBranch.ApprovalsWhitelistTeamIDs
  816. }
  817. }
  818. err = git_model.UpdateProtectBranch(ctx, ctx.Repo.Repository, protectBranch, git_model.WhitelistOptions{
  819. UserIDs: whitelistUsers,
  820. TeamIDs: whitelistTeams,
  821. MergeUserIDs: mergeWhitelistUsers,
  822. MergeTeamIDs: mergeWhitelistTeams,
  823. ApprovalsUserIDs: approvalsWhitelistUsers,
  824. ApprovalsTeamIDs: approvalsWhitelistTeams,
  825. })
  826. if err != nil {
  827. ctx.Error(http.StatusInternalServerError, "UpdateProtectBranch", err)
  828. return
  829. }
  830. isPlainRule := !git_model.IsRuleNameSpecial(bpName)
  831. var isBranchExist bool
  832. if isPlainRule {
  833. isBranchExist = git.IsBranchExist(ctx.Req.Context(), ctx.Repo.Repository.RepoPath(), bpName)
  834. }
  835. if isBranchExist {
  836. if err = pull_service.CheckPRsForBaseBranch(ctx, ctx.Repo.Repository, bpName); err != nil {
  837. ctx.Error(http.StatusInternalServerError, "CheckPrsForBaseBranch", err)
  838. return
  839. }
  840. } else {
  841. if !isPlainRule {
  842. if ctx.Repo.GitRepo == nil {
  843. ctx.Repo.GitRepo, err = git.OpenRepository(ctx, ctx.Repo.Repository.RepoPath())
  844. if err != nil {
  845. ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
  846. return
  847. }
  848. defer func() {
  849. ctx.Repo.GitRepo.Close()
  850. ctx.Repo.GitRepo = nil
  851. }()
  852. }
  853. // FIXME: since we only need to recheck files protected rules, we could improve this
  854. matchedBranches, err := git_model.FindAllMatchedBranches(ctx, ctx.Repo.Repository.ID, protectBranch.RuleName)
  855. if err != nil {
  856. ctx.Error(http.StatusInternalServerError, "FindAllMatchedBranches", err)
  857. return
  858. }
  859. for _, branchName := range matchedBranches {
  860. if err = pull_service.CheckPRsForBaseBranch(ctx, ctx.Repo.Repository, branchName); err != nil {
  861. ctx.Error(http.StatusInternalServerError, "CheckPrsForBaseBranch", err)
  862. return
  863. }
  864. }
  865. }
  866. }
  867. // Reload from db to ensure get all whitelists
  868. bp, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, bpName)
  869. if err != nil {
  870. ctx.Error(http.StatusInternalServerError, "GetProtectedBranchBy", err)
  871. return
  872. }
  873. if bp == nil || bp.RepoID != ctx.Repo.Repository.ID {
  874. ctx.Error(http.StatusInternalServerError, "New branch protection not found", err)
  875. return
  876. }
  877. ctx.JSON(http.StatusOK, convert.ToBranchProtection(ctx, bp))
  878. }
  879. // DeleteBranchProtection deletes a branch protection for a repo
  880. func DeleteBranchProtection(ctx *context.APIContext) {
  881. // swagger:operation DELETE /repos/{owner}/{repo}/branch_protections/{name} repository repoDeleteBranchProtection
  882. // ---
  883. // summary: Delete a specific branch protection for the repository
  884. // produces:
  885. // - application/json
  886. // parameters:
  887. // - name: owner
  888. // in: path
  889. // description: owner of the repo
  890. // type: string
  891. // required: true
  892. // - name: repo
  893. // in: path
  894. // description: name of the repo
  895. // type: string
  896. // required: true
  897. // - name: name
  898. // in: path
  899. // description: name of protected branch
  900. // type: string
  901. // required: true
  902. // responses:
  903. // "204":
  904. // "$ref": "#/responses/empty"
  905. // "404":
  906. // "$ref": "#/responses/notFound"
  907. repo := ctx.Repo.Repository
  908. bpName := ctx.Params(":name")
  909. bp, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, bpName)
  910. if err != nil {
  911. ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
  912. return
  913. }
  914. if bp == nil || bp.RepoID != repo.ID {
  915. ctx.NotFound()
  916. return
  917. }
  918. if err := git_model.DeleteProtectedBranch(ctx, ctx.Repo.Repository.ID, bp.ID); err != nil {
  919. ctx.Error(http.StatusInternalServerError, "DeleteProtectedBranch", err)
  920. return
  921. }
  922. ctx.Status(http.StatusNoContent)
  923. }