]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8745 reduce code complexity of persisting issues
authorDaniel Schwarz <daniel.schwarz@sonarsource.com>
Thu, 2 Feb 2017 12:58:57 +0000 (13:58 +0100)
committerDaniel Schwarz <bartfastiel@users.noreply.github.com>
Fri, 10 Feb 2017 07:57:34 +0000 (08:57 +0100)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistIssuesStep.java

index ed708af42a10fdd291ec68527fd612c4b3d7579a..71103230b2de4fb6dac7e17409624cb74bab04ca 100644 (file)
@@ -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;