{"record":{"id":"a665845157091c7f","repo":"SonarSource/sonarqube","slug":"pull-request-with-key-does-not-target-branch","errorCode":null,"errorMessage":"Pull request with key '{}' does not target branch '{}'","messagePattern":"Pull request with key '(.+?)' does not target branch '(.+?)'","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":400,"severity":"error","filePath":"server/sonar-webserver-es/src/main/java/org/sonar/server/issue/index/IssueQueryFactory.java","lineNumber":226,"sourceCode":"    checkArgument(StringUtils.isNotBlank(fixedInPullRequest), \"Parameter '%s' is empty\", PARAM_FIXED_IN_PULL_REQUEST);\n    List<String> componentKeys = request.getComponentKeys();\n    if (componentKeys == null || componentKeys.size() != 1) {\n      throw new IllegalArgumentException(\"Exactly one project needs to be provided in the \" +\n        \"'\" + PARAM_COMPONENTS + \"' param when used together with '\" + PARAM_FIXED_IN_PULL_REQUEST + \"' param\");\n    }\n    String projectKey = componentKeys.get(0);\n    ProjectDto projectDto = dbClient.projectDao().selectProjectByKey(dbSession, projectKey)\n      .orElseThrow(() -> new IllegalArgumentException(\"Project with key '\" + projectKey + \"' does not exist\"));\n    BranchDto pullRequest = dbClient.branchDao().selectByPullRequestKey(dbSession, projectDto.getUuid(), fixedInPullRequest)\n      .orElseThrow(() -> new IllegalArgumentException(\"Pull request with key '\" + fixedInPullRequest + \"' does not exist for project \" +\n        projectKey));\n\n    String branch = request.getBranch();\n    if (branch != null) {\n      BranchDto targetBranch = dbClient.branchDao().selectByBranchKey(dbSession, projectDto.getUuid(), branch)\n        .orElseThrow(() -> new IllegalArgumentException(\"Branch with key '\" + branch + \"' does not exist\"));\n      if (!Objects.equals(targetBranch.getUuid(), pullRequest.getMergeBranchUuid())) {\n        throw new IllegalArgumentException(\"Pull request with key '\" + fixedInPullRequest + \"' does not target branch '\" + branch + \"'\");\n      }\n    }\n    return dbClient.issueFixedDao().selectByPullRequest(dbSession, pullRequest.getUuid())\n      .stream()\n      .map(IssueFixedDto::issueKey)\n      .collect(Collectors.toSet());\n  }\n\n  private static Optional<ZoneId> parseTimeZone(@Nullable String timeZone) {\n    if (timeZone == null) {\n      return Optional.empty();\n    }\n    try {\n      return Optional.of(ZoneId.of(timeZone));\n    } catch (DateTimeException e) {\n      LOGGER.warn(\"TimeZone '\" + timeZone + \"' cannot be parsed as a valid zone ID\");\n      return Optional.empty();\n    }","sourceCodeStart":208,"sourceCodeEnd":244,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-webserver-es/src/main/java/org/sonar/server/issue/index/IssueQueryFactory.java#L208-L244","documentation":"In getIssuesFixedByPullRequest, when a branch param is supplied, the code verifies the pull request actually targets that branch by comparing the target branch UUID with the PR's mergeBranchUuid. A mismatch throws IllegalArgumentException 'Pull request with key X does not target branch Y'.","triggerScenarios":"Calling issue search with fixedInPullRequest=PR-12 and branch=main while PR-12 was opened against a different branch (e.g. develop); passing a branch name that exists but is not the PR's base branch.","commonSituations":"Dashboards hard-coding branch=main while PRs target feature branches; renamed or re-targeted pull requests in the SCM after a cached query was saved; copy-pasted queries across projects.","solutions":["Omit the branch parameter when searching issues fixed by a pull request.","Set branch to the pull request's actual base (merge) branch, viewable in the SCM PR details.","Re-target the pull request to the expected branch if the intent is to query against it."],"exampleFix":"// before\nsearchRequest.fixedInPullRequest(\"PR-12\").branch(\"main\"); // PR targets develop\n// after\nsearchRequest.fixedInPullRequest(\"PR-12\").branch(\"develop\");","handlingStrategy":"validation","validationCode":"BranchDto pr = branchDao.selectByPullRequestKey(dbSession, uuid, prKey).orElseThrow();\nBranchDto target = branchDao.selectByBranchKey(dbSession, uuid, branch).orElseThrow();\nif (!target.getUuid().equals(pr.getMergeBranchUuid())) { /* wrong base branch — omit or fix branch param */ }","typeGuard":null,"tryCatchPattern":"try { search(req); } catch (IllegalArgumentException e) { if (e.getMessage().contains(\"does not target branch\")) { /* retry without the branch param */ } }","preventionTips":["Fetch the PR's base branch from the SCM before querying","Avoid hard-coding branch=main in saved queries","Drop the branch filter when searching fixedInPullRequest issues"],"tags":["issues-search","pull-request","branching","java"],"backgroundTag":"invalid-argument-value","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}