From 97a815175c48fbd63bebb607ff4a4a6d12e5164c Mon Sep 17 00:00:00 2001 From: Michal Duda <44705056+michal-duda-sonarsource@users.noreply.github.com> Date: Thu, 27 Jun 2019 13:57:22 +0200 Subject: [PATCH] Merge pull request #1789 from SonarSource/fix/md/SONAR-11845-project-import-should-handle-external-issues SONAR-11845 project import should handle external issues --- .../task/projectanalysis/issue/AdHocRuleCreator.java | 2 +- .../src/main/java/org/sonar/db/rule/RuleDao.java | 11 +++++------ .../src/test/java/org/sonar/db/rule/RuleDaoTest.java | 12 ++++++------ .../org/sonar/server/rule/DefaultRuleFinder.java | 2 +- .../main/java/org/sonar/server/rule/RuleCreator.java | 2 +- .../java/org/sonar/server/rule/ws/ShowAction.java | 2 +- .../java/org/sonar/server/rule/ws/UpdateAction.java | 4 ++-- .../org/sonar/server/rule/RegisterRulesTest.java | 6 +++--- .../org/sonar/server/rule/ws/UpdateActionTest.java | 6 +++--- 9 files changed, 23 insertions(+), 24 deletions(-) diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/AdHocRuleCreator.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/AdHocRuleCreator.java index 3a3532e3b1c..f0c43494d83 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/AdHocRuleCreator.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/AdHocRuleCreator.java @@ -58,7 +58,7 @@ public class AdHocRuleCreator { */ public RuleDto persistAndIndex(DbSession dbSession, NewAdHocRule adHoc, OrganizationDto organizationDto) { RuleDao dao = dbClient.ruleDao(); - Optional existingRuleDtoOpt = dao.selectByKey(dbSession, organizationDto, adHoc.getKey()); + Optional existingRuleDtoOpt = dao.selectByKey(dbSession, organizationDto.getUuid(), adHoc.getKey()); RuleMetadataDto metadata; long now = system2.now(); if (!existingRuleDtoOpt.isPresent()) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java index dc292e48f82..e5f586c69ab 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java @@ -25,7 +25,6 @@ import java.util.Optional; import java.util.Set; import java.util.function.Consumer; import javax.annotation.Nullable; - import org.apache.ibatis.session.ResultHandler; import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.RuleQuery; @@ -44,9 +43,9 @@ import static org.sonar.db.DatabaseUtils.executeLargeUpdates; public class RuleDao implements Dao { - public Optional selectByKey(DbSession session, OrganizationDto organization, RuleKey key) { - RuleDto res = mapper(session).selectByKey(organization.getUuid(), key); - ensureOrganizationIsSet(organization.getUuid(), res); + public Optional selectByKey(DbSession session, String organizationUuid, RuleKey key) { + RuleDto res = mapper(session).selectByKey(organizationUuid, key); + ensureOrganizationIsSet(organizationUuid, res); return ofNullable(res); } @@ -59,8 +58,8 @@ public class RuleDao implements Dao { return ofNullable(mapper(session).selectDefinitionByKey(key)); } - public Optional selectMetadataByKey(DbSession session, RuleKey key, OrganizationDto organization) { - return ofNullable(mapper(session).selectMetadataByKey(key, organization.getUuid())); + public Optional selectMetadataByKey(DbSession session, RuleKey key, String organizationUuid) { + return ofNullable(mapper(session).selectMetadataByKey(key, organizationUuid)); } public RuleDto selectOrFailByKey(DbSession session, OrganizationDto organization, RuleKey key) { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java index 4e1167afccb..3eb348a8797 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java @@ -84,9 +84,9 @@ public class RuleDaoTest { RuleMetadataDto metadata = newRuleMetadata(ruleDefinition, organization); db.rules().insertRule(ruleDefinition, metadata); - assertThat(underTest.selectByKey(db.getSession(), organization, RuleKey.of("foo", "bar"))) + assertThat(underTest.selectByKey(db.getSession(), organization.getUuid(), RuleKey.of("foo", "bar"))) .isEmpty(); - RuleDto rule = underTest.selectByKey(db.getSession(), organization, ruleDefinition.getKey()).get(); + RuleDto rule = underTest.selectByKey(db.getSession(), organization.getUuid(), ruleDefinition.getKey()).get(); assertEquals(rule.getDefinition(), ruleDefinition); verifyMetadata(rule.getMetadata(), ruleDefinition, metadata); } @@ -95,7 +95,7 @@ public class RuleDaoTest { public void selectByKey_return_rule_even_if_organization_does_not_exist() { RuleDefinitionDto ruleDefinition = db.rules().insert(); - assertThat(underTest.selectByKey(db.getSession(), OrganizationTesting.newOrganizationDto(), ruleDefinition.getKey())) + assertThat(underTest.selectByKey(db.getSession(), OrganizationTesting.newOrganizationDto().getUuid(), ruleDefinition.getKey())) .isNotEmpty(); } @@ -104,7 +104,7 @@ public class RuleDaoTest { OrganizationDto organization = db.organizations().insert(); RuleDefinitionDto ruleDefinition = db.rules().insert(); - RuleDto rule = underTest.selectByKey(db.getSession(), organization, ruleDefinition.getKey()).get(); + RuleDto rule = underTest.selectByKey(db.getSession(), organization.getUuid(), ruleDefinition.getKey()).get(); verifyNoMetadata(rule.getMetadata(), ruleDefinition, organization); } @@ -118,9 +118,9 @@ public class RuleDaoTest { RuleMetadataDto expectedOrg2 = newRuleMetadata(ruleDefinition, organization2); db.rules().insertRule(ruleDefinition, expectedOrg2); - RuleDto rule = underTest.selectByKey(db.getSession(), organization1, ruleDefinition.getKey()).get(); + RuleDto rule = underTest.selectByKey(db.getSession(), organization1.getUuid(), ruleDefinition.getKey()).get(); verifyMetadata(rule.getMetadata(), ruleDefinition, expectedOrg1); - rule = underTest.selectByKey(db.getSession(), organization2, ruleDefinition.getKey()).get(); + rule = underTest.selectByKey(db.getSession(), organization2.getUuid(), ruleDefinition.getKey()).get(); verifyMetadata(rule.getMetadata(), ruleDefinition, expectedOrg2); } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java b/server/sonar-server-common/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java index 8ac510c096b..a0354c128d3 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java @@ -79,7 +79,7 @@ public class DefaultRuleFinder implements RuleFinder { String defaultOrganizationUuid = defaultOrganizationProvider.get().getUuid(); OrganizationDto defaultOrganization = dbClient.organizationDao().selectByUuid(dbSession, defaultOrganizationUuid) .orElseThrow(() -> new IllegalStateException(String.format("Cannot find default organization '%s'", defaultOrganizationUuid))); - Optional rule = ruleDao.selectByKey(dbSession, defaultOrganization, key); + Optional rule = ruleDao.selectByKey(dbSession, defaultOrganization.getUuid(), key); if (rule.isPresent() && rule.get().getStatus() != RuleStatus.REMOVED) { return toRule(rule.get(), ruleDao.selectRuleParamsByRuleKey(dbSession, rule.get().getKey())); } else { diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/RuleCreator.java b/server/sonar-server/src/main/java/org/sonar/server/rule/RuleCreator.java index 60a149bb652..691fccc29e8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/RuleCreator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/RuleCreator.java @@ -74,7 +74,7 @@ public class RuleCreator { String defaultOrganizationUuid = defaultOrganizationProvider.get().getUuid(); OrganizationDto defaultOrganization = dbClient.organizationDao().selectByUuid(dbSession, defaultOrganizationUuid) .orElseThrow(() -> new IllegalStateException(format("Could not find default organization for uuid '%s'", defaultOrganizationUuid))); - RuleDto templateRule = dbClient.ruleDao().selectByKey(dbSession, defaultOrganization, templateKey) + RuleDto templateRule = dbClient.ruleDao().selectByKey(dbSession, defaultOrganization.getUuid(), templateKey) .orElseThrow(() -> new IllegalArgumentException(format("The template key doesn't exist: %s", templateKey))); checkArgument(templateRule.isTemplate(), "This rule is not a template rule: %s", templateKey.toString()); checkArgument(templateRule.getStatus() != RuleStatus.REMOVED, "The template key doesn't exist: %s", templateKey.toString()); diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/ShowAction.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/ShowAction.java index b3f87fc4914..d3ce556fba8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/ShowAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/ShowAction.java @@ -104,7 +104,7 @@ public class ShowAction implements RulesWsAction { RuleKey key = RuleKey.parse(request.mandatoryParam(PARAM_KEY)); try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = ruleWsSupport.getOrganizationByKey(dbSession, request.param(PARAM_ORGANIZATION)); - RuleDto rule = dbClient.ruleDao().selectByKey(dbSession, organization, key) + RuleDto rule = dbClient.ruleDao().selectByKey(dbSession, organization.getUuid(), key) .orElseThrow(() -> new NotFoundException(String.format("Rule not found: %s", key))); List templateRules = ofNullable(rule.getTemplateId()) diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/UpdateAction.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/UpdateAction.java index 40278e60e52..39eb128af3c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/UpdateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/UpdateAction.java @@ -207,7 +207,7 @@ public class UpdateAction implements RulesWsAction { } private RuleUpdate createRuleUpdate(DbSession dbSession, RuleKey key, OrganizationDto organization) { - RuleDto rule = dbClient.ruleDao().selectByKey(dbSession, organization, key) + RuleDto rule = dbClient.ruleDao().selectByKey(dbSession, organization.getUuid(), key) .orElseThrow(() -> new NotFoundException(format("This rule does not exist: %s", key))); RuleUpdate ruleUpdate = ofNullable(rule.getTemplateId()) .map(x -> RuleUpdate.createForCustomRule(key)) @@ -252,7 +252,7 @@ public class UpdateAction implements RulesWsAction { } private UpdateResponse buildResponse(DbSession dbSession, RuleKey key, OrganizationDto organization) { - RuleDto rule = dbClient.ruleDao().selectByKey(dbSession, organization, key) + RuleDto rule = dbClient.ruleDao().selectByKey(dbSession, organization.getUuid(), key) .orElseThrow(() -> new NotFoundException(format("Rule not found: %s", key))); List templateRules = new ArrayList<>(1); if (rule.getDefinition().isCustomRule()) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java index 80a5084a761..a557963cee6 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java @@ -664,7 +664,7 @@ public class RegisterRulesTest { repo.done(); }); - RuleDto reloaded = dbClient.ruleDao().selectByKey(db.getSession(), defaultOrganization, rule.getKey()).get(); + RuleDto reloaded = dbClient.ruleDao().selectByKey(db.getSession(), defaultOrganization.getUuid(), rule.getKey()).get(); assertThat(reloaded.isAdHoc()).isFalse(); } @@ -677,7 +677,7 @@ public class RegisterRulesTest { execute(); - RuleDto reloaded = dbClient.ruleDao().selectByKey(db.getSession(), defaultOrganization, rule.getKey()).get(); + RuleDto reloaded = dbClient.ruleDao().selectByKey(db.getSession(), defaultOrganization.getUuid(), rule.getKey()).get(); assertThat(reloaded.getStatus()).isEqualTo(REMOVED); } @@ -690,7 +690,7 @@ public class RegisterRulesTest { execute(); - RuleDto reloaded = dbClient.ruleDao().selectByKey(db.getSession(), defaultOrganization, rule.getKey()).get(); + RuleDto reloaded = dbClient.ruleDao().selectByKey(db.getSession(), defaultOrganization.getUuid(), rule.getKey()).get(); assertThat(reloaded.getStatus()).isEqualTo(READY); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/UpdateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/UpdateActionTest.java index a11505feed6..13bb50c00bc 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/UpdateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/UpdateActionTest.java @@ -197,7 +197,7 @@ public class UpdateActionTest { assertThat(updatedRule.getTags().getTagsList()).containsExactly("tag2", "tag3"); // check database - RuleMetadataDto metadataOfSpecificOrg = db.getDbClient().ruleDao().selectMetadataByKey(db.getSession(), rule.getKey(), organization) + RuleMetadataDto metadataOfSpecificOrg = db.getDbClient().ruleDao().selectMetadataByKey(db.getSession(), rule.getKey(), organization.getUuid()) .orElseThrow(() -> new IllegalStateException("Cannot load metadata")); assertThat(metadataOfSpecificOrg.getTags()).containsExactly("tag2", "tag3"); } @@ -238,7 +238,7 @@ public class UpdateActionTest { assertThat(updatedRule.getRemFnBaseEffort()).isEqualTo(newEffort); // check database - RuleMetadataDto metadataOfSpecificOrg = db.getDbClient().ruleDao().selectMetadataByKey(db.getSession(), rule.getKey(), organization) + RuleMetadataDto metadataOfSpecificOrg = db.getDbClient().ruleDao().selectMetadataByKey(db.getSession(), rule.getKey(), organization.getUuid()) .orElseThrow(() -> new IllegalStateException("Cannot load metadata")); assertThat(metadataOfSpecificOrg.getRemediationFunction()).isEqualTo(newOffset); assertThat(metadataOfSpecificOrg.getRemediationGapMultiplier()).isEqualTo(newMultiplier); @@ -267,7 +267,7 @@ public class UpdateActionTest { assertThat(updatedRule.getNoteLogin()).isEqualTo(userAuthenticated.getLogin()); // check database - RuleMetadataDto metadataOfSpecificOrg = db.getDbClient().ruleDao().selectMetadataByKey(db.getSession(), rule.getKey(), organization).get(); + RuleMetadataDto metadataOfSpecificOrg = db.getDbClient().ruleDao().selectMetadataByKey(db.getSession(), rule.getKey(), organization.getUuid()).get(); assertThat(metadataOfSpecificOrg.getNoteData()).isEqualTo("new data"); assertThat(metadataOfSpecificOrg.getNoteUserUuid()).isEqualTo(userAuthenticated.getUuid()); } -- 2.39.5