theonedev/onedev · error · ExplicitException

No branch in query context

Error message

No branch in query context

What it means

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.

Source

Thrown at server-core/src/main/java/io/onedev/server/search/entity/issue/ReferencedInCurrentBranchCriteria.java:24

import javax.persistence.criteria.Predicate;

import org.jspecify.annotations.Nullable;

import io.onedev.commons.utils.ExplicitException;
import io.onedev.server.model.Issue;
import io.onedev.server.util.ProjectScope;
import io.onedev.server.util.ProjectScopedBranch;
import io.onedev.server.util.criteria.Criteria;

public class ReferencedInCurrentBranchCriteria extends Criteria<Issue> {

	private static final long serialVersionUID = 1L;

	@Override
	public Predicate getPredicate(@Nullable ProjectScope projectScope, CriteriaQuery<?> query, From<Issue, Issue> from, CriteriaBuilder builder) {
		ProjectScopedBranch branch = ProjectScopedBranch.get();
		if (branch == null)
			throw new ExplicitException("No branch in query context");
		Long issueNumber = Issue.parseNumberFromBranch(branch.getBranchName());
		if (issueNumber == null)
			return builder.disjunction();
		return builder.and(
				builder.equal(from.get(Issue.PROP_PROJECT), branch.getProject()),
				builder.equal(from.get(Issue.PROP_NUMBER), issueNumber));
	}

	@Override
	public boolean matches(Issue issue) {
		ProjectScopedBranch branch = ProjectScopedBranch.get();
		if (branch == null)
			throw new ExplicitException("No branch in query context");
		Long issueNumber = Issue.parseNumberFromBranch(branch.getBranchName());
		if (issueNumber == null)
			return false;
		return issue.getProject().equals(branch.getProject())
				&& issue.getNumber() == issueNumber.longValue();

View on GitHub (pinned to d44925c47c)

Solutions

  1. Execute the query within a branch-scoped request (branch context present in URL).
  2. Replace the criterion with an explicit branch or issue-reference condition.
  3. Bind ProjectScopedBranch programmatically if evaluating outside web requests.
  4. Catch ExplicitException and degrade to an unfiltered query.

Example fix

// before
query: "referenced in current branch" (no branch context)
// after
GET /projects/p/repos/r/branches/feature-1/issues?query=...
// or explicit:
query: "number is 42"
Defensive patterns

Strategy: validation

Validate before calling

if (ProjectScopedBranch.get() == null)
    throw new ExplicitException("Query uses branch-context criterion; run within a branch scope");

Type guard

ProjectScopedBranch branch = ProjectScopedBranch.get();
boolean branchScoped = branch != null;

Try / catch

try {
    Predicate p = criteria.getPredicate(projectScope, query, from, builder);
} catch (ExplicitException e) {
    throw new QueryContextException(e.getMessage());
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of theonedev/onedev@d44925c47c (2026-09-06). Data as JSON: /api/errors/35a89b313365a337. Report an issue: GitHub.