From f480ae1f6255e9cf6b58bd37f2841c0cd20ce31c Mon Sep 17 00:00:00 2001 From: Antoine Vigneau Date: Wed, 20 Mar 2024 14:59:46 +0100 Subject: [PATCH] SONAR-21883 Fix SSF-570 on LTS --- .../components/almIntegration/AzureForm.tsx | 3 +++ .../__snapshots__/AzureForm-test.tsx.snap | 6 +++++ .../almsettings/ws/UpdateAzureAction.java | 2 ++ .../almsettings/ws/UpdateBitbucketAction.java | 2 +- .../almsettings/ws/UpdateAzureActionTest.java | 23 +++++++++++++++++-- .../ws/UpdateBitbucketActionTest.java | 2 +- 6 files changed, 34 insertions(+), 4 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/settings/components/almIntegration/AzureForm.tsx b/server/sonar-web/src/main/js/apps/settings/components/almIntegration/AzureForm.tsx index 47c534d247b..a075c2f1505 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/almIntegration/AzureForm.tsx +++ b/server/sonar-web/src/main/js/apps/settings/components/almIntegration/AzureForm.tsx @@ -56,6 +56,9 @@ export default function AzureForm(props: AzureFormProps) { {translate('settings.almintegration.form.url.azure.help2')}
https://dev.azure.com/your_organization +
+
+ {translate('settings.almintegration.form.url.pat_warning')} } id="url.azure" diff --git a/server/sonar-web/src/main/js/apps/settings/components/almIntegration/__tests__/__snapshots__/AzureForm-test.tsx.snap b/server/sonar-web/src/main/js/apps/settings/components/almIntegration/__tests__/__snapshots__/AzureForm-test.tsx.snap index c2c39f83b8d..f966b6c8146 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/almIntegration/__tests__/__snapshots__/AzureForm-test.tsx.snap +++ b/server/sonar-web/src/main/js/apps/settings/components/almIntegration/__tests__/__snapshots__/AzureForm-test.tsx.snap @@ -26,6 +26,9 @@ exports[`should render correctly: create 1`] = ` https://dev.azure.com/your_organization +
+
+ settings.almintegration.form.url.pat_warning } id="url.azure" @@ -97,6 +100,9 @@ exports[`should render correctly: edit 1`] = ` https://dev.azure.com/your_organization +
+
+ settings.almintegration.form.url.pat_warning } id="url.azure" diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/UpdateAzureAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/UpdateAzureAction.java index fe3347676e1..29840f313ff 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/UpdateAzureAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/UpdateAzureAction.java @@ -95,6 +95,8 @@ public class UpdateAzureAction implements AlmSettingsWsAction { almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, newKey); } + almSettingsSupport.checkPatOnUrlUpdate(almSettingDto, url, pat); + if (isNotBlank(pat)) { almSettingDto.setPersonalAccessToken(pat); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/UpdateBitbucketAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/UpdateBitbucketAction.java index 24e17c187f0..777e8683216 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/UpdateBitbucketAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/UpdateBitbucketAction.java @@ -85,6 +85,7 @@ public class UpdateBitbucketAction implements AlmSettingsWsAction { private void doHandle(Request request) { String key = request.mandatoryParam(PARAM_KEY); String newKey = request.param(PARAM_NEW_KEY); + String url = request.mandatoryParam(PARAM_URL); String pat = request.param(PARAM_PERSONAL_ACCESS_TOKEN); try (DbSession dbSession = dbClient.openSession(false)) { @@ -93,7 +94,6 @@ public class UpdateBitbucketAction implements AlmSettingsWsAction { almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, newKey); } - String url = request.mandatoryParam(PARAM_URL); almSettingsSupport.checkPatOnUrlUpdate(almSettingDto, url, pat); if (isNotBlank(pat)) { diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/UpdateAzureActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/UpdateAzureActionTest.java index 97eeaa95441..b459fe54453 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/UpdateAzureActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/UpdateAzureActionTest.java @@ -31,6 +31,7 @@ import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.tester.UserSessionRule; +import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.WsActionTester; import static java.lang.String.format; @@ -90,7 +91,7 @@ public class UpdateAzureActionTest { } @Test - public void update_without_pat() { + public void update_with_url_change_needs_pat() { UserDto user = db.users().insertUser(); userSession.logIn(user).setSystemAdministrator(); @@ -99,10 +100,28 @@ public class UpdateAzureActionTest { ws.newRequest() .setParam("key", almSettingDto.getKey()) .setParam("url", AZURE_URL) + .setParam("personalAccessToken", "0123456789") .execute(); + assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession())) .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, s -> s.getDecryptedPersonalAccessToken(encryption)) - .containsOnly(tuple(almSettingDto.getKey(), AZURE_URL, almSettingDto.getDecryptedPersonalAccessToken(encryption))); + .containsOnly(tuple(almSettingDto.getKey(), AZURE_URL, "0123456789")); + } + + @Test + public void fail_when_update_url_without_pat() { + UserDto user = db.users().insertUser(); + userSession.logIn(user).setSystemAdministrator(); + + AlmSettingDto almSettingDto = db.almSettings().insertAzureAlmSetting(); + + TestRequest request = ws.newRequest() + .setParam("key", almSettingDto.getKey()) + .setParam("url", AZURE_URL); + + assertThatThrownBy(() -> request.execute()) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Please provide the Personal Access Token to update the URL."); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/UpdateBitbucketActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/UpdateBitbucketActionTest.java index 0a8162b36eb..40515f29d29 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/UpdateBitbucketActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/UpdateBitbucketActionTest.java @@ -105,7 +105,7 @@ public class UpdateBitbucketActionTest { } @Test - public void update_with_url_change_needs_path() { + public void update_with_url_change_needs_pat() { UserDto user = db.users().insertUser(); userSession.logIn(user).setSystemAdministrator(); -- 2.39.5