diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-12-27 15:50:23 +0100 |
---|---|---|
committer | Janos Gyerik <janos.gyerik@sonarsource.com> | 2018-01-05 15:49:51 +0100 |
commit | 902fabcf137d0ad6775fde22b2771e31f3da90fb (patch) | |
tree | bd156b18082b9523fcbb6b3a8e50307468e473ed /server | |
parent | b2aeb17fd953c1c7f8408989a439813f6ecd5f40 (diff) | |
download | sonarqube-902fabcf137d0ad6775fde22b2771e31f3da90fb.tar.gz sonarqube-902fabcf137d0ad6775fde22b2771e31f3da90fb.zip |
SONAR-10223 add optional organization to PostProjectAnalysisTask
Diffstat (limited to 'server')
4 files changed, 85 insertions, 7 deletions
diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java index 30f22461dd9..5387324ec12 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java @@ -109,6 +109,7 @@ import org.sonar.server.notification.email.AlertsEmailTemplate; import org.sonar.server.notification.email.EmailNotificationChannel; import org.sonar.server.organization.BillingValidationsProxyImpl; import org.sonar.server.organization.DefaultOrganizationProviderImpl; +import org.sonar.server.organization.OrganizationFlagsImpl; import org.sonar.server.permission.GroupPermissionChanger; import org.sonar.server.permission.PermissionTemplateService; import org.sonar.server.permission.PermissionUpdater; @@ -312,7 +313,8 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { UriReader.class, ServerImpl.class, DefaultOrganizationProviderImpl.class, - SynchronousAsyncExecution.class); + SynchronousAsyncExecution.class, + OrganizationFlagsImpl.class); } private static void populateLevel4(ComponentContainer container, Props props) { diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java index 950cbde71b7..76da867464e 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java @@ -65,7 +65,7 @@ public class ComputeEngineContainerImplTest { private ComputeEngineContainerImpl underTest; @Before - public void setUp() throws Exception { + public void setUp() { underTest = new ComputeEngineContainerImpl(); underTest.setComputeEngineStatus(mock(ComputeEngineStatus.class)); } @@ -105,7 +105,7 @@ public class ComputeEngineContainerImplTest { ); assertThat(picoContainer.getParent().getComponentAdapters()).hasSize( CONTAINER_ITSELF - + 6 // level 3 + + 7 // level 3 ); assertThat(picoContainer.getParent().getParent().getComponentAdapters()).hasSize( CONTAINER_ITSELF diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java index 6f016550ace..0bbbae93173 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java @@ -29,6 +29,7 @@ import javax.annotation.Nullable; import org.sonar.api.ce.posttask.Analysis; import org.sonar.api.ce.posttask.Branch; import org.sonar.api.ce.posttask.CeTask; +import org.sonar.api.ce.posttask.Organization; import org.sonar.api.ce.posttask.PostProjectAnalysisTask; import org.sonar.api.ce.posttask.Project; import org.sonar.api.ce.posttask.QualityGate; @@ -116,6 +117,7 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor private ProjectAnalysisImpl createProjectAnalysis(CeTask.Status status) { return new ProjectAnalysisImpl( + createOrganization(), new CeTaskImpl(this.ceTask.getUuid(), status), createProject(this.ceTask), getAnalysis().orElse(null), @@ -125,6 +127,15 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor createBranch()); } + @CheckForNull + private Organization createOrganization() { + org.sonar.server.computation.task.projectanalysis.analysis.Organization organization = analysisMetadataHolder.getOrganization(); + if (organization.isOrganizationsEnabled()) { + return new OrganizationImpl(organization.getName(), organization.getKey()); + } + return null; + } + private Optional<Analysis> getAnalysis() { Long analysisDate = getAnalysisDate(); @@ -196,6 +207,8 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor } private static class ProjectAnalysisImpl implements PostProjectAnalysisTask.ProjectAnalysis { + @Nullable + private final Organization organization; private final CeTask ceTask; private final Project project; private final long date; @@ -207,9 +220,10 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor @Nullable private final Analysis analysis; - private ProjectAnalysisImpl(CeTask ceTask, Project project, + private ProjectAnalysisImpl(@Nullable Organization organization, CeTask ceTask, Project project, @Nullable Analysis analysis, long date, ScannerContext scannerContext, @Nullable QualityGate qualityGate, @Nullable Branch branch) { + this.organization = organization; this.ceTask = requireNonNull(ceTask, "ceTask can not be null"); this.project = requireNonNull(project, "project can not be null"); this.analysis = analysis; @@ -220,6 +234,11 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor } @Override + public Optional<Organization> getOrganization() { + return Optional.ofNullable(organization); + } + + @Override public CeTask getCeTask() { return ceTask; } @@ -293,4 +312,24 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor return new Date(date); } } + + private static class OrganizationImpl implements Organization { + private final String name; + private final String key; + + private OrganizationImpl(String name, String key) { + this.name = requireNonNull(name, "name can't be null"); + this.key = requireNonNull(key, "key can't be null"); + } + + @Override + public String getName() { + return name; + } + + @Override + public String getKey() { + return key; + } + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java index 484180fabb2..6f7ca22c791 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java @@ -26,6 +26,7 @@ import com.tngtech.java.junit.dataprovider.UseDataProvider; import java.util.Date; import java.util.List; import java.util.Optional; +import java.util.Random; import javax.annotation.Nullable; import org.apache.commons.lang.RandomStringUtils; import org.junit.Before; @@ -39,9 +40,11 @@ import org.sonar.api.ce.posttask.Project; import org.sonar.api.utils.System2; import org.sonar.ce.queue.CeTask; import org.sonar.db.component.BranchType; +import org.sonar.db.organization.OrganizationDto; import org.sonar.scanner.protocol.output.ScannerReport; import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule; import org.sonar.server.computation.task.projectanalysis.analysis.Branch; +import org.sonar.server.computation.task.projectanalysis.analysis.Organization; import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReaderRule; import org.sonar.server.computation.task.projectanalysis.component.DefaultBranchImpl; import org.sonar.server.computation.task.projectanalysis.metric.Metric; @@ -79,10 +82,13 @@ public class PostProjectAnalysisTasksExecutorTest { @Rule public BatchReportReaderRule reportReader = new BatchReportReaderRule(); + private String organizationUuid = "org1"; + private String organizationKey = organizationUuid + "_key"; + private String organizationName = organizationUuid + "_name"; private System2 system2 = mock(System2.class); private ArgumentCaptor<PostProjectAnalysisTask.ProjectAnalysis> projectAnalysisArgumentCaptor = ArgumentCaptor.forClass(PostProjectAnalysisTask.ProjectAnalysis.class); private CeTask ceTask = new CeTask.Builder() - .setOrganizationUuid("org1") + .setOrganizationUuid(organizationUuid) .setType("type") .setUuid("uuid") .setComponentKey("component key") @@ -104,6 +110,9 @@ public class PostProjectAnalysisTasksExecutorTest { Branch branch = mock(Branch.class); when(branch.getType()).thenReturn(BranchType.LONG); analysisMetadataHolder.setBranch(branch); + analysisMetadataHolder.setOrganization(Organization.from( + new OrganizationDto().setKey(organizationKey).setName(organizationName).setUuid(organizationUuid).setDefaultQualityGateUuid("foo"), + new Random().nextBoolean())); } @Test @@ -123,7 +132,7 @@ public class PostProjectAnalysisTasksExecutorTest { new PostProjectAnalysisTasksExecutor( ceTask, analysisMetadataHolder, qualityGateHolder, qualityGateStatusHolder, reportReader, system2, new PostProjectAnalysisTask[] {postProjectAnalysisTask1, postProjectAnalysisTask2}) - .finished(allStepsExecuted); + .finished(allStepsExecuted); inOrder.verify(postProjectAnalysisTask1).finished(projectAnalysisArgumentCaptor.capture()); inOrder.verify(postProjectAnalysisTask2).finished(projectAnalysisArgumentCaptor.capture()); @@ -136,6 +145,34 @@ public class PostProjectAnalysisTasksExecutorTest { @Test @UseDataProvider("booleanValues") + public void organization_is_null_when_organization_are_disabled(boolean allStepsExecuted) { + analysisMetadataHolder.setOrganization(Organization.from( + new OrganizationDto().setKey(organizationKey).setName(organizationName).setUuid(organizationUuid).setDefaultQualityGateUuid("foo"), + false)); + underTest.finished(allStepsExecuted); + + verify(postProjectAnalysisTask).finished(projectAnalysisArgumentCaptor.capture()); + + assertThat(projectAnalysisArgumentCaptor.getValue().getOrganization()).isEmpty(); + } + + @Test + @UseDataProvider("booleanValues") + public void organization_is_not_null_when_organization_are_enabled(boolean allStepsExecuted) { + analysisMetadataHolder.setOrganization(Organization.from( + new OrganizationDto().setKey(organizationKey).setName(organizationName).setUuid(organizationUuid).setDefaultQualityGateUuid("foo"), + true)); + underTest.finished(allStepsExecuted); + + verify(postProjectAnalysisTask).finished(projectAnalysisArgumentCaptor.capture()); + + org.sonar.api.ce.posttask.Organization organization = projectAnalysisArgumentCaptor.getValue().getOrganization().get(); + assertThat(organization.getKey()).isEqualTo(organizationKey); + assertThat(organization.getName()).isEqualTo(organizationName); + } + + @Test + @UseDataProvider("booleanValues") public void CeTask_status_depends_on_finished_method_argument_is_true_or_false(boolean allStepsExecuted) { underTest.finished(allStepsExecuted); @@ -325,7 +362,7 @@ public class PostProjectAnalysisTasksExecutorTest { new PostProjectAnalysisTasksExecutor( ceTask, analysisMetadataHolder, qualityGateHolder, qualityGateStatusHolder, reportReader, system2, new PostProjectAnalysisTask[] {postProjectAnalysisTask1, postProjectAnalysisTask2, postProjectAnalysisTask3}) - .finished(allStepsExecuted); + .finished(allStepsExecuted); inOrder.verify(postProjectAnalysisTask1).finished(projectAnalysisArgumentCaptor.capture()); inOrder.verify(postProjectAnalysisTask2).finished(projectAnalysisArgumentCaptor.capture()); |