import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.NullAndEmptySource;
+import org.junit.jupiter.params.provider.NullSource;
+import org.junit.jupiter.params.provider.ValueSource;
import org.sonar.db.component.ComponentQualifiers;
import org.sonar.db.component.ComponentScopes;
import org.sonar.api.utils.System2;
import org.sonar.db.component.BranchDto;
import org.sonar.db.component.BranchType;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.property.PropertyDto;
import org.sonar.server.component.ComponentTypesRule;
import org.sonar.db.project.CreationMethod;
import org.sonar.db.project.ProjectDto;
import static org.sonar.db.component.ComponentQualifiers.PROJECT;
import static org.sonar.db.component.ComponentQualifiers.VIEW;
import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;
+import static org.sonar.server.common.component.ComponentUpdater.ENABLED_FOR_ALL_PROJECTS;
+import static org.sonar.server.common.component.ComponentUpdater.SUGGESTION_FEATURE_ENABLED_PROPERTY;
public class ComponentUpdaterIT {
.creationMethod(CreationMethod.LOCAL_API)
.build();
ProjectDto projectDto = underTest.create(db.getSession(), creationParameters).projectDto();
+ assertThat(projectDto.getAiCodeFixEnabled()).isFalse();
assertThat(projectDto.getCreationMethod()).isEqualTo(CreationMethod.LOCAL_API);
}
+ @ParameterizedTest
+ @NullSource
+ @ValueSource(strings = {"DISABLED", "ENABLED_FOR_ALL_PROJECTS", "ENABLED_FOR_SOME_PROJECTS"})
+ void create_whenAiCodeFixEnabledForAllProjects_setAiCodeFixEnabled(String enablementOption) {
+ Optional.ofNullable(enablementOption).ifPresent(s -> {
+ db.properties()
+ .insertProperty(SUGGESTION_FEATURE_ENABLED_PROPERTY, enablementOption, null);
+ });
+
+ ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
+ .newComponent(DEFAULT_COMPONENT)
+ .creationMethod(CreationMethod.LOCAL_API)
+ .mainBranchName("main")
+ .build();
+
+ ProjectDto projectDto = underTest.create(db.getSession(), creationParameters).projectDto();
+ assertThat(projectDto.getAiCodeFixEnabled()).isEqualTo(ENABLED_FOR_ALL_PROJECTS.equals(enablementOption));
+ db.getDbClient().purgeDao().deleteProject(db.getSession(), projectDto.getUuid(), projectDto.getQualifier(), projectDto.getName(), projectDto.getKey());
+ }
+
@Test
public void create_whenCreationMethodIsAlmImportBrowser_persistsIt() {
ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
import org.sonar.db.portfolio.PortfolioDto.SelectionMode;
import org.sonar.db.project.CreationMethod;
import org.sonar.db.project.ProjectDto;
+import org.sonar.db.property.PropertyDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.common.permission.Operation;
import org.sonar.server.common.permission.PermissionTemplateService;
public class ComponentUpdater {
+ static final String SUGGESTION_FEATURE_ENABLED_PROPERTY = "sonar.ai.suggestions.enabled";
+ static final String ENABLED_FOR_ALL_PROJECTS = "ENABLED_FOR_ALL_PROJECTS";
private static final Set<String> PROJ_APP_QUALIFIERS = Set.of(ComponentQualifiers.PROJECT, ComponentQualifiers.APP);
private static final String KEY_ALREADY_EXISTS_ERROR = "Could not create %s with key: \"%s\". A similar key already exists: \"%s\"";
private static final String MALFORMED_KEY_ERROR = "Malformed key for %s: '%s'. %s.";
PortfolioDto portfolioDto = null;
if (isProjectOrApp(componentDto)) {
- projectDto = toProjectDto(componentDto, now, componentCreationParameters.creationMethod());
+ var isAiCodeFixEnabled = isAiCodeFixEnabledForAllProjects();
+ projectDto = toProjectDto(componentDto, now, componentCreationParameters.creationMethod(), isAiCodeFixEnabled);
dbClient.projectDao().insert(dbSession, projectDto);
addToFavourites(dbSession, projectDto, componentCreationParameters.userUuid(), componentCreationParameters.userLogin());
mainBranch = createMainBranch(dbSession, componentDto.uuid(), projectDto.getUuid(), componentCreationParameters.mainBranchName());
return new ComponentCreationData(componentDto, portfolioDto, mainBranch, projectDto);
}
+ private boolean isAiCodeFixEnabledForAllProjects() {
+ return Optional.ofNullable(dbClient.propertiesDao().selectGlobalProperty(SUGGESTION_FEATURE_ENABLED_PROPERTY))
+ .map(PropertyDto::getValue)
+ .stream().anyMatch(ENABLED_FOR_ALL_PROJECTS::equals);
+ }
+
private void applyPublicPermissionsForCreator(DbSession dbSession, ProjectDto projectDto, @Nullable String userUuid) {
if (userUuid != null) {
UserDto userDto = dbClient.userDao().selectByUuid(dbSession, userUuid);
return component;
}
- private ProjectDto toProjectDto(ComponentDto component, long now, CreationMethod creationMethod) {
+ private ProjectDto toProjectDto(ComponentDto component, long now, CreationMethod creationMethod, boolean isAiCodeFixEnabled) {
return new ProjectDto()
.setUuid(uuidFactory.create())
.setKey(component.getKey())
.setPrivate(component.isPrivate())
.setDescription(component.description())
.setCreationMethod(creationMethod)
+ .setAiCodeFixEnabled(isAiCodeFixEnabled)
.setUpdatedAt(now)
.setCreatedAt(now);
}