diff options
author | lukasz-jarocki-sonarsource <lukasz.jarocki@sonarsource.com> | 2023-10-05 17:06:41 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-10-10 20:02:44 +0000 |
commit | 9ff811fd2d3421731dfb4097e8748cee1553d2c5 (patch) | |
tree | 45d4866bd21131d61def4f67536433aaade279c2 /server/sonar-db-dao/src | |
parent | 047d3846d5e415786323625cd5101a9d54b04725 (diff) | |
download | sonarqube-9ff811fd2d3421731dfb4097e8748cee1553d2c5.tar.gz sonarqube-9ff811fd2d3421731dfb4097e8748cee1553d2c5.zip |
SONAR-20548 added event for changing the CCT data for rules
Diffstat (limited to 'server/sonar-db-dao/src')
10 files changed, 284 insertions, 4 deletions
diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/rule/RuleChangeDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/rule/RuleChangeDaoIT.java new file mode 100644 index 00000000000..0a3548707c7 --- /dev/null +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/rule/RuleChangeDaoIT.java @@ -0,0 +1,76 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.rule; + +import java.util.Set; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.issue.impact.Severity; +import org.sonar.api.rules.CleanCodeAttribute; +import org.sonar.api.utils.System2; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; +import org.sonar.db.qualityprofile.RuleImpactChangeDto; +import org.sonarqube.ws.Common; + +import static org.assertj.core.api.Assertions.assertThat; + +public class RuleChangeDaoIT { + + @Rule + public DbTester db = DbTester.create(System2.INSTANCE); + + private final RuleChangeDao underTest = db.getDbClient().ruleChangeDao(); + + @Test + public void insert_shouldInsertRuleChangeWithNullableImpacts() { + RuleChangeDto ruleChangeDto = new RuleChangeDto(); + ruleChangeDto.setNewCleanCodeAttribute(CleanCodeAttribute.CLEAR); + ruleChangeDto.setOldCleanCodeAttribute(CleanCodeAttribute.CONVENTIONAL); + ruleChangeDto.setRuleUuid("ruleUuid"); + ruleChangeDto.setUuid("uuid"); + + RuleImpactChangeDto ruleImpactChangeDto = new RuleImpactChangeDto(); + ruleImpactChangeDto.setNewSoftwareQuality(Common.SoftwareQuality.RELIABILITY.name()); + ruleImpactChangeDto.setOldSoftwareQuality(Common.SoftwareQuality.RELIABILITY.name()); + ruleImpactChangeDto.setNewSeverity(Severity.LOW.name()); + ruleImpactChangeDto.setOldSeverity(Severity.HIGH.name()); + + RuleImpactChangeDto ruleImpactChangeDto2 = new RuleImpactChangeDto(); + ruleImpactChangeDto2.setNewSoftwareQuality(Common.SoftwareQuality.SECURITY.name()); + ruleImpactChangeDto2.setNewSeverity(Severity.MEDIUM.name()); + + RuleImpactChangeDto ruleImpactChangeDto3 = new RuleImpactChangeDto(); + ruleImpactChangeDto2.setOldSoftwareQuality(Common.SoftwareQuality.MAINTAINABILITY.name()); + ruleImpactChangeDto2.setOldSeverity(Severity.MEDIUM.name()); + + Set<RuleImpactChangeDto> impactChanges = Set.of(ruleImpactChangeDto, ruleImpactChangeDto2, ruleImpactChangeDto3); + impactChanges.forEach(i -> i.setRuleChangeUuid(ruleChangeDto.getUuid())); + + ruleChangeDto.setRuleImpactChangeDtos(impactChanges); + DbSession session = db.getSession(); + + underTest.insert(session, ruleChangeDto); + session.commit(); + + assertThat(db.select("select * from rule_impact_changes")).hasSize(3); + assertThat(db.select("select * from rule_changes")).hasSize(1); + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java index e67eee6703d..cd454bd0d64 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java @@ -82,6 +82,7 @@ import org.sonar.db.qualityprofile.QualityProfileExportDao; import org.sonar.db.report.RegulatoryReportDao; import org.sonar.db.report.ReportScheduleDao; import org.sonar.db.report.ReportSubscriptionDao; +import org.sonar.db.rule.RuleChangeDao; import org.sonar.db.rule.RuleDao; import org.sonar.db.rule.RuleRepositoryDao; import org.sonar.db.scannercache.ScannerAnalysisCacheDao; @@ -172,6 +173,7 @@ public class DaoModule extends Module { ReportScheduleDao.class, RoleDao.class, RuleDao.class, + RuleChangeDao.class, RuleRepositoryDao.class, SamlMessageIdDao.class, ScannerAnalysisCacheDao.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java index 9949da0a223..f4f30d4e406 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java @@ -82,6 +82,7 @@ import org.sonar.db.qualityprofile.QualityProfileExportDao; import org.sonar.db.report.RegulatoryReportDao; import org.sonar.db.report.ReportScheduleDao; import org.sonar.db.report.ReportSubscriptionDao; +import org.sonar.db.rule.RuleChangeDao; import org.sonar.db.rule.RuleDao; import org.sonar.db.rule.RuleRepositoryDao; import org.sonar.db.scannercache.ScannerAnalysisCacheDao; @@ -189,6 +190,7 @@ public class DbClient { private final ReportSubscriptionDao reportSubscriptionDao; private final GithubOrganizationGroupDao githubOrganizationGroupDao; private final GithubPermissionsMappingDao githubPermissionsMappingDao; + private final RuleChangeDao ruleChangeDao; public DbClient(Database database, MyBatis myBatis, DBSessions dbSessions, Dao... daos) { this.database = database; @@ -279,6 +281,7 @@ public class DbClient { reportScheduleDao = getDao(map, ReportScheduleDao.class); reportSubscriptionDao = getDao(map, ReportSubscriptionDao.class); anticipatedTransitionDao = getDao(map, AnticipatedTransitionDao.class); + ruleChangeDao = getDao(map, RuleChangeDao.class); } public DbSession openSession(boolean batch) { @@ -618,5 +621,9 @@ public class DbClient { public AnticipatedTransitionDao anticipatedTransitionDao() { return anticipatedTransitionDao; } + + public RuleChangeDao ruleChangeDao() { + return ruleChangeDao; + } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java index 76f0e4a5faf..0ffad887f73 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java @@ -146,6 +146,7 @@ import org.sonar.db.qualityprofile.QualityProfileMapper; import org.sonar.db.report.RegulatoryReportMapper; import org.sonar.db.report.ReportScheduleMapper; import org.sonar.db.report.ReportSubscriptionMapper; +import org.sonar.db.rule.RuleChangeMapper; import org.sonar.db.rule.RuleMapper; import org.sonar.db.rule.RuleParamDto; import org.sonar.db.rule.RuleRepositoryMapper; @@ -331,6 +332,7 @@ public class MyBatis { ReportSubscriptionMapper.class, RoleMapper.class, RuleMapper.class, + RuleChangeMapper.class, RuleRepositoryMapper.class, SamlMessageIdMapper.class, ScannerAnalysisCacheMapper.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileChangeDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileChangeDto.java index 9e2c7b2bce9..2bf6361d8dc 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileChangeDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileChangeDto.java @@ -45,6 +45,7 @@ public class QProfileChangeDto { private Set<RuleImpactChangeDto> ruleImpactChangeDtos; private long createdAt; + private String ruleChangeUuid; public String getUuid() { return uuid; @@ -147,5 +148,11 @@ public class QProfileChangeDto { return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE); } + public void setRuleChangeUuid(String ruleChangeUuid) { + this.ruleChangeUuid = ruleChangeUuid; + } + public String getRuleChangeUuid() { + return ruleChangeUuid; + } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleChangeDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleChangeDao.java new file mode 100644 index 00000000000..0b7ee3133d2 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleChangeDao.java @@ -0,0 +1,38 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.rule; + +import org.sonar.db.Dao; +import org.sonar.db.DbSession; +import org.sonar.db.qualityprofile.RuleImpactChangeDto; + +public class RuleChangeDao implements Dao { + + /** + * Inserts a rule change with its impacts. + * The method doesn't commit its transaction. + */ + public void insert(DbSession session, RuleChangeDto ruleChangeDto) { + for (RuleImpactChangeDto ruleImpactChangeDto : ruleChangeDto.getRuleImpactChangeDtos()) { + session.getMapper(RuleChangeMapper.class).insertRuleImpactChange(ruleImpactChangeDto); + } + session.getMapper(RuleChangeMapper.class).insertRuleChange(ruleChangeDto); + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleChangeDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleChangeDto.java new file mode 100644 index 00000000000..776817fe063 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleChangeDto.java @@ -0,0 +1,79 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.rule; + +import java.util.HashSet; +import java.util.Set; +import org.sonar.api.rules.CleanCodeAttribute; +import org.sonar.db.qualityprofile.RuleImpactChangeDto; + +public class RuleChangeDto { + + private String uuid; + private CleanCodeAttribute oldCleanCodeAttribute; + private CleanCodeAttribute newCleanCodeAttribute; + private Set<RuleImpactChangeDto> ruleImpactChangeDtos = new HashSet<>(); + private String ruleUuid; + + public CleanCodeAttribute getOldCleanCodeAttribute() { + return oldCleanCodeAttribute; + } + + public void setOldCleanCodeAttribute(CleanCodeAttribute oldCleanCodeAttribute) { + this.oldCleanCodeAttribute = oldCleanCodeAttribute; + } + + public CleanCodeAttribute getNewCleanCodeAttribute() { + return newCleanCodeAttribute; + } + + public void setNewCleanCodeAttribute(CleanCodeAttribute newCleanCodeAttribute) { + this.newCleanCodeAttribute = newCleanCodeAttribute; + } + + public Set<RuleImpactChangeDto> getRuleImpactChangeDtos() { + return ruleImpactChangeDtos; + } + + public void setRuleImpactChangeDtos(Set<RuleImpactChangeDto> ruleImpactChangeDtos) { + this.ruleImpactChangeDtos = ruleImpactChangeDtos; + } + + public String getRuleUuid() { + return ruleUuid; + } + + public void setRuleUuid(String ruleUuid) { + this.ruleUuid = ruleUuid; + } + + public String getUuid() { + return uuid; + } + + public void setUuid(String uuid) { + this.uuid = uuid; + } + + public void addRuleImpactChangeDto(RuleImpactChangeDto ruleImpactChangeDto) { + this.ruleImpactChangeDtos.add(ruleImpactChangeDto); + } + +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleChangeMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleChangeMapper.java new file mode 100644 index 00000000000..7d344f68091 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleChangeMapper.java @@ -0,0 +1,30 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.rule; + +import org.apache.ibatis.annotations.Param; +import org.sonar.db.qualityprofile.RuleImpactChangeDto; + +public interface RuleChangeMapper { + + void insertRuleChange(@Param("dto") RuleChangeDto dto); + + void insertRuleImpactChange(@Param("dto") RuleImpactChangeDto dto); +} diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleChangeMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleChangeMapper.xml new file mode 100644 index 00000000000..430d6187ddd --- /dev/null +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleChangeMapper.xml @@ -0,0 +1,39 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd"> + +<mapper namespace="org.sonar.db.rule.RuleChangeMapper"> + + <insert id="insertRuleChange" parameterType="Map" useGeneratedKeys="false"> + insert into rule_changes ( + uuid, + new_clean_code_attribute, + old_clean_code_attribute, + rule_uuid + ) + values ( + #{dto.uuid,jdbcType=VARCHAR}, + #{dto.newCleanCodeAttribute,jdbcType=VARCHAR}, + #{dto.oldCleanCodeAttribute,jdbcType=VARCHAR}, + #{dto.ruleUuid,jdbcType=VARCHAR} + ) + </insert> + + <insert id="insertRuleImpactChange" parameterType="Map" useGeneratedKeys="false"> + insert into rule_impact_changes ( + new_software_quality, + old_software_quality, + new_severity, + old_severity, + rule_change_uuid + ) + values ( + #{dto.newSoftwareQuality,jdbcType=VARCHAR}, + #{dto.oldSoftwareQuality,jdbcType=VARCHAR}, + #{dto.newSeverity,jdbcType=VARCHAR}, + #{dto.oldSeverity,jdbcType=VARCHAR}, + #{dto.ruleChangeUuid,jdbcType=VARCHAR} + ) + </insert> + +</mapper> + diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl index f8fb47e1b5a..bc77b77d309 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -894,10 +894,10 @@ ALTER TABLE "RULE_DESC_SECTIONS" ADD CONSTRAINT "PK_RULE_DESC_SECTIONS" PRIMARY CREATE UNIQUE NULLS DISTINCT INDEX "UNIQ_RULE_DESC_SECTIONS" ON "RULE_DESC_SECTIONS"("RULE_UUID" NULLS FIRST, "KEE" NULLS FIRST, "CONTEXT_KEY" NULLS FIRST); CREATE TABLE "RULE_IMPACT_CHANGES"( - "NEW_SOFTWARE_QUALITY" CHARACTER VARYING(40) NOT NULL, - "OLD_SOFTWARE_QUALITY" CHARACTER VARYING(40) NOT NULL, - "NEW_SEVERITY" CHARACTER VARYING(40) NOT NULL, - "OLD_SEVERITY" CHARACTER VARYING(40) NOT NULL, + "NEW_SOFTWARE_QUALITY" CHARACTER VARYING(40), + "OLD_SOFTWARE_QUALITY" CHARACTER VARYING(40), + "NEW_SEVERITY" CHARACTER VARYING(40), + "OLD_SEVERITY" CHARACTER VARYING(40), "RULE_CHANGE_UUID" CHARACTER VARYING(40) NOT NULL ); CREATE INDEX "RULE_IMPACT_CHANGES_R_C_UUID" ON "RULE_IMPACT_CHANGES"("RULE_CHANGE_UUID" NULLS FIRST); |