diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2019-08-30 02:32:36 -0500 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-09-24 20:21:16 +0200 |
commit | 80f844f576932e2d457ce5d0741f790e92fc50ba (patch) | |
tree | 88c988e28c9a1866fcec97d8edc5396f14851793 | |
parent | bc586fa19ba08951a910828222adcf923385facc (diff) | |
download | sonarqube-80f844f576932e2d457ce5d0741f790e92fc50ba.tar.gz sonarqube-80f844f576932e2d457ce5d0741f790e92fc50ba.zip |
SONAR-12366 Always set new code periods to main branch in CE (#2047)
4 files changed, 80 insertions, 14 deletions
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 4c99f809f97..80f8634cbaf 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 @@ -20,14 +20,12 @@ package org.sonar.server.newcodeperiod.ws; import com.google.common.base.Preconditions; -import java.util.EnumSet; -import java.util.Locale; -import java.util.Set; -import javax.annotation.Nullable; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.web.UserRole; +import org.sonar.core.platform.EditionProvider; +import org.sonar.core.platform.PlatformEditionProvider; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.BranchDto; @@ -42,6 +40,11 @@ import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.user.UserSession; +import javax.annotation.Nullable; +import java.util.EnumSet; +import java.util.Locale; +import java.util.Set; + import static com.google.common.base.Preconditions.checkArgument; import static java.lang.String.format; import static org.sonar.db.newcodeperiod.NewCodePeriodType.NUMBER_OF_DAYS; @@ -61,12 +64,14 @@ public class SetAction implements NewCodePeriodsWsAction { private final DbClient dbClient; private final UserSession userSession; private final ComponentFinder componentFinder; + private final PlatformEditionProvider editionProvider; private final NewCodePeriodDao newCodePeriodDao; - public SetAction(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder, NewCodePeriodDao newCodePeriodDao) { + public SetAction(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder, PlatformEditionProvider editionProvider, NewCodePeriodDao newCodePeriodDao) { this.dbClient = dbClient; this.userSession = userSession; this.componentFinder = componentFinder; + this.editionProvider = editionProvider; this.newCodePeriodDao = newCodePeriodDao; } @@ -115,7 +120,9 @@ public class SetAction implements NewCodePeriodsWsAction { if (projectStr != null) { projectBranch = getProject(dbSession, projectStr, branchStr); userSession.checkComponentPermission(UserRole.ADMIN, projectBranch); - if (branchStr != null) { + // in CE set main branch value instead of project value + boolean isCommunityEdition = editionProvider.get().filter(t -> t == EditionProvider.Edition.COMMUNITY).isPresent(); + if (branchStr != null || isCommunityEdition) { dto.setBranchUuid(projectBranch.uuid()); } // depending whether it's the main branch or not @@ -132,7 +139,7 @@ public class SetAction implements NewCodePeriodsWsAction { } private void setValue(DbSession dbSession, NewCodePeriodDto dto, NewCodePeriodType type, @Nullable ComponentDto projectBranch, - @Nullable String branch, @Nullable String value) { + @Nullable String branch, @Nullable String value) { switch (type) { case PREVIOUS_VERSION: Preconditions.checkArgument(value == null, "Unexpected value for type '%s'", type); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/UnsetAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/UnsetAction.java index 2ef7d0ca728..0eb2f2ddef0 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/UnsetAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/UnsetAction.java @@ -19,11 +19,12 @@ */ package org.sonar.server.newcodeperiod.ws; -import javax.annotation.Nullable; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.web.UserRole; +import org.sonar.core.platform.EditionProvider; +import org.sonar.core.platform.PlatformEditionProvider; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.BranchDto; @@ -34,6 +35,8 @@ import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.user.UserSession; +import javax.annotation.Nullable; + import static com.google.common.base.Preconditions.checkArgument; import static java.lang.String.format; import static org.sonar.server.component.ComponentFinder.ParamNames.PROJECT_ID_AND_KEY; @@ -45,12 +48,14 @@ public class UnsetAction implements NewCodePeriodsWsAction { private final DbClient dbClient; private final UserSession userSession; private final ComponentFinder componentFinder; + private final PlatformEditionProvider editionProvider; private final NewCodePeriodDao newCodePeriodDao; - public UnsetAction(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder, NewCodePeriodDao newCodePeriodDao) { + public UnsetAction(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder, PlatformEditionProvider editionProvider, NewCodePeriodDao newCodePeriodDao) { this.dbClient = dbClient; this.userSession = userSession; this.componentFinder = componentFinder; + this.editionProvider = editionProvider; this.newCodePeriodDao = newCodePeriodDao; } @@ -87,10 +92,14 @@ public class UnsetAction implements NewCodePeriodsWsAction { String branchUuid = null; ComponentDto projectBranch = null; + // in CE set main branch value instead of project value + boolean isCommunityEdition = editionProvider.get().filter(t -> t == EditionProvider.Edition.COMMUNITY).isPresent(); + if (projectStr != null) { projectBranch = getProject(dbSession, projectStr, branchStr); userSession.checkComponentPermission(UserRole.ADMIN, projectBranch); - if (branchStr != null) { + + if (branchStr != null || isCommunityEdition) { branchUuid = projectBranch.uuid(); } // depending whether it's the main branch or not @@ -100,6 +109,11 @@ public class UnsetAction implements NewCodePeriodsWsAction { } newCodePeriodDao.delete(dbSession, projectUuid, branchUuid); + if (isCommunityEdition && projectUuid != null) { + // also delete project default in case it was somehow set (downgrade from another edition, for example) + newCodePeriodDao.delete(dbSession, projectUuid, null); + } + dbSession.commit(); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/SetActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/SetActionTest.java index e18763e72eb..be5fe9fffe6 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/SetActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/SetActionTest.java @@ -19,13 +19,14 @@ */ package org.sonar.server.newcodeperiod.ws; -import javax.annotation.Nullable; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; +import org.sonar.core.platform.EditionProvider; +import org.sonar.core.platform.PlatformEditionProvider; import org.sonar.core.util.UuidFactoryFast; import org.sonar.db.DbClient; import org.sonar.db.DbSession; @@ -43,8 +44,13 @@ import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsActionTester; +import javax.annotation.Nullable; +import java.util.Optional; + import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class SetActionTest { @Rule @@ -58,9 +64,10 @@ public class SetActionTest { private DbClient dbClient = db.getDbClient(); private DbSession dbSession = db.getSession(); private ComponentFinder componentFinder = TestComponentFinder.from(db); + private PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class); private NewCodePeriodDao dao = new NewCodePeriodDao(System2.INSTANCE, UuidFactoryFast.getInstance()); - private SetAction underTest = new SetAction(dbClient, userSession, componentFinder, dao); + private SetAction underTest = new SetAction(dbClient, userSession, componentFinder, editionProvider, dao); private WsActionTester ws = new WsActionTester(underTest); @Test @@ -299,6 +306,19 @@ public class SetActionTest { } @Test + public void never_set_project_value_in_community_edition() { + when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY)); + ComponentDto project = componentDb.insertMainBranch(); + logInAsProjectAdministrator(project); + ws.newRequest() + .setParam("project", project.getKey()) + .setParam("type", "number_of_days") + .setParam("value", "5") + .execute(); + assertTableContainsOnly(project.uuid(), project.uuid(), NewCodePeriodType.NUMBER_OF_DAYS, "5"); + } + + @Test public void set_project_twice_period_to_number_of_days() { ComponentDto project = componentDb.insertMainBranch(); logInAsProjectAdministrator(project); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/UnsetActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/UnsetActionTest.java index 7475a38d2a5..97214f615eb 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/UnsetActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/UnsetActionTest.java @@ -19,13 +19,14 @@ */ package org.sonar.server.newcodeperiod.ws; -import javax.annotation.Nullable; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; +import org.sonar.core.platform.EditionProvider; +import org.sonar.core.platform.PlatformEditionProvider; import org.sonar.core.util.UuidFactoryFast; import org.sonar.db.DbClient; import org.sonar.db.DbSession; @@ -42,8 +43,13 @@ import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsActionTester; +import javax.annotation.Nullable; +import java.util.Optional; + import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class UnsetActionTest { @Rule @@ -58,8 +64,9 @@ public class UnsetActionTest { private DbSession dbSession = db.getSession(); private ComponentFinder componentFinder = TestComponentFinder.from(db); private NewCodePeriodDao dao = new NewCodePeriodDao(System2.INSTANCE, UuidFactoryFast.getInstance()); + private PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class); - private UnsetAction underTest = new UnsetAction(dbClient, userSession, componentFinder, dao); + private UnsetAction underTest = new UnsetAction(dbClient, userSession, componentFinder, editionProvider, dao); private WsActionTester ws = new WsActionTester(underTest); @Test @@ -209,6 +216,24 @@ public class UnsetActionTest { assertTableContainsOnly(project.uuid(), null, NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid1"); } + @Test + public void delete_branch_and_project_period_in_community_edition() { + ComponentDto project = componentDb.insertMainBranch(); + + db.newCodePeriods().insert(project.uuid(), null, NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid1"); + db.newCodePeriods().insert(project.uuid(), project.uuid(), NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid2"); + + when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY)); + + logInAsProjectAdministrator(project); + + ws.newRequest() + .setParam("project", project.getKey()) + .execute(); + + assertTableEmpty(); + } + private void assertTableEmpty() { assertThat(db.countRowsOfTable(dbSession, "new_code_periods")).isEqualTo(0); } |