{"record":{"id":"35a89b313365a337","repo":"theonedev/onedev","slug":"no-branch-in-query-context","errorCode":null,"errorMessage":"No branch in query context","messagePattern":"No branch in query context","errorType":"exception","errorClass":"ExplicitException","httpStatus":null,"severity":"error","filePath":"server-core/src/main/java/io/onedev/server/search/entity/issue/ReferencedInCurrentBranchCriteria.java","lineNumber":24,"sourceCode":"import javax.persistence.criteria.Predicate;\n\nimport org.jspecify.annotations.Nullable;\n\nimport io.onedev.commons.utils.ExplicitException;\nimport io.onedev.server.model.Issue;\nimport io.onedev.server.util.ProjectScope;\nimport io.onedev.server.util.ProjectScopedBranch;\nimport io.onedev.server.util.criteria.Criteria;\n\npublic class ReferencedInCurrentBranchCriteria extends Criteria<Issue> {\n\n\tprivate static final long serialVersionUID = 1L;\n\n\t@Override\n\tpublic Predicate getPredicate(@Nullable ProjectScope projectScope, CriteriaQuery<?> query, From<Issue, Issue> from, CriteriaBuilder builder) {\n\t\tProjectScopedBranch branch = ProjectScopedBranch.get();\n\t\tif (branch == null)\n\t\t\tthrow new ExplicitException(\"No branch in query context\");\n\t\tLong issueNumber = Issue.parseNumberFromBranch(branch.getBranchName());\n\t\tif (issueNumber == null)\n\t\t\treturn builder.disjunction();\n\t\treturn builder.and(\n\t\t\t\tbuilder.equal(from.get(Issue.PROP_PROJECT), branch.getProject()),\n\t\t\t\tbuilder.equal(from.get(Issue.PROP_NUMBER), issueNumber));\n\t}\n\n\t@Override\n\tpublic boolean matches(Issue issue) {\n\t\tProjectScopedBranch branch = ProjectScopedBranch.get();\n\t\tif (branch == null)\n\t\t\tthrow new ExplicitException(\"No branch in query context\");\n\t\tLong issueNumber = Issue.parseNumberFromBranch(branch.getBranchName());\n\t\tif (issueNumber == null)\n\t\t\treturn false;\n\t\treturn issue.getProject().equals(branch.getProject())\n\t\t\t\t&& issue.getNumber() == issueNumber.longValue();","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/theonedev/onedev/blob/d44925c47c37992c828ea673a5f9620539bc3ff2/server-core/src/main/java/io/onedev/server/search/entity/issue/ReferencedInCurrentBranchCriteria.java#L6-L42","documentation":"ReferencedInCurrentBranchCriteria.getPredicate builds a predicate for issues referenced by the branch currently in context (ProjectScopedBranch.get()). When no branch is bound to the context it throws ExplicitException 'No branch in query context' since the criterion is undefined.","triggerScenarios":"Executing an issue query containing the 'referenced in current branch' criterion outside a branch-scoped context (no ProjectScopedBranch bound), e.g. global issue search or non-branch REST calls.","commonSituations":"Using a branch-context query from a commit list page where branch scope is absent; API queries reusing branch-scoped saved filters; background jobs evaluating branch criteria.","solutions":["Execute the query within a branch-scoped request (branch context present in URL).","Replace the criterion with an explicit branch or issue-reference condition.","Bind ProjectScopedBranch programmatically if evaluating outside web requests.","Catch ExplicitException and degrade to an unfiltered query."],"exampleFix":"// before\nquery: \"referenced in current branch\" (no branch context)\n// after\nGET /projects/p/repos/r/branches/feature-1/issues?query=...\n// or explicit:\nquery: \"number is 42\"","handlingStrategy":"validation","validationCode":"if (ProjectScopedBranch.get() == null)\n    throw new ExplicitException(\"Query uses branch-context criterion; run within a branch scope\");","typeGuard":"ProjectScopedBranch branch = ProjectScopedBranch.get();\nboolean branchScoped = branch != null;","tryCatchPattern":"try {\n    Predicate p = criteria.getPredicate(projectScope, query, from, builder);\n} catch (ExplicitException e) {\n    throw new QueryContextException(e.getMessage());\n}","preventionTips":["Offer 'referenced in current branch' only from branch-scoped pages.","Bind ProjectScopedBranch in programmatic evaluations.","Prefer explicit reference criteria in globally reusable saved queries."],"tags":["query-context","branch-scope","issue-search"],"backgroundTag":"missing-required-context","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"}