{"record":{"id":"c3bb355498015447","repo":"SonarSource/sonarqube","slug":"if-branch-key-is-specified-project-key-needs-to-b","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/SetAction.java","lineNumber":141,"sourceCode":"    action.createParam(PARAM_VALUE)\n      .setDescription(\"Value<br/>\" +\n        \"For each type, a different value is expected:\" +\n        BEGIN_LIST +\n        BEGIN_ITEM_LIST + \"the uuid of an analysis, when type is \" + SPECIFIC_ANALYSIS.name() + END_ITEM_LIST +\n        BEGIN_ITEM_LIST + \"no value, when type is \" + PREVIOUS_VERSION.name() + END_ITEM_LIST +\n        BEGIN_ITEM_LIST + \"a number between 1 and 90, when type is \" + NUMBER_OF_DAYS.name() + END_ITEM_LIST +\n        BEGIN_ITEM_LIST + \"a string, when type is \" + REFERENCE_BRANCH.name() + END_ITEM_LIST +\n        END_LIST\n      );\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      String typeStr = request.mandatoryParam(PARAM_TYPE);\n      String valueStr = request.getParam(PARAM_VALUE).emptyAsNull().or(() -> null);\n      boolean isCommunityEdition = editionProvider.get().filter(t -> t == EditionProvider.Edition.COMMUNITY).isPresent();\n\n      NewCodePeriodType type = validateType(typeStr, projectKey == null, branchKey != null || isCommunityEdition);\n\n      NewCodePeriodDto dto = new NewCodePeriodDto();\n      dto.setType(type);\n\n      ProjectDto project = null;\n      BranchDto branch = null;\n\n      if (projectKey != null) {\n        project = getProject(dbSession, projectKey);\n        userSession.checkEntityPermission(ProjectPermission.ADMIN, project);","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java#L123-L159","documentation":"SetAction.handle rejects requests where a branch key is provided without a project key. The New Code Definition 'set' WS endpoint requires 'project' whenever 'branch' is given, because the branch is always resolved in the context of its parent project.","triggerScenarios":"POSTing to api/new_code_periods/set with param branch set but param project omitted (e.g. intending to set an organization-wide or branch-only definition).","commonSituations":"Automation that iterates branches and forgets the enclosing project key; copying a curl command and deleting the project param; assuming branch alone identifies the component.","solutions":["Add the project key parameter whenever you pass a branch key.","If you meant a project-wide (or overall) definition, remove the branch parameter instead.","Derive the project key from the branch's parent before calling the endpoint in scripts."],"exampleFix":"// before\nPOST api/new_code_periods/set?branch=feature-1&type=REFERENCE_BRANCH&value=master\n// after\nPOST api/new_code_periods/set?project=my-app&branch=feature-1&type=REFERENCE_BRANCH&value=master","handlingStrategy":"validation","validationCode":"if (params.branch && !params.project) throw new Error('branch requires project');\nawait api.post('/api/new_code_periods/set', 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":["Treat branch as always nested under project in every new_code_periods call.","Build param objects from a helper that enforces the project-with-branch invariant."],"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"}