From 21b74a19c2141344495b1074ef575921b1b9ecaf Mon Sep 17 00:00:00 2001 From: Benjamin Campomenosi Date: Tue, 17 Oct 2023 15:59:01 +0200 Subject: [PATCH] SONAR-20531 fix exporting project With AdHod Rules when pluginKey is null --- .../rule/ExportAdHocRulesStepIT.java | 40 ++++++++++++++++++- .../rule/ExportAdHocRulesStep.java | 19 ++++----- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStepIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStepIT.java index feca4587a1a..2f99341ef25 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStepIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStepIT.java @@ -181,6 +181,45 @@ public class ExportAdHocRulesStepIT { assertThat(underTest.getDescription()).isEqualTo("Export ad-hoc rules"); } + @Test + public void execute_shouldMapFieldWithEmptyString_whenNameOrPluginKeyAreNull() { + RuleKey ruleKey = RuleKey.of("plugin1", "partiallyInit"); + RuleDto partiallyInitRuleDto = insertAdHocRuleWithoutNameAndPluginKeyAndAdHocInformations(ruleKey); + insertIssue(partiallyInitRuleDto, mainBranch.uuid(), mainBranch.uuid()); + + underTest.execute(new TestComputationStepContext()); + + List exportedRules = dumpWriter.getWrittenMessagesOf(DumpElement.AD_HOC_RULES); + assertThat(exportedRules).hasSize(1); + ProjectDump.AdHocRule adHocRule = exportedRules.iterator().next(); + assertThat(adHocRule.getName()).isEmpty(); + assertThat(adHocRule.getPluginKey()).isEmpty(); + ProjectDump.AdHocRule.RuleMetadata adHocRuleMetadata = adHocRule.getMetadata(); + assertThat(adHocRuleMetadata.getAdHocDescription()).isEmpty(); + assertThat(adHocRuleMetadata.getAdHocName()).isEmpty(); + assertThat(adHocRuleMetadata.getAdHocSeverity()).isEmpty(); + assertThat(adHocRuleMetadata.getAdHocType()).isZero(); + + } + + private RuleDto insertAdHocRuleWithoutNameAndPluginKeyAndAdHocInformations(RuleKey ruleKey) { + RuleDto partiallyInitRuleDto = new RuleDto() + .setIsExternal(false) + .setIsAdHoc(true) + .setCleanCodeAttribute(CleanCodeAttribute.CONVENTIONAL) + .addDefaultImpact(new ImpactDto().setUuid(Uuids.createFast()).setSoftwareQuality(SoftwareQuality.MAINTAINABILITY).setSeverity(org.sonar.api.issue.impact.Severity.MEDIUM)) + .addDefaultImpact(new ImpactDto().setUuid(Uuids.createFast()).setSoftwareQuality(SoftwareQuality.RELIABILITY).setSeverity(org.sonar.api.issue.impact.Severity.HIGH)) + .setRuleKey(ruleKey) + .setScope(RuleDto.Scope.ALL) + .setStatus(RuleStatus.READY); + + dbTester.rules().insert(partiallyInitRuleDto); + dbTester.commit(); + + return dbTester.getDbClient().ruleDao().selectByKey(dbTester.getSession(), ruleKey) + .orElseThrow(() -> new RuntimeException("insertAdHocRule failed")); + } + private ProjectDto createProject() { Date createdAt = new Date(); ProjectData projectData = dbTester.components().insertPublicProject(PROJECT_UUID); @@ -278,7 +317,6 @@ public class ExportAdHocRulesStepIT { i -> org.sonar.api.issue.impact.Severity.valueOf(i.getSeverity().name()))); } - private static void assertProtobufAdHocRuleIsCorrectlyBuilt(ProjectDump.AdHocRule.RuleMetadata metadata, RuleDto expected) { assertThat(metadata.getAdHocName()).isEqualTo(expected.getAdHocName()); assertThat(metadata.getAdHocDescription()).isEqualTo(expected.getAdHocDescription()); diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStep.java index cae6a5a94b0..a20a5e5b8cd 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStep.java @@ -22,6 +22,7 @@ package org.sonar.ce.task.projectexport.rule; import com.hazelcast.internal.util.MutableLong; import com.sonarsource.governance.projectdump.protobuf.ProjectDump; import java.util.List; +import java.util.Optional; import java.util.Set; import org.apache.ibatis.cursor.Cursor; import org.slf4j.LoggerFactory; @@ -57,8 +58,7 @@ public class ExportAdHocRulesStep implements ComputationStep { try ( StreamWriter output = dumpWriter.newStreamWriter(DumpElement.AD_HOC_RULES); DbSession dbSession = dbClient.openSession(false); - Cursor ruleDtoCursor = dbSession.getMapper(ProjectExportMapper.class).scrollAdhocRulesForExport(projectHolder.projectDto().getUuid()) - ) { + Cursor ruleDtoCursor = dbSession.getMapper(ProjectExportMapper.class).scrollAdhocRulesForExport(projectHolder.projectDto().getUuid())) { ProjectDump.AdHocRule.Builder adHocRuleBuilder = ProjectDump.AdHocRule.newBuilder(); ruleDtoCursor .forEach(ruleDto -> { @@ -78,11 +78,11 @@ public class ExportAdHocRulesStep implements ComputationStep { return builder .clear() .setRef(ruleDto.getUuid()) - .setPluginKey(ruleDto.getPluginKey()) + .setPluginKey(Optional.of(ruleDto).map(RuleDto::getPluginKey).orElse("")) .setPluginRuleKey(ruleDto.getKey().rule()) .setPluginName(ruleDto.getRepositoryKey()) - .setName(ruleDto.getName()) - .setStatus(ruleDto.getStatus().name()) + .setName(Optional.of(ruleDto).map(RuleDto::getName).orElse("")) + .setStatus(Optional.of(ruleDto).map(RuleDto::getStatus).map(Enum::name).orElse("")) .setType(ruleDto.getType()) .setScope(ruleDto.getScope().name()) .setMetadata(buildMetadata(ruleDto)) @@ -101,11 +101,12 @@ public class ExportAdHocRulesStep implements ComputationStep { } private static ProjectDump.AdHocRule.RuleMetadata buildMetadata(RuleDto ruleDto) { + Optional rule = Optional.of(ruleDto); return ProjectDump.AdHocRule.RuleMetadata.newBuilder() - .setAdHocName(ruleDto.getAdHocName()) - .setAdHocDescription(ruleDto.getAdHocDescription()) - .setAdHocSeverity(ruleDto.getAdHocSeverity()) - .setAdHocType(ruleDto.getAdHocType()) + .setAdHocName(rule.map(RuleDto::getAdHocName).orElse("")) + .setAdHocDescription(rule.map(RuleDto::getAdHocDescription).orElse("")) + .setAdHocSeverity(rule.map(RuleDto::getAdHocSeverity).orElse("")) + .setAdHocType(rule.map(RuleDto::getAdHocType).orElse(0)) .build(); } -- 2.39.5