{"record":{"id":"e537597d22fad016","repo":"theonedev/onedev","slug":"branch-creation-prohibited-by-branch-protection-ru","errorCode":null,"errorMessage":"Branch creation prohibited by branch protection rule","messagePattern":"Branch creation prohibited by branch protection rule","errorType":"http","errorClass":"ExplicitException","httpStatus":400,"severity":"error","filePath":"server-core/src/main/java/io/onedev/server/rest/resource/RepositoryResource.java","lineNumber":160,"sourceCode":"\t\t\n\t\tresponse.refName = ref.getName();\n\t\tresponse.commitHash = project.getRevCommit(ref.getObjectId(), true).getName();\n\t\t\n\t\treturn response;\n\t}\n\n\t@Api(order=30, description=\"Create a new branch\")\n\t@Path(\"/{projectId}/branches\")\n\t@POST\n\tpublic Response createBranch(@PathParam(\"projectId\") Long projectId, @NotNull CreateBranchRequest request) {\n\t\tProject project = projectService.load(projectId);\n\t\tUser user = SecurityUtils.getUser();\n\t\tif (!SecurityUtils.canWriteCode(project)) \n\t\t\tthrow new UnauthorizedException();\n\t\telse if (project.getBranchRef(request.getBranchName()) != null) \n\t\t\tthrow new NotAcceptableException(\"Branch '\" + request.getBranchName() + \"' already exists\");\n\t\telse if (project.getBranchProtection(request.getBranchName(), user).isPreventCreation()) \n\t\t\tthrow new ExplicitException(\"Branch creation prohibited by branch protection rule\");\n\t\t\n\t\tif (!project.isCommitSignatureRequirementSatisfied(\n\t\t\t\tuser, request.getBranchName(), \n\t\t\t\tproject.getRevCommit(request.getRevision(), true))) {\n\t\t\tthrow new ExplicitException(\"Cannot create this branch as branch protection setting \"\n\t\t\t\t\t+ \"requires valid signature on head commit\");\n\t\t}\n\t\t\n\t\tgitService.createBranch(project, request.getBranchName(), request.getRevision());\n\n\t\treturn Response.ok().build();\n\t}\n\n\t@Api(order=40, description=\"Delete specified branch\")\n\t@Path(\"/{projectId}/branches/{branch:.*}\")\n\t@DELETE\n\tpublic Response deleteBranch(@PathParam(\"projectId\") Long projectId, \n\t\t\t@PathParam(\"branch\") @Api(example=\"test-branch\") String branchName) {","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/theonedev/onedev/blob/d44925c47c37992c828ea673a5f9620539bc3ff2/server-core/src/main/java/io/onedev/server/rest/resource/RepositoryResource.java#L142-L178","documentation":"POST /{projectId}/branches throws ExplicitException when a branch protection rule for the requested branch name has 'prevent creation' enabled. This is an intentional policy rejection by OneDev, not a system failure.","triggerScenarios":"Calling POST /~api/{projectId}/branches where the branch name matches a branch protection pattern whose 'prevent creation' option is checked, for the acting user.","commonSituations":"Creating branches matching protected patterns like 'release/*' or 'main' that admins locked; automated tooling unaware of protection rules; permission model changes after new protection rules were added.","solutions":["Ask a project admin to allow creation for that pattern in Branch Protection settings","Create a branch with a name not matched by the protection rule","Use an allowed alternative (e.g. create branch then push, only if policy permits)"],"exampleFix":"// before\nPOST /~api/1/branches {\"branchName\":\"release/1.0\",\"revision\":\"main\"} // blocked\n// after (name outside protected pattern)\nPOST /~api/1/branches {\"branchName\":\"feature/release-1.0-prep\",\"revision\":\"main\"}","handlingStrategy":"validation","validationCode":"// review branch protection rules via project settings/API before naming branches\nconst forbidden = protectionRules.some(r => r.preventCreation && matches(r.pattern, name));\nif (forbidden) throw new Error('branch name protected');","typeGuard":null,"tryCatchPattern":"try {\n  createBranch(projectId, req);\n} catch (ExplicitException e) {\n  // show policy message to user; suggest compliant name\n}","preventionTips":["Review Branch Protection settings before choosing branch names","Document protected patterns for automation authors","Namespace generated branches (e.g. ci/*) outside protected patterns"],"tags":["rest","git","branch-protection","policy"],"backgroundTag":"insufficient-permissions","analyzedSha":"d44925c47c37992c828ea673a5f9620539bc3ff2","analyzedAt":"2026-09-06T07:18:27.995Z","contentChangedAt":"2026-09-06T07:18:27.995Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}