{"record":{"id":"e08c29f14c13fd96","repo":"SonarSource/sonarqube","slug":"if-branch-key-is-specified-project-key-needs-to-b-e08c29","errorCode":null,"errorMessage":"If branch key is specified, project key needs to be specified too","messagePattern":"If branch key is specified, project key needs to be specified too","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":400,"severity":"error","filePath":"server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/ShowAction.java","lineNumber":93,"sourceCode":"        \"<li>'Execute analysis' rights on the specified component</li>\" +\n        \"</ul>\")\n      .setSince(\"8.0\")\n      .setResponseExample(getClass().getResource(\"show-example.json\"))\n      .setHandler(this);\n\n    action.createParam(PARAM_PROJECT)\n      .setDescription(\"Project key\");\n    action.createParam(PARAM_BRANCH)\n      .setDescription(\"Branch key\");\n  }\n\n  @Override\n  public void handle(Request request, Response response) throws Exception {\n    String projectKey = request.getParam(PARAM_PROJECT).emptyAsNull().or(() -> null);\n    String branchKey = request.getParam(PARAM_BRANCH).emptyAsNull().or(() -> null);\n\n    if (projectKey == null && branchKey != null) {\n      throw new IllegalArgumentException(\"If branch key is specified, project key needs to be specified too\");\n    }\n\n    try (DbSession dbSession = dbClient.openSession(false)) {\n      ProjectDto project = null;\n      BranchDto branch = null;\n      boolean inherited = false;\n\n      if (projectKey != null) {\n        try {\n          project = getProject(dbSession, projectKey);\n          checkPermission(project);\n          if (branchKey != null) {\n            try {\n              branch = getBranch(dbSession, project, branchKey);\n            } catch (NotFoundException e) {\n              inherited = true;\n            }\n          }","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/ShowAction.java#L75-L111","documentation":"ShowAction.handle mirrors SetAction's parameter rule: api/new_code_periods/show rejects a request specifying branch without project, since a branch key only resolves in the context of its parent project. The exception is thrown before any database work when projectKey is null and branchKey is not.","triggerScenarios":"GET api/new_code_periods/show?branch=feature-1 with no project parameter.","commonSituations":"Scripts querying per-branch definitions while iterating branches and omitting the project key; hand-built URLs where the project param was dropped; clients assuming branch keys are globally unique.","solutions":["Include the project key alongside the branch key.","Omit branch if you want the project-level (or effective overall) definition instead.","Compute the branch's project key from your branch inventory before calling show."],"exampleFix":"// before\nGET api/new_code_periods/show?branch=feature-1\n// after\nGET api/new_code_periods/show?project=my-app&branch=feature-1","handlingStrategy":"validation","validationCode":"if (params.branch && !params.project) throw new Error('branch requires project');\nawait api.get('/api/new_code_periods/show', { params });","typeGuard":null,"tryCatchPattern":"try { ... } catch (e) { if (String(e.message).includes('project key needs to be specified')) { params.project = resolveProjectForBranch(params.branch); return retry(params); } throw e; }","preventionTips":["Always pass project when filtering new_code_periods/show by branch.","Centralize URL construction so the project param cannot be dropped."],"tags":["webapi","new-code-period","parameters","sonarqube"],"backgroundTag":"missing-required-argument","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}