aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhat Yenican <104850907+serhat-yenican-sonarsource@users.noreply.github.com>2025-03-11 11:42:03 +0100
committersonartech <sonartech@sonarsource.com>2025-03-12 20:03:12 +0000
commite0077aada26d0e7272f532a9a74157f1b030f2b2 (patch)
tree52de548be5984e09d805ad1e399769a5e2371058
parent640eca60bd0edc66653e19ae3d361e4e881ad925 (diff)
downloadsonarqube-e0077aada26d0e7272f532a9a74157f1b030f2b2.tar.gz
sonarqube-e0077aada26d0e7272f532a9a74157f1b030f2b2.zip
SONAR-24413 Set default provider.key and modelKey properties
-rw-r--r--server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202502/InsertDefaultAiSuggestionProviderKeyAndModelKeyPropertiesTest.java90
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202502/DbVersion202502.java3
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202502/InsertDefaultAiSuggestionProviderKeyAndModelKeyProperties.java76
3 files changed, 168 insertions, 1 deletions
diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202502/InsertDefaultAiSuggestionProviderKeyAndModelKeyPropertiesTest.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202502/InsertDefaultAiSuggestionProviderKeyAndModelKeyPropertiesTest.java
new file mode 100644
index 00000000000..8fc64e7347e
--- /dev/null
+++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202502/InsertDefaultAiSuggestionProviderKeyAndModelKeyPropertiesTest.java
@@ -0,0 +1,90 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2025 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v202502;
+
+import java.sql.SQLException;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+import org.sonar.api.impl.utils.TestSystem2;
+import org.sonar.api.utils.System2;
+import org.sonar.core.util.SequenceUuidFactory;
+import org.sonar.db.MigrationDbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.core.config.AiCodeFixEnablementConstants.SUGGESTION_FEATURE_ENABLED_PROPERTY;
+import static org.sonar.core.config.AiCodeFixEnablementConstants.SUGGESTION_PROVIDER_KEY_PROPERTY;
+import static org.sonar.core.config.AiCodeFixEnablementConstants.SUGGESTION_PROVIDER_MODEL_KEY_PROPERTY;
+import static org.sonar.server.platform.db.migration.version.v202502.InsertDefaultAiSuggestionProviderKeyAndModelKeyProperties.DEFAULT_PROVIDER_KEY;
+import static org.sonar.server.platform.db.migration.version.v202502.InsertDefaultAiSuggestionProviderKeyAndModelKeyProperties.DEFAULT_PROVIDER_MODEL_KEY;
+import static org.sonar.server.platform.db.migration.version.v202502.InsertDefaultAiSuggestionProviderKeyAndModelKeyProperties.DISABLED;
+
+class InsertDefaultAiSuggestionProviderKeyAndModelKeyPropertiesTest {
+ private static final long NOW = 1;
+
+ @RegisterExtension
+ private final MigrationDbTester db = MigrationDbTester.createForMigrationStep(InsertDefaultAiSuggestionProviderKeyAndModelKeyProperties.class);
+ private final System2 system2 = new TestSystem2().setNow(NOW);
+ private final InsertDefaultAiSuggestionProviderKeyAndModelKeyProperties underTest = new InsertDefaultAiSuggestionProviderKeyAndModelKeyProperties(db.database(), system2,
+ new SequenceUuidFactory());
+
+ @Test
+ void execute_shouldNotUpdateAnything_whenTheAiCodeFixNotSet() throws SQLException {
+ underTest.execute();
+
+ assertThat(db.countSql(String.format("select count(*) from properties where prop_key = '%s'", SUGGESTION_FEATURE_ENABLED_PROPERTY))).isZero();
+ assertThat(db.countSql(String.format("select count(*) from properties where prop_key = '%s'", SUGGESTION_PROVIDER_KEY_PROPERTY))).isZero();
+ assertThat(db.countSql(String.format("select count(*) from properties where prop_key = '%s'", SUGGESTION_PROVIDER_MODEL_KEY_PROPERTY))).isZero();
+ }
+
+ @Test
+ void execute_shouldNotUpdateAnything_whenTheAiCodeFixDisabled() throws SQLException {
+ insertProperty(SUGGESTION_FEATURE_ENABLED_PROPERTY, DISABLED);
+ underTest.execute();
+
+ assertThat(db.selectFirst(String.format("select text_value from properties where prop_key = '%s'", SUGGESTION_FEATURE_ENABLED_PROPERTY))).containsEntry("text_value", DISABLED);
+ assertThat(db.countSql(String.format("select count(*) from properties where prop_key = '%s'", SUGGESTION_PROVIDER_KEY_PROPERTY))).isZero();
+ assertThat(db.countSql(String.format("select count(*) from properties where prop_key = '%s'", SUGGESTION_PROVIDER_MODEL_KEY_PROPERTY))).isZero();
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"ENABLED_FOR_ALL_PROJECTS", "ENABLED_FOR_SOME_PROJECTS"})
+ void execute_shouldSetDefaultValues_whenTheAiCodeFixEnabled(String enablement) throws SQLException {
+ insertProperty(SUGGESTION_FEATURE_ENABLED_PROPERTY, enablement);
+ underTest.execute();
+
+ assertThat(db.selectFirst(String.format("select text_value from properties where prop_key = '%s'", SUGGESTION_FEATURE_ENABLED_PROPERTY))).containsEntry("text_value",
+ enablement);
+ assertThat(db.selectFirst(String.format("select text_value from properties where prop_key = '%s'", SUGGESTION_PROVIDER_KEY_PROPERTY))).containsEntry("text_value",
+ DEFAULT_PROVIDER_KEY);
+ assertThat(db.selectFirst(String.format("select text_value from properties where prop_key = '%s'", SUGGESTION_PROVIDER_MODEL_KEY_PROPERTY))).containsEntry("text_value",
+ DEFAULT_PROVIDER_MODEL_KEY);
+ }
+
+ private void insertProperty(String key, String value) {
+ db.executeInsert("properties",
+ "uuid", "uuid-1",
+ "prop_key", key,
+ "is_empty", false,
+ "text_value", value,
+ "created_at", NOW);
+ }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202502/DbVersion202502.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202502/DbVersion202502.java
index 14760351949..fa10e3d5083 100644
--- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202502/DbVersion202502.java
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202502/DbVersion202502.java
@@ -51,6 +51,7 @@ public class DbVersion202502 implements DbVersion {
.add(2025_02_012, "Create index for SCA issues to releases to SCA releases", CreateIndexOnScaIssuesReleasesScaReleaseUuid.class)
.add(2025_02_013, "Create unique index for SCA issues to releases", CreateUniqueIndexOnScaIssuesReleases.class)
.add(2025_02_014, "Add new_in_pull_request column to SCA releases", AddNewInPullRequestToScaReleasesTable.class)
- .add(2025_02_015, "Add new_in_pull_request column to SCA dependencies", AddNewInPullRequestToScaDependenciesTable.class);
+ .add(2025_02_015, "Add new_in_pull_request column to SCA dependencies", AddNewInPullRequestToScaDependenciesTable.class)
+ .add(2025_02_016, "Insert default AI Codefix provider key and modelKey properties", InsertDefaultAiSuggestionProviderKeyAndModelKeyProperties.class);
}
}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202502/InsertDefaultAiSuggestionProviderKeyAndModelKeyProperties.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202502/InsertDefaultAiSuggestionProviderKeyAndModelKeyProperties.java
new file mode 100644
index 00000000000..17eb9f23a70
--- /dev/null
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202502/InsertDefaultAiSuggestionProviderKeyAndModelKeyProperties.java
@@ -0,0 +1,76 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2025 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v202502;
+
+import java.sql.SQLException;
+import java.util.Optional;
+import org.sonar.api.utils.System2;
+import org.sonar.core.util.UuidFactory;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.step.DataChange;
+
+import static org.sonar.core.config.AiCodeFixEnablementConstants.SUGGESTION_FEATURE_ENABLED_PROPERTY;
+import static org.sonar.core.config.AiCodeFixEnablementConstants.SUGGESTION_PROVIDER_KEY_PROPERTY;
+import static org.sonar.core.config.AiCodeFixEnablementConstants.SUGGESTION_PROVIDER_MODEL_KEY_PROPERTY;
+
+public class InsertDefaultAiSuggestionProviderKeyAndModelKeyProperties extends DataChange {
+
+ static final String DISABLED = "DISABLED";
+ static final String DEFAULT_PROVIDER_KEY = "OPENAI";
+ static final String DEFAULT_PROVIDER_MODEL_KEY = "OPENAI_GPT_4O";
+ private final System2 system2;
+ private final UuidFactory uuidFactory;
+
+ public InsertDefaultAiSuggestionProviderKeyAndModelKeyProperties(Database db, System2 system2, UuidFactory uuidFactory) {
+ super(db);
+ this.system2 = system2;
+ this.uuidFactory = uuidFactory;
+ }
+
+ @Override
+ protected void execute(Context context) throws SQLException {
+ boolean isAiCodeFixEnabled = Optional.ofNullable(context.prepareSelect("select text_value from properties where prop_key=?")
+ .setString(1, SUGGESTION_FEATURE_ENABLED_PROPERTY)
+ .get(r -> r.getString(1)))
+ .map(value -> !DISABLED.equals(value))
+ .orElse(false);
+
+ if (isAiCodeFixEnabled) {
+ insertProperty(context, SUGGESTION_PROVIDER_KEY_PROPERTY, DEFAULT_PROVIDER_KEY);
+ insertProperty(context, SUGGESTION_PROVIDER_MODEL_KEY_PROPERTY, DEFAULT_PROVIDER_MODEL_KEY);
+ }
+ }
+
+ private void insertProperty(Context context, String key, String value) throws SQLException {
+ context.prepareUpsert("""
+ INSERT INTO properties
+ (prop_key, is_empty, text_value, created_at, uuid)
+ VALUES(?, ?, ?, ?, ?)
+ """)
+ .setString(1, key)
+ .setBoolean(2, false)
+ .setString(3, value)
+ .setLong(4, system2.now())
+ .setString(5, uuidFactory.create())
+ .execute()
+ .commit();
+ }
+
+}