private List<Setting> loadSettings(DbSession dbSession, Optional<ComponentDto> component, Set<String> keys) { // List of settings must be kept in the following orders : default -> global -> component -> branch List<Setting> settings = new ArrayList<>(); settings.addAll(loadDefaultValues(keys)); settings.addAll(loadGlobalSettings(dbSession, keys)); if (component.isPresent() && component.get().getBranch() != null && component.get().getMainBranchProjectUuid() != null) { ComponentDto project = dbClient.componentDao().selectOrFailByUuid(dbSession, component.get().getMainBranchProjectUuid()); settings.addAll(loadComponentSettings(dbSession, keys, project).values()); } component.ifPresent(componentDto -> settings.addAll(loadComponentSettings(dbSession, keys, componentDto).values())); return settings.stream() .filter(s -> settingsWsSupport.isVisible(s.getKey(), s.getDefinition(), component)) .collect(Collectors.toList()); }
private static void setBranch(IssueQuery.Builder builder, ComponentDto component, @Nullable String branch, @Nullable String pullRequest) { builder.branchUuid(branch == null && pullRequest == null ? null : component.projectUuid()); builder.mainBranch(UNKNOWN_COMPONENT.equals(component) || (branch == null && pullRequest == null) || (branch != null && !branch.equals(component.getBranch())) || (pullRequest != null && !pullRequest.equals(component.getPullRequest()))); } }
private Optional<CurrentWsResponse.Homepage> projectHomepage(DbSession dbSession, UserDto user) { Optional<ComponentDto> projectOptional = ofNullable(dbClient.componentDao().selectByUuid(dbSession, of(user.getHomepageParameter()).orElse(EMPTY)).orElse(null)); if (shouldCleanProjectHomepage(projectOptional)) { cleanUserHomepageInDb(dbSession, user); return empty(); } CurrentWsResponse.Homepage.Builder homepage = CurrentWsResponse.Homepage.newBuilder() .setType(CurrentWsResponse.HomepageType.valueOf(user.getHomepageType())) .setComponent(projectOptional.get().getKey()); ofNullable(projectOptional.get().getBranch()).ifPresent(homepage::setBranch); return of(homepage.build()); }
@Test public void getKey_and_getBranch() { ComponentDto underTest = new ComponentDto().setDbKey("my_key:BRANCH:my_branch"); assertThat(underTest.getKey()).isEqualTo("my_key"); assertThat(underTest.getBranch()).isEqualTo("my_branch"); underTest = new ComponentDto().setDbKey("my_key"); assertThat(underTest.getKey()).isEqualTo("my_key"); assertThat(underTest.getBranch()).isNull(); }
private static String generateKey(String key, ComponentDto parentModuleOrProject) { String branch = parentModuleOrProject.getBranch(); if (branch != null) { return generateBranchKey(key, branch); } String pullRequest = parentModuleOrProject.getPullRequest(); if (pullRequest != null) { return generatePullRequestKey(key, pullRequest); } return key; }
private ReportAnalysisFailureNotification buildNotification(CeActivityDto ceActivityDto, ComponentDto projectDto, @Nullable Throwable error) { Long executedAt = ceActivityDto.getExecutedAt(); return new ReportAnalysisFailureNotification( new ReportAnalysisFailureNotification.Project( projectDto.uuid(), projectDto.getKey(), projectDto.name(), projectDto.getBranch()), new ReportAnalysisFailureNotification.Task( ceActivityDto.getUuid(), ceActivityDto.getSubmittedAt(), executedAt == null ? system2.now() : executedAt), error == null ? null : error.getMessage()); } }
@Override public void handle(Request request, Response response) { try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto component = loadComponent(dbSession, request); userSession.checkComponentPermission(UserRole.CODEVIEWER, component); String duplications = findDataFromComponent(dbSession, component); String branch = component.getBranch(); String pullRequest = component.getPullRequest(); List<DuplicationsParser.Block> blocks = parser.parse(dbSession, component, branch, pullRequest, duplications); writeProtobuf(responseBuilder.build(dbSession, blocks, branch, pullRequest), request, response); } }
@Test public void bulk_updateKey_on_branch_containing_slash() { ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch/with/slash")); String newKey = "newKey"; underTest.bulkUpdateKey(dbSession, project.uuid(), project.getKey(), newKey, t -> true); assertThat(dbClient.componentDao().selectAllComponentsFromProjectKey(dbSession, newKey)).hasSize(1); assertThat(dbClient.componentDao().selectAllComponentsFromProjectKey(dbSession, ComponentDto.generateBranchKey(newKey, branch.getBranch()))).hasSize(1); }
@Test public void search_issue_on_component_only_from_branch() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto branch = db.components().insertProjectBranch(project); ComponentDto file = db.components().insertComponent(newFileDto(branch)); assertThat(underTest.create(new SearchRequest() .setComponentKeys(singletonList(file.getKey())) .setBranch(branch.getBranch()) .setOnComponentOnly(true))) .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.componentUuids()), IssueQuery::isMainBranch) .containsOnly(branch.uuid(), singletonList(file.uuid()), false); }
private static ViewsComponent.Builder createProjectView1Builder(ComponentDto project, Long analysisDate) { return builder(Component.Type.PROJECT_VIEW, PROJECT_VIEW_1_KEY) .setUuid(PROJECT_VIEW_1_UUID) .setName(PROJECT_VIEW_1_NAME) .setDescription("project view description is not persisted") .setProjectViewAttributes(new ProjectViewAttributes(project.uuid(), project.getDbKey(), analysisDate, project.getBranch())); }
@Test public void branch_settings_contains_global_settings() { settings.setProperty("global", "global_value"); ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project); db.properties().insertProperties(newComponentPropertyDto(branch).setKey("sonar.leak.period").setValue("1")); Configuration config = underTest.newProjectConfiguration(project.getKey(), createBranch(branch.getBranch(), false)); assertThat(config.get("global")).hasValue("global_value"); assertThat(config.get("sonar.leak.period")).hasValue("1"); }
@Test public void branch_settings() { ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project); db.properties().insertProperties(newComponentPropertyDto(branch).setKey("sonar.leak.period").setValue("1")); Configuration config = underTest.newProjectConfiguration(project.getKey(), createBranch(branch.getBranch(), false)); assertThat(config.get("sonar.leak.period")).hasValue("1"); }
@Test public void branch_settings_contains_project_settings() { ComponentDto project = db.components().insertMainBranch(); db.properties().insertProperties(newComponentPropertyDto(project).setKey("key").setValue("value")); ComponentDto branch = db.components().insertProjectBranch(project); db.properties().insertProperties(newComponentPropertyDto(branch).setKey("sonar.leak.period").setValue("1")); Configuration config = underTest.newProjectConfiguration(project.getKey(), createBranch(branch.getBranch(), false)); assertThat(config.get("key")).hasValue("value"); assertThat(config.get("sonar.leak.period")).hasValue("1"); }
@Test public void return_homepage_when_set_to_a_branch() { ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project); UserDto user = db.users().insertUser(u -> u.setHomepageType("PROJECT").setHomepageParameter(branch.uuid())); userSessionRule.logIn(user).addProjectPermission(USER, project); CurrentWsResponse response = call(); assertThat(response.getHomepage()) .extracting(CurrentWsResponse.Homepage::getType, CurrentWsResponse.Homepage::getComponent, CurrentWsResponse.Homepage::getBranch) .containsExactly(CurrentWsResponse.HomepageType.PROJECT, branch.getKey(), branch.getBranch()); }
@Test public void mergeBranch_is_using_default_main_name_when_main_branch_has_no_name() { ComponentDto project = db.components().insertMainBranch(); userSession.logIn().addProjectPermission(USER, project); ComponentDto shortLivingBranch = db.components().insertProjectBranch(project, b -> b.setKey("short").setBranchType(SHORT).setMergeBranchUuid(project.uuid())); ProjectBranches.ShowWsResponse response = ws.newRequest() .setParam("project", shortLivingBranch.getKey()) .executeProtobuf(ProjectBranches.ShowWsResponse.class); assertThat(response.getBranch()) .extracting(Branch::getName, Branch::getType, Branch::getMergeBranch) .containsExactlyInAnyOrder(shortLivingBranch.getBranch(), BranchType.SHORT, "master"); }
@Test public void branch_settings_override_project_settings() { ComponentDto project = db.components().insertMainBranch(); db.properties().insertProperties(newComponentPropertyDto(project).setKey("sonar.leak.period").setValue("1")); ComponentDto branch = db.components().insertProjectBranch(project); db.properties().insertProperties(newComponentPropertyDto(branch).setKey("sonar.leak.period").setValue("2")); Configuration config = underTest.newProjectConfiguration(project.getKey(), createBranch(branch.getBranch(), false)); assertThat(config.get("sonar.leak.period")).hasValue("2"); }
@Test public void return_error_on_short_living_branch() throws ParseException { ComponentDto project = db.components().insertMainBranch(p -> p.setPrivate(false)); userSession.registerComponents(project); ComponentDto shortBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(SHORT)); TestResponse response = ws.newRequest() .setParam("project", shortBranch.getKey()) .setParam("branch", shortBranch.getBranch()) .execute(); checkError(response, "Project is invalid"); }
@Test public void fail_if_both_componentId_and_branch_parameters_provided() { ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project); ComponentDto file = db.components().insertComponent(newFileDto(branch)); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Parameter 'componentId' cannot be used at the same time as 'branch' or 'pullRequest'"); ws.newRequest() .setParam("uuid", file.uuid()) .setParam("branch", file.getBranch()) .execute(); }
@Test public void quality_gate_on_long_living_branch() { ComponentDto project = db.components().insertMainBranch(p -> p.setPrivate(false)); userSession.registerComponents(project); MetricDto metric = createQualityGateMetric(); db.measures().insertLiveMeasure(project, metric, m -> m.setData(OK.name())); ComponentDto longBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(LONG)); db.measures().insertLiveMeasure(longBranch, metric, m -> m.setData(ERROR.name())); TestResponse response = ws.newRequest() .setParam("project", longBranch.getKey()) .setParam("branch", longBranch.getBranch()) .execute(); checkResponse(response, ERROR); }
@Test public void return_error_on_short_living_branch() throws ParseException { ComponentDto project = db.components().insertMainBranch(); ComponentDto shortBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(SHORT)); UserDto user = db.users().insertUser(); userSession.logIn(user).addProjectPermission(USER, project); MetricDto metric = db.measures().insertMetric(m -> m.setKey(BUGS_KEY).setValueType(INT.name())); TestResponse response = ws.newRequest() .setParam("project", shortBranch.getKey()) .setParam("branch", shortBranch.getBranch()) .setParam("metric", metric.getKey()) .execute(); checkError(response, "Project is invalid"); }