diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2018-03-06 11:39:36 +0100 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2018-03-13 09:11:10 +0100 |
commit | 337a26aea27d440e56e1a34ef660be0edadd629c (patch) | |
tree | 15f8497e2d46772a3d87af469d71a555b6825a49 /server/sonar-server | |
parent | 9955fbbea60109c203cde83c110ffd4524a33422 (diff) | |
download | sonarqube-337a26aea27d440e56e1a34ef660be0edadd629c.tar.gz sonarqube-337a26aea27d440e56e1a34ef660be0edadd629c.zip |
SONAR-10461 load branch even if project key in report is wrong
Diffstat (limited to 'server/sonar-server')
2 files changed, 49 insertions, 27 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java index 52d459ee2a5..a394db74c4a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java @@ -88,9 +88,10 @@ public class LoadReportAnalysisMetadataHolderStep implements ComputationStep { loadMetadata(reportMetadata); Organization organization = loadOrganization(reportMetadata); - loadProject(reportMetadata, organization); + Runnable projectValidation = loadProject(reportMetadata, organization); loadQualityProfiles(reportMetadata, organization); branchLoader.load(reportMetadata); + projectValidation.run(); } private void loadMetadata(ScannerReport.Metadata reportMetadata) { @@ -99,17 +100,33 @@ public class LoadReportAnalysisMetadataHolderStep implements ComputationStep { analysisMetadata.setCrossProjectDuplicationEnabled(reportMetadata.getCrossProjectDuplicationActivated()); } - private void loadProject(ScannerReport.Metadata reportMetadata, Organization organization) { + /** + * @return a {@link Runnable} to execute some checks on the project at the end of the step + */ + private Runnable loadProject(ScannerReport.Metadata reportMetadata, Organization organization) { String reportProjectKey = projectKeyFromReport(reportMetadata); - checkProjectKeyConsistency(reportProjectKey); - ComponentDto dto = toProject(reportProjectKey); - if (!dto.getOrganizationUuid().equals(organization.getUuid())) { - throw MessageException.of(format("Project is not in the expected organization: %s", organization.getKey())); - } - if (dto.getMainBranchProjectUuid() != null) { - throw MessageException.of("Project should not reference a branch"); + String componentKey = ceTask.getComponentKey(); + if (componentKey == null) { + throw MessageException.of(format( + "Compute Engine task component key is null. Project with UUID %s must have been deleted since report was uploaded. Can not proceed.", + ceTask.getComponentUuid())); } + ComponentDto dto = toProject(reportProjectKey); analysisMetadata.setProject(new Project(dto.uuid(), dto.getDbKey(), dto.name())); + return () -> { + if (!componentKey.equals(reportProjectKey)) { + throw MessageException.of(format( + "ProjectKey in report (%s) is not consistent with projectKey under which the report has been submitted (%s)", + reportProjectKey, + componentKey)); + } + if (!dto.getOrganizationUuid().equals(organization.getUuid())) { + throw MessageException.of(format("Project is not in the expected organization: %s", organization.getKey())); + } + if (dto.getMainBranchProjectUuid() != null) { + throw MessageException.of("Project should not reference a branch"); + } + }; } private Organization loadOrganization(ScannerReport.Metadata reportMetadata) { @@ -164,21 +181,6 @@ public class LoadReportAnalysisMetadataHolderStep implements ComputationStep { } } - private void checkProjectKeyConsistency(String reportProjectKey) { - String componentKey = ceTask.getComponentKey(); - if (componentKey == null) { - throw MessageException.of(format( - "Compute Engine task component key is null. Project with UUID %s must have been deleted since report was uploaded. Can not proceed.", - ceTask.getComponentUuid())); - } - if (!componentKey.equals(reportProjectKey)) { - throw MessageException.of(format( - "ProjectKey in report (%s) is not consistent with projectKey under which the report as been submitted (%s)", - reportProjectKey, - componentKey)); - } - } - private void checkOrganizationKeyConsistency(ScannerReport.Metadata reportMetadata, Organization organization) { String organizationKey = reportMetadata.getOrganizationKey(); String resolveReportOrganizationKey = resolveReportOrganizationKey(organizationKey); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStepTest.java index 77fbfe6a9d4..d6f575495c5 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStepTest.java @@ -157,22 +157,42 @@ public class LoadReportAnalysisMetadataHolderStepTest { @Test public void execute_fails_with_MessageException_when_projectKey_in_report_is_different_from_componentKey_in_CE_task() { + OrganizationDto organization = db.organizations().insert(); + ComponentDto otherProject = db.components().insertPublicProject(organization); reportReader.setMetadata( ScannerReport.Metadata.newBuilder() - .setProjectKey("some other key") + .setProjectKey(otherProject.getDbKey()) .build()); expectedException.expect(MessageException.class); - expectedException.expectMessage("ProjectKey in report (some other key) is not consistent with projectKey under which the report as been submitted (" + PROJECT_KEY + ")"); + expectedException.expectMessage("ProjectKey in report (" + otherProject.getDbKey() + ") is not consistent with projectKey under which the report has been submitted (" + PROJECT_KEY + ")"); underTest.execute(); } @Test + public void execute_sets_branch_even_if_MessageException_is_thrown_because_projectKey_in_report_is_different_from_componentKey_in_CE_task() { + OrganizationDto organization = db.organizations().insert(); + ComponentDto otherProject = db.components().insertPublicProject(organization); + reportReader.setMetadata( + ScannerReport.Metadata.newBuilder() + .setProjectKey(otherProject.getDbKey()) + .build()); + + try { + underTest.execute(); + } catch (MessageException e) { + assertThat(analysisMetadataHolder.getBranch()).isNotNull(); + } + } + + @Test public void execute_sets_analysis_date_even_if_MessageException_is_thrown_because_projectKey_is_different_from_componentKey_in_CE_task() { + OrganizationDto organization = db.organizations().insert(); + ComponentDto otherProject = db.components().insertPublicProject(organization); reportReader.setMetadata( ScannerReport.Metadata.newBuilder() - .setProjectKey("some other key") + .setProjectKey(otherProject.getDbKey()) .setAnalysisDate(ANALYSIS_DATE) .build()); |