]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20531 fix exporting project With AdHod Rules when pluginKey is null
authorBenjamin Campomenosi <benjamin.campomenosi@sonarsource.com>
Tue, 17 Oct 2023 13:59:01 +0000 (15:59 +0200)
committersonartech <sonartech@sonarsource.com>
Mon, 23 Oct 2023 20:02:42 +0000 (20:02 +0000)
server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStepIT.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStep.java

index feca4587a1aeb84e433c6432994885821ff8f79c..2f99341ef25f9f51d6e30403c32723d440236e36 100644 (file)
@@ -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<ProjectDump.AdHocRule> 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());
index cae6a5a94b041dcabff1a45636d136ad1483fc12..a20a5e5b8cd0072432b743bcd6551609efdc3858 100644 (file)
@@ -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<ProjectDump.AdHocRule> output = dumpWriter.newStreamWriter(DumpElement.AD_HOC_RULES);
       DbSession dbSession = dbClient.openSession(false);
-      Cursor<RuleDto> ruleDtoCursor = dbSession.getMapper(ProjectExportMapper.class).scrollAdhocRulesForExport(projectHolder.projectDto().getUuid())
-    ) {
+      Cursor<RuleDto> 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<RuleDto> 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();
   }