From: Daniel Schwarz Date: Thu, 2 Feb 2017 12:58:57 +0000 (+0100) Subject: SONAR-8745 reduce code complexity of persisting issues X-Git-Tag: 6.3-RC1~73 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a887c37a525ac0d9300ae0756e01ff730408cc0f;p=sonarqube.git SONAR-8745 reduce code complexity of persisting issues --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistIssuesStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistIssuesStep.java index ed708af42a1..71103230b2d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistIssuesStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistIssuesStep.java @@ -24,17 +24,17 @@ import org.sonar.api.utils.System2; import org.sonar.core.issue.DefaultIssue; import org.sonar.core.issue.DefaultIssueComment; import org.sonar.core.issue.FieldDiffs; +import org.sonar.core.util.CloseableIterator; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.MyBatis; import org.sonar.db.issue.IssueChangeDto; import org.sonar.db.issue.IssueChangeMapper; import org.sonar.db.issue.IssueDto; import org.sonar.db.issue.IssueMapper; -import org.sonar.server.computation.task.projectanalysis.issue.UpdateConflictResolver; -import org.sonar.db.DbSession; -import org.sonar.db.MyBatis; import org.sonar.server.computation.task.projectanalysis.issue.IssueCache; import org.sonar.server.computation.task.projectanalysis.issue.RuleRepository; -import org.sonar.db.DbClient; -import org.sonar.core.util.CloseableIterator; +import org.sonar.server.computation.task.projectanalysis.issue.UpdateConflictResolver; import org.sonar.server.computation.task.step.ComputationStep; public class PersistIssuesStep implements ComputationStep { @@ -64,22 +64,7 @@ public class PersistIssuesStep implements ComputationStep { try { while (issues.hasNext()) { DefaultIssue issue = issues.next(); - boolean saved = false; - if (issue.isNew()) { - Integer ruleId = ruleRepository.getByKey(issue.ruleKey()).getId(); - IssueDto dto = IssueDto.toDtoForComputationInsert(issue, ruleId, system2.now()); - mapper.insert(dto); - saved = true; - } else if (issue.isChanged()) { - IssueDto dto = IssueDto.toDtoForUpdate(issue, system2.now()); - int updateCount = mapper.updateIfBeforeSelectedDate(dto); - if (updateCount == 0) { - // End-user and scan changed the issue at the same time. - // See https://jira.sonarsource.com/browse/SONAR-4309 - conflictResolver.resolve(issue, mapper); - } - saved = true; - } + boolean saved = persistIssueIfRequired(mapper, issue); if (saved) { insertChanges(changeMapper, issue); } @@ -92,6 +77,34 @@ public class PersistIssuesStep implements ComputationStep { } } + private boolean persistIssueIfRequired(IssueMapper mapper, DefaultIssue issue) { + if (issue.isNew()) { + persistNewIssue(mapper, issue); + return true; + } + if (issue.isChanged()) { + persistChangedIssue(mapper, issue); + return true; + } + return false; + } + + private void persistNewIssue(IssueMapper mapper, DefaultIssue issue) { + Integer ruleId = ruleRepository.getByKey(issue.ruleKey()).getId(); + IssueDto dto = IssueDto.toDtoForComputationInsert(issue, ruleId, system2.now()); + mapper.insert(dto); + } + + private void persistChangedIssue(IssueMapper mapper, DefaultIssue issue) { + IssueDto dto = IssueDto.toDtoForUpdate(issue, system2.now()); + int updateCount = mapper.updateIfBeforeSelectedDate(dto); + if (updateCount == 0) { + // End-user and scan changed the issue at the same time. + // See https://jira.sonarsource.com/browse/SONAR-4309 + conflictResolver.resolve(issue, mapper); + } + } + private static void insertChanges(IssueChangeMapper mapper, DefaultIssue issue) { for (IssueComment comment : issue.comments()) { DefaultIssueComment c = (DefaultIssueComment) comment;