aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-webserver-webapi
diff options
context:
space:
mode:
authorZipeng WU <zipeng.wu@sonarsource.com>2023-05-17 17:51:43 +0200
committersonartech <sonartech@sonarsource.com>2023-05-23 20:03:09 +0000
commita4fa68f5b7ebeb15eb012679c6660d4fe57a82d1 (patch)
tree06c2c1834136cc4d61a65141bac28c4d2fc0a4dc /server/sonar-webserver-webapi
parent01363ea39feba1f5bfec0d57ef7ee24d3f3a6d82 (diff)
downloadsonarqube-a4fa68f5b7ebeb15eb012679c6660d4fe57a82d1.tar.gz
sonarqube-a4fa68f5b7ebeb15eb012679c6660d4fe57a82d1.zip
SONAR-19294 fail call to api/new_code_period/set when non-compliant option is used
Diffstat (limited to 'server/sonar-webserver-webapi')
-rw-r--r--server/sonar-webserver-webapi/src/it/java/org/sonar/server/newcodeperiod/ws/SetActionIT.java35
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java9
2 files changed, 41 insertions, 3 deletions
diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/newcodeperiod/ws/SetActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/newcodeperiod/ws/SetActionIT.java
index ce442e615ef..aadb154a276 100644
--- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/newcodeperiod/ws/SetActionIT.java
+++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/newcodeperiod/ws/SetActionIT.java
@@ -76,7 +76,7 @@ public class SetActionIT {
private WsActionTester ws;
@Before
- public void setup(){
+ public void setup() {
when(documentationLinkGenerator.getDocumentationLink(any())).thenReturn("https://docs.sonarqube.org/9.9/project-administration/defining-new-code/");
ws = new WsActionTester(new SetAction(dbClient, userSession, componentFinder, editionProvider, dao, documentationLinkGenerator));
}
@@ -178,6 +178,37 @@ public class SetActionIT {
}
@Test
+ public void throw_IAE_if_setting_is_not_cayc_compliant() {
+ ComponentDto project = componentDb.insertPublicProject().getMainBranchComponent();
+ logInAsProjectAdministrator(project);
+
+ TestRequest request = ws.newRequest()
+ .setParam("project", project.getKey())
+ .setParam("type", "number_of_days")
+ .setParam("branch", DEFAULT_MAIN_BRANCH_NAME)
+ .setParam("value", "92");
+ assertThatThrownBy(() -> request
+ .execute())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("Failed to set the New Code Definition. The given value is not compatible with the Clean as You Code methodology. "
+ + "Please refer to the documentation for compliant options.");
+ }
+
+ @Test
+ public void no_error_if_setting_is_cayc_compliant() {
+ ComponentDto project = componentDb.insertPublicProject().getMainBranchComponent();
+ logInAsProjectAdministrator(project);
+
+ ws.newRequest()
+ .setParam("project", project.getKey())
+ .setParam("type", "number_of_days")
+ .setParam("value", "90")
+ .execute();
+
+ assertTableContainsOnly(project.uuid(), null, NewCodePeriodType.NUMBER_OF_DAYS, "90");
+ }
+
+ @Test
public void throw_IAE_if_analysis_is_not_found() {
ComponentDto project = componentDb.insertPublicProject().getMainBranchComponent();
logInAsProjectAdministrator(project);
@@ -317,7 +348,7 @@ public class SetActionIT {
@DataProvider
public static Object[][] provideNewCodePeriodTypeAndValue() {
- return new Object[][]{
+ return new Object[][] {
{NewCodePeriodType.NUMBER_OF_DAYS, "5"},
{NewCodePeriodType.SPECIFIC_ANALYSIS, "analysis-uuid"},
{NewCodePeriodType.PREVIOUS_VERSION, null},
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java
index 317b09c3ffc..c3a29aecd35 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java
@@ -42,6 +42,7 @@ import org.sonar.db.newcodeperiod.NewCodePeriodType;
import org.sonar.db.project.ProjectDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.newcodeperiod.CaycUtils;
import org.sonar.server.user.UserSession;
import static com.google.common.base.Preconditions.checkArgument;
@@ -94,6 +95,7 @@ public class SetAction implements NewCodePeriodsWsAction {
"Existing projects or branches having a specific new code definition will not be impacted" + END_ITEM_LIST +
BEGIN_ITEM_LIST + "Project key must be provided to update the value for a project" + END_ITEM_LIST +
BEGIN_ITEM_LIST + "Both project and branch keys must be provided to update the value for a branch" + END_ITEM_LIST +
+ BEGIN_ITEM_LIST + "New setting must be compliant with the Clean as You Code methodology" + END_ITEM_LIST +
END_LIST +
"Requires one of the following permissions: " +
BEGIN_LIST +
@@ -124,7 +126,7 @@ public class SetAction implements NewCodePeriodsWsAction {
BEGIN_LIST +
BEGIN_ITEM_LIST + "the uuid of an analysis, when type is " + SPECIFIC_ANALYSIS.name() + END_ITEM_LIST +
BEGIN_ITEM_LIST + "no value, when type is " + PREVIOUS_VERSION.name() + END_ITEM_LIST +
- BEGIN_ITEM_LIST + "a number, when type is " + NUMBER_OF_DAYS.name() + END_ITEM_LIST +
+ BEGIN_ITEM_LIST + "a number between 1 and 90, when type is " + NUMBER_OF_DAYS.name() + END_ITEM_LIST +
BEGIN_ITEM_LIST + "a string, when type is " + REFERENCE_BRANCH.name() + END_ITEM_LIST +
END_LIST
);
@@ -172,6 +174,11 @@ public class SetAction implements NewCodePeriodsWsAction {
setValue(dbSession, dto, type, project, branch, valueStr);
+ if (!CaycUtils.isNewCodePeriodCompliant(dto.getType(), dto.getValue())) {
+ throw new IllegalArgumentException("Failed to set the New Code Definition. The given value is not compatible with the Clean as You Code methodology. "
+ + "Please refer to the documentation for compliant options.");
+ }
+
newCodePeriodDao.upsert(dbSession, dto);
dbSession.commit();
}