@Test public void saveBranchButExists() throws IOException, GitAPIException { when(repoManager.listProjectBranches(PROJECT)).thenReturn(Lists.newArrayList(BRANCH)); BranchForm branchForm = new BranchForm(PROJECT, BRANCH, BRANCH, null); BindingResult bindingResult = new BeanPropertyBindingResult(branchForm, "branchForm"); String view = branchController.saveBranch(branchForm, bindingResult, authentication); assertEquals("/project/" + PROJECT, removeViewPrefix(view)); assertRedirect(view); assertFalse(bindingResult.hasErrors()); verify(repoManager, never()).createProjectBranchRepository(anyString(), anyString(), anyString()); }
@RequestMapping(value="/save/{projectName:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}", method=RequestMethod.POST) @PreAuthorize("hasBranchPermission(#form.projectName, #form.name, EDIT_BRANCH)") public String saveBranch(@ModelAttribute @Valid BranchForm form, BindingResult bindingResult, Authentication authentication) throws IOException, GitAPIException { String projectName = form.getProjectName(); String name = form.getName(); String originalName = form.getOriginalName(); List<String> branches = globalRepositoryManager.listProjectBranches(projectName); boolean firstBranch = branches.isEmpty(); if (StringUtils.isNotBlank(originalName)) { if (!branches.contains(originalName)) { bindingResult.rejectValue("originalName", "branch.name.nonexistent"); } if (!StringUtils.equals(name, originalName) && branches.contains(name)) { bindingResult.rejectValue("name", "branch.name.exists"); } } else { if (branches.contains(name)) { bindingResult.rejectValue("name", "branch.name.exists"); } } if (bindingResult.hasErrors()) { return "/project/branch/edit"; } User user = userStore.getUser(authentication.getName()); if (StringUtils.isNotBlank(originalName) && !StringUtils.equals(name, originalName)) { globalRepositoryManager.renameProjectBranch(projectName, originalName, name, user); } else if (StringUtils.isBlank(originalName)) { ILockedRepository repo = null; try { repo = globalRepositoryManager.createProjectBranchRepository(projectName, name, form.getStartingBranch()); } finally { Util.closeQuietly(repo); } if (firstBranch) { Page page = Page.fromText("Home", StringUtils.EMPTY); pageStore.savePage(projectName, name, DocumentrConstants.HOME_PAGE_NAME, page, null, user); return "redirect:/page/edit/" + projectName + "/" + name + "/" + DocumentrConstants.HOME_PAGE_NAME; } } return "redirect:/project/" + projectName; }
BranchController { @RequestMapping(value="/save/{projectName:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}", method=RequestMethod.POST) @PreAuthorize("hasBranchPermission(#form.projectName, #form.name, EDIT_BRANCH)") public String saveBranch(@ModelAttribute @Valid BranchForm form, BindingResult bindingResult, Authentication authentication) throws IOException, GitAPIException { String projectName = form.getProjectName(); String name = form.getName(); String originalName = form.getOriginalName(); List<String> branches = globalRepositoryManager.listProjectBranches(projectName); boolean firstBranch = branches.isEmpty(); if (StringUtils.isNotBlank(originalName)) { if (!branches.contains(originalName)) { bindingResult.rejectValue("originalName", "branch.name.nonexistent"); } if (!StringUtils.equals(name, originalName) && branches.contains(name)) { bindingResult.rejectValue("name", "branch.name.exists"); } } else { if (branches.contains(name)) { bindingResult.rejectValue("name", "branch.name.exists"); } } if (bindingResult.hasErrors()) { return "/project/branch/edit"; } User user = userStore.getUser(authentication.getName()); if (StringUtils.isNotBlank(originalName) && !StringUtils.equals(name, originalName)) { globalRepositoryManager.renameProjectBranch(projectName, originalName, name, user); } else if (StringUtils.isBlank(originalName)) { ILockedRepository repo = null; try { repo = globalRepositoryManager.createProjectBranchRepository(projectName, name, form.getStartingBranch()); } finally { Util.closeQuietly(repo); } if (firstBranch) { Page page = Page.fromText("Home", StringUtils.EMPTY); pageStore.savePage(projectName, name, DocumentrConstants.HOME_PAGE_NAME, page, null, user); return "redirect:/page/edit/" + projectName + "/" + name + "/" + DocumentrConstants.HOME_PAGE_NAME; } } return "redirect:/project/" + projectName; } }
BranchController { @RequestMapping(value="/save/{projectName:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}", method=RequestMethod.POST) @PreAuthorize("hasBranchPermission(#form.projectName, #form.name, EDIT_BRANCH)") public String saveBranch(@ModelAttribute @Valid BranchForm form, BindingResult bindingResult, Authentication authentication) throws IOException, GitAPIException { String projectName = form.getProjectName(); String name = form.getName(); String originalName = form.getOriginalName(); List<String> branches = globalRepositoryManager.listProjectBranches(projectName); boolean firstBranch = branches.isEmpty(); if (StringUtils.isNotBlank(originalName)) { if (!branches.contains(originalName)) { bindingResult.rejectValue("originalName", "branch.name.nonexistent"); } if (!StringUtils.equals(name, originalName) && branches.contains(name)) { bindingResult.rejectValue("name", "branch.name.exists"); } } else { if (branches.contains(name)) { bindingResult.rejectValue("name", "branch.name.exists"); } } if (bindingResult.hasErrors()) { return "/project/branch/edit"; } User user = userStore.getUser(authentication.getName()); if (StringUtils.isNotBlank(originalName) && !StringUtils.equals(name, originalName)) { globalRepositoryManager.renameProjectBranch(projectName, originalName, name, user); } else if (StringUtils.isBlank(originalName)) { ILockedRepository repo = null; try { repo = globalRepositoryManager.createProjectBranchRepository(projectName, name, form.getStartingBranch()); } finally { Util.closeQuietly(repo); } if (firstBranch) { Page page = Page.fromText("Home", StringUtils.EMPTY); pageStore.savePage(projectName, name, DocumentrConstants.HOME_PAGE_NAME, page, null, user); return "redirect:/page/edit/" + projectName + "/" + name + "/" + DocumentrConstants.HOME_PAGE_NAME; } } return "redirect:/project/" + projectName; }  }
BranchController { @RequestMapping(value="/save/{projectName:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}", method=RequestMethod.POST) @PreAuthorize("hasBranchPermission(#form.projectName, #form.name, EDIT_BRANCH)") public String saveBranch(@ModelAttribute @Valid BranchForm form, BindingResult bindingResult, Authentication authentication) throws IOException, GitAPIException { String projectName = form.getProjectName(); String name = form.getName(); String originalName = form.getOriginalName(); List<String> branches = globalRepositoryManager.listProjectBranches(projectName); boolean firstBranch = branches.isEmpty(); if (StringUtils.isNotBlank(originalName)) { if (!branches.contains(originalName)) { bindingResult.rejectValue("originalName", "branch.name.nonexistent"); } if (!StringUtils.equals(name, originalName) && branches.contains(name)) { bindingResult.rejectValue("name", "branch.name.exists"); } } else { if (branches.contains(name)) { bindingResult.rejectValue("name", "branch.name.exists"); } } if (bindingResult.hasErrors()) { return "/project/branch/edit"; } User user = userStore.getUser(authentication.getName()); if (StringUtils.isNotBlank(originalName) && !StringUtils.equals(name, originalName)) { globalRepositoryManager.renameProjectBranch(projectName, originalName, name, user); } else if (StringUtils.isBlank(originalName)) { ILockedRepository repo = null; try { repo = globalRepositoryManager.createProjectBranchRepository(projectName, name, form.getStartingBranch()); } finally { Util.closeQuietly(repo); } if (firstBranch) { Page page = Page.fromText("Home", StringUtils.EMPTY); pageStore.savePage(projectName, name, DocumentrConstants.HOME_PAGE_NAME, page, null, user); return "redirect:/page/edit/" + projectName + "/" + name + "/" + DocumentrConstants.HOME_PAGE_NAME; } } return "redirect:/project/" + projectName; }  @RequestMapping(value="/create/{projectName:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}", method=RequestMethod.GET) @PreAuthorize("hasProjectPermission(#projectName, EDIT_BRANCH)") String createBranch(@PathVariable String projectName, Model model); @RequestMapping(value="/edit/{projectName:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}/" + "{name:" + DocumentrConstants.BRANCH_NAME_PATTERN + "}", method=RequestMethod.GET) @PreAuthorize("hasBranchPermission(#projectName, #name, EDIT_BRANCH)") String createBranch(@PathVariable String projectName, @PathVariable String name, Model model); @RequestMapping(value="/save/{projectName:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}", method=RequestMethod.POST) @PreAuthorize("hasBranchPermission(#form.projectName, #form.name, EDIT_BRANCH)") String saveBranch(@ModelAttribute @Valid BranchForm form, BindingResult bindingResult,
			Authentication authentication); @RequestMapping(value="/delete/{projectName:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}/" + "{name:" + DocumentrConstants.BRANCH_NAME_PATTERN + "}", method=RequestMethod.GET) @PreAuthorize("hasProjectPermission(#projectName, EDIT_BRANCH)") String deleteBranch(@PathVariable String projectName, @PathVariable String name,
			Authentication authentication); @ModelAttribute BranchForm createBranchForm(@PathVariable String projectName, @RequestParam(required=false) String name,
			@RequestParam(required=false) String originalName, @RequestParam(required=false) String startingBranch); }
BranchController { @RequestMapping(value="/save/{projectName:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}", method=RequestMethod.POST) @PreAuthorize("hasBranchPermission(#form.projectName, #form.name, EDIT_BRANCH)") public String saveBranch(@ModelAttribute @Valid BranchForm form, BindingResult bindingResult, Authentication authentication) throws IOException, GitAPIException { String projectName = form.getProjectName(); String name = form.getName(); String originalName = form.getOriginalName(); List<String> branches = globalRepositoryManager.listProjectBranches(projectName); boolean firstBranch = branches.isEmpty(); if (StringUtils.isNotBlank(originalName)) { if (!branches.contains(originalName)) { bindingResult.rejectValue("originalName", "branch.name.nonexistent"); } if (!StringUtils.equals(name, originalName) && branches.contains(name)) { bindingResult.rejectValue("name", "branch.name.exists"); } } else { if (branches.contains(name)) { bindingResult.rejectValue("name", "branch.name.exists"); } } if (bindingResult.hasErrors()) { return "/project/branch/edit"; } User user = userStore.getUser(authentication.getName()); if (StringUtils.isNotBlank(originalName) && !StringUtils.equals(name, originalName)) { globalRepositoryManager.renameProjectBranch(projectName, originalName, name, user); } else if (StringUtils.isBlank(originalName)) { ILockedRepository repo = null; try { repo = globalRepositoryManager.createProjectBranchRepository(projectName, name, form.getStartingBranch()); } finally { Util.closeQuietly(repo); } if (firstBranch) { Page page = Page.fromText("Home", StringUtils.EMPTY); pageStore.savePage(projectName, name, DocumentrConstants.HOME_PAGE_NAME, page, null, user); return "redirect:/page/edit/" + projectName + "/" + name + "/" + DocumentrConstants.HOME_PAGE_NAME; } } return "redirect:/project/" + projectName; }  @RequestMapping(value="/create/{projectName:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}", method=RequestMethod.GET) @PreAuthorize("hasProjectPermission(#projectName, EDIT_BRANCH)") String createBranch(@PathVariable String projectName, Model model); @RequestMapping(value="/edit/{projectName:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}/" + "{name:" + DocumentrConstants.BRANCH_NAME_PATTERN + "}", method=RequestMethod.GET) @PreAuthorize("hasBranchPermission(#projectName, #name, EDIT_BRANCH)") String createBranch(@PathVariable String projectName, @PathVariable String name, Model model); @RequestMapping(value="/save/{projectName:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}", method=RequestMethod.POST) @PreAuthorize("hasBranchPermission(#form.projectName, #form.name, EDIT_BRANCH)") String saveBranch(@ModelAttribute @Valid BranchForm form, BindingResult bindingResult,
			Authentication authentication); @RequestMapping(value="/delete/{projectName:" + DocumentrConstants.PROJECT_NAME_PATTERN + "}/" + "{name:" + DocumentrConstants.BRANCH_NAME_PATTERN + "}", method=RequestMethod.GET) @PreAuthorize("hasProjectPermission(#projectName, EDIT_BRANCH)") String deleteBranch(@PathVariable String projectName, @PathVariable String name,
			Authentication authentication); @ModelAttribute BranchForm createBranchForm(@PathVariable String projectName, @RequestParam(required=false) String name,
			@RequestParam(required=false) String originalName, @RequestParam(required=false) String startingBranch);  }