]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 13 Jul 2016 17:11:09 +0000 (19:11 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 13 Jul 2016 17:21:36 +0000 (19:21 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/issue/TrackerRawInputFactory.java
server/sonar-server/src/main/java/org/sonar/server/debt/DebtModelBackup.java
server/sonar-server/src/main/java/org/sonar/server/issue/IssueBulkChangeService.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java
server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java
server/sonar-server/src/main/java/org/sonar/server/source/HtmlTextDecorator.java
server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java
server/sonar-server/src/main/java/org/sonar/server/user/index/UserResultSetIterator.java
sonar-db/src/main/java/org/sonar/db/version/v51/CopyScmAccountsFromAuthorsToUsers.java
sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRuleParam.java

index bb81467ab44b04194d0a8b3a15ed4e5fc67141ee..e5fa25fbc747d1cb0677eb9a61f71cd62f5ec074 100644 (file)
@@ -98,13 +98,13 @@ public class TrackerRawInputFactory {
         // as late as possible
         while (reportIssues.hasNext()) {
           ScannerReport.Issue reportIssue = reportIssues.next();
-          if (isIssueOnUnsupportedCommonRule(reportIssue)) {
-            DefaultIssue issue = toIssue(getLineHashSequence(), reportIssue);
-            if (issueFilter.accept(issue, component)) {
-              result.add(issue);
-            }
-          } else {
+          if (!isIssueOnUnsupportedCommonRule(reportIssue)) {
             Loggers.get(getClass()).debug("Ignored issue from analysis report on rule {}:{}", reportIssue.getRuleRepository(), reportIssue.getRuleKey());
+            continue;
+          }
+          DefaultIssue issue = toIssue(getLineHashSequence(), reportIssue);
+          if (issueFilter.accept(issue, component)) {
+            result.add(issue);
           }
         }
       }
index 60b47e10347f24d938e5d637014b52bdcd56c49f..780b0a1190d405cf81a8db533ae45c32dba6fde6 100644 (file)
@@ -89,11 +89,12 @@ public class DebtModelBackup {
     try {
       List<RuleDebt> rules = newArrayList();
       for (RuleDto rule : dbClient.ruleDao().selectEnabled(session)) {
-        if (languageKey == null || languageKey.equals(rule.getLanguage())) {
-          RuleDebt ruleDebt = toRuleDebt(rule);
-          if (ruleDebt != null) {
-            rules.add(ruleDebt);
-          }
+        if (languageKey != null && !languageKey.equals(rule.getLanguage())) {
+          continue;
+        }
+        RuleDebt ruleDebt = toRuleDebt(rule);
+        if (ruleDebt != null) {
+          rules.add(ruleDebt);
         }
       }
       return debtModelXMLExporter.export(rules);
index 9d1d7eba9e059fb54119005e1e6de90db38c5c3a..e41ceac9470cdbffa1b6386d0d57cbb992ab7efd 100644 (file)
@@ -72,7 +72,7 @@ public class IssueBulkChangeService {
   private final UserSession userSession;
 
   public IssueBulkChangeService(DbClient dbClient, IssueIndex issueIndex, IssueStorage issueStorage, DefaultRuleFinder ruleFinder,
-                                NotificationManager notificationService, List<Action> actions, UserSession userSession) {
+    NotificationManager notificationService, List<Action> actions, UserSession userSession) {
     this.dbClient = dbClient;
     this.issueIndex = issueIndex;
     this.issueStorage = issueStorage;
@@ -100,26 +100,27 @@ public class IssueBulkChangeService {
       for (Action action : bulkActions) {
         applyAction(action, actionContext, issueBulkChangeQuery, result);
       }
-      if (result.issuesChanged().contains(issue)) {
-        // Apply comment action only on changed issues
-        if (issueBulkChangeQuery.hasComment()) {
-          applyAction(getAction(CommentAction.COMMENT_KEY), actionContext, issueBulkChangeQuery, result);
-        }
-        issueStorage.save((DefaultIssue) issue);
-        if (issueBulkChangeQuery.sendNotifications()) {
-          String projectKey = issue.projectKey();
-          if (projectKey != null) {
-            Rule rule = repository.rule(issue.ruleKey());
-            notificationService.scheduleForSending(new IssueChangeNotification()
-              .setIssue((DefaultIssue) issue)
-              .setChangeAuthorLogin(issueChangeContext.login())
-              .setRuleName(rule != null ? rule.getName() : null)
-              .setProject(projectKey, repository.project(projectKey).name())
-              .setComponent(repository.component(issue.componentKey())));
-          }
-        }
-        concernedProjects.add(issue.projectKey());
+      if (!result.issuesChanged().contains(issue)) {
+        continue;
+      }
+      if (issueBulkChangeQuery.hasComment()) {
+        applyAction(getAction(CommentAction.COMMENT_KEY), actionContext, issueBulkChangeQuery, result);
+      }
+      issueStorage.save((DefaultIssue) issue);
+      if (!issueBulkChangeQuery.sendNotifications()) {
+        continue;
+      }
+      String projectKey = issue.projectKey();
+      if (projectKey != null) {
+        Rule rule = repository.rule(issue.ruleKey());
+        notificationService.scheduleForSending(new IssueChangeNotification()
+          .setIssue((DefaultIssue) issue)
+          .setChangeAuthorLogin(issueChangeContext.login())
+          .setRuleName(rule != null ? rule.getName() : null)
+          .setProject(projectKey, repository.project(projectKey).name())
+          .setComponent(repository.component(issue.componentKey())));
       }
+      concernedProjects.add(issue.projectKey());
     }
     LOG.debug("BulkChange execution time : {} ms", System.currentTimeMillis() - start);
     return result;
index b3d62dfa3e24be0086fc9811c458f0811ea63f37..54b77e514639a453e05486b928d59a19d9097834 100644 (file)
@@ -126,20 +126,21 @@ public class ActiveRuleIndex extends BaseIndex {
     return stats;
   }
 
-  private Multimap<String, FacetValue> processAggregations(@Nullable Aggregations aggregations) {
+  private static Multimap<String, FacetValue> processAggregations(@Nullable Aggregations aggregations) {
     Multimap<String, FacetValue> stats = ArrayListMultimap.create();
-    if (aggregations != null) {
-      for (Aggregation aggregation : aggregations.asList()) {
-        if (aggregation instanceof StringTerms) {
-          for (Terms.Bucket value : ((Terms) aggregation).getBuckets()) {
-            FacetValue facetValue = new FacetValue(value.getKeyAsString(), value.getDocCount());
-            stats.put(aggregation.getName(), facetValue);
-          }
-        } else if (aggregation instanceof InternalValueCount) {
-          InternalValueCount count = (InternalValueCount) aggregation;
-          FacetValue facetValue = new FacetValue(count.getName(), count.getValue());
-          stats.put(count.getName(), facetValue);
+    if (aggregations == null) {
+      return stats;
+    }
+    for (Aggregation aggregation : aggregations.asList()) {
+      if (aggregation instanceof StringTerms) {
+        for (Terms.Bucket value : ((Terms) aggregation).getBuckets()) {
+          FacetValue facetValue = new FacetValue(value.getKeyAsString(), value.getDocCount());
+          stats.put(aggregation.getName(), facetValue);
         }
+      } else if (aggregation instanceof InternalValueCount) {
+        InternalValueCount count = (InternalValueCount) aggregation;
+        FacetValue facetValue = new FacetValue(count.getName(), count.getValue());
+        stats.put(count.getName(), facetValue);
       }
     }
     return stats;
index dd8cd06cc9d0ee2e7bd4777f13101e0d95007ca9..9da3af25f22161be7f8deb6c75f8a211d7195652 100644 (file)
@@ -303,20 +303,22 @@ public class RegisterRules implements Startable {
     // Create newly parameters
     for (RulesDefinition.Param param : ruleDef.params()) {
       RuleParamDto paramDto = existingParamsByName.get(param.key());
-      if (paramDto == null) {
-        paramDto = RuleParamDto.createFor(rule)
-          .setName(param.key())
-          .setDescription(param.description())
-          .setDefaultValue(param.defaultValue())
-          .setType(param.type().toString());
-        dbClient.ruleDao().insertRuleParam(session, rule, paramDto);
-        if (!StringUtils.isEmpty(param.defaultValue())) {
-          // Propagate the default value to existing active rule parameters
-          for (ActiveRuleDto activeRule : dbClient.activeRuleDao().selectByRuleId(session, rule.getId())) {
-            ActiveRuleParamDto activeParam = ActiveRuleParamDto.createFor(paramDto).setValue(param.defaultValue());
-            dbClient.activeRuleDao().insertParam(session, activeRule, activeParam);
-          }
-        }
+      if (paramDto != null) {
+        continue;
+      }
+      paramDto = RuleParamDto.createFor(rule)
+        .setName(param.key())
+        .setDescription(param.description())
+        .setDefaultValue(param.defaultValue())
+        .setType(param.type().toString());
+      dbClient.ruleDao().insertRuleParam(session, rule, paramDto);
+      if (StringUtils.isEmpty(param.defaultValue())) {
+        continue;
+      }
+      // Propagate the default value to existing active rule parameters
+      for (ActiveRuleDto activeRule : dbClient.activeRuleDao().selectByRuleId(session, rule.getId())) {
+        ActiveRuleParamDto activeParam = ActiveRuleParamDto.createFor(paramDto).setValue(param.defaultValue());
+        dbClient.activeRuleDao().insertParam(session, activeRule, activeParam);
       }
     }
   }
@@ -451,8 +453,7 @@ public class RegisterRules implements Startable {
       public String apply(@Nonnull RulesDefinition.Repository input) {
         return input.key();
       }
-    }
-      ));
+    }));
 
     List<ActiveRuleChange> changes = new ArrayList<>();
     for (RuleDto rule : removedRules) {
index 700f3d24fc10924f01b2c3196509773825ba7098..6b582262c81c09f9c6f365bb9c41c02fea91e22c 100644 (file)
@@ -129,7 +129,6 @@ class HtmlTextDecorator {
     return to != null && to < currentLine;
   }
 
-
   private char[] normalize(char currentChar) {
     char[] normalizedChars;
     if (currentChar == HTML_OPENING) {
@@ -139,7 +138,7 @@ class HtmlTextDecorator {
     } else if (currentChar == AMPERSAND) {
       normalizedChars = ENCODED_AMPERSAND.toCharArray();
     } else {
-      normalizedChars = new char[]{currentChar};
+      normalizedChars = new char[] {currentChar};
     }
     return normalizedChars;
   }
@@ -176,7 +175,7 @@ class HtmlTextDecorator {
   private boolean shouldReopenPendingTags(CharactersReader charactersReader) {
     return (charactersReader.getPreviousValue() == LF_END_OF_LINE && charactersReader.getCurrentValue() != LF_END_OF_LINE)
       || (charactersReader.getPreviousValue() == CR_END_OF_LINE && charactersReader.getCurrentValue() != CR_END_OF_LINE
-      && charactersReader.getCurrentValue() != LF_END_OF_LINE);
+        && charactersReader.getCurrentValue() != LF_END_OF_LINE);
   }
 
   private boolean shouldStartNewLine(CharactersReader charactersReader) {
@@ -185,7 +184,7 @@ class HtmlTextDecorator {
   }
 
   private void closeCompletedTags(CharactersReader charactersReader, int numberOfTagsToClose,
-                                  StringBuilder decoratedText) {
+    StringBuilder decoratedText) {
     for (int i = 0; i < numberOfTagsToClose; i++) {
       injectClosingHtml(decoratedText);
       charactersReader.removeLastOpenTag();
@@ -193,7 +192,7 @@ class HtmlTextDecorator {
   }
 
   private void openNewTags(CharactersReader charactersReader, Collection<String> tagsToOpen,
-                           StringBuilder decoratedText) {
+    StringBuilder decoratedText) {
     for (String tagToOpen : tagsToOpen) {
       injectOpeningHtmlForRule(tagToOpen, decoratedText);
       charactersReader.registerOpenTag(tagToOpen);
index 2e869f5cdd33cda9fc950fcfcd4fc2b1e43ccf27..25836f1d272dd610fb14b30ede877ccdf9477603 100644 (file)
  */
 package org.sonar.server.user;
 
-import static com.google.common.base.Strings.isNullOrEmpty;
-import static com.google.common.collect.Lists.newArrayList;
-import static org.sonar.db.user.UserDto.encryptPassword;
-
 import com.google.common.base.Joiner;
 import com.google.common.base.Predicate;
 import com.google.common.collect.Iterables;
@@ -51,6 +47,10 @@ import org.sonar.server.exceptions.ServerException;
 import org.sonar.server.user.index.UserIndexer;
 import org.sonar.server.util.Validation;
 
+import static com.google.common.base.Strings.isNullOrEmpty;
+import static com.google.common.collect.Lists.newArrayList;
+import static org.sonar.db.user.UserDto.encryptPassword;
+
 @ServerSide
 public class UserUpdater {
 
@@ -336,9 +336,10 @@ public class UserUpdater {
         List<UserDto> matchingUsers = dbClient.userDao().selectByScmAccountOrLoginOrEmail(dbSession, scmAccount);
         List<String> matchingUsersWithoutExistingUser = newArrayList();
         for (UserDto matchingUser : matchingUsers) {
-          if (existingUser == null || !matchingUser.getId().equals(existingUser.getId())) {
-            matchingUsersWithoutExistingUser.add(matchingUser.getName() + " (" + matchingUser.getLogin() + ")");
+          if (existingUser != null && matchingUser.getId().equals(existingUser.getId())) {
+            continue;
           }
+          matchingUsersWithoutExistingUser.add(matchingUser.getName() + " (" + matchingUser.getLogin() + ")");
         }
         if (!matchingUsersWithoutExistingUser.isEmpty()) {
           messages.add(Message.of("user.scm_account_already_used", scmAccount, Joiner.on(", ").join(matchingUsersWithoutExistingUser)));
index b23f7572a3be0827b43c0be28b36e13a0b384416..6a9de0063eef89c01ecbe904306b46df225929ef 100644 (file)
 package org.sonar.server.user.index;
 
 import com.google.common.collect.Maps;
-import org.apache.commons.lang.StringUtils;
-import org.sonar.db.DbSession;
-import org.sonar.db.user.UserDto;
-import org.sonar.db.DbClient;
-import org.sonar.db.ResultSetIterator;
-
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.ResultSetIterator;
+import org.sonar.db.user.UserDto;
 
 /**
  * Scrolls over table USERS and reads documents to populate the user index
@@ -50,6 +49,10 @@ class UserResultSetIterator extends ResultSetIterator<UserDoc> {
 
   private static final String SQL_AFTER_DATE = SQL_ALL + " where u.updated_at>?";
 
+  private UserResultSetIterator(PreparedStatement stmt) throws SQLException {
+    super(stmt);
+  }
+
   static UserResultSetIterator create(DbClient dbClient, DbSession session, long afterDate) {
     try {
       String sql = afterDate > 0L ? SQL_AFTER_DATE : SQL_ALL;
@@ -63,10 +66,6 @@ class UserResultSetIterator extends ResultSetIterator<UserDoc> {
     }
   }
 
-  private UserResultSetIterator(PreparedStatement stmt) throws SQLException {
-    super(stmt);
-  }
-
   @Override
   protected UserDoc read(ResultSet rs) throws SQLException {
     UserDoc doc = new UserDoc(Maps.<String, Object>newHashMapWithExpectedSize(7));
index ddbed3a3f58b5c142bcb5fae317721fe6f54df15..369ae4f1b118cedf4bfcfefd6144031168a37f9a 100644 (file)
@@ -59,29 +59,29 @@ public class CopyScmAccountsFromAuthorsToUsers extends BaseDataChange {
       final Multimap<Long, String> authorsByPersonId = ArrayListMultimap.create();
       context.prepareSelect("SELECT a.person_id, a.login FROM authors a," +
         "  (SELECT person_id, COUNT(*) AS nb FROM authors GROUP BY person_id HAVING COUNT(*) > 1) group_by_person" +
-        "     WHERE a.person_id = group_by_person.person_id "
-        ).scroll(new AuthorsByPersonIdHandler(authorsByPersonId));
+        "     WHERE a.person_id = group_by_person.person_id ").scroll(new AuthorsByPersonIdHandler(authorsByPersonId));
 
       Upsert update = context.prepareUpsert("UPDATE users SET scm_accounts = ?, updated_at = ? WHERE id = ?");
       for (Long personId : authorsByPersonId.keySet()) {
         List<String> authors = newArrayList(authorsByPersonId.get(personId));
         List<User> users = selectUsersFromLoginOrEmail(context, authors);
-        if (users.size() == 1) {
-          User user = users.get(0);
-          if (authors.contains(user.login)) {
-            authors.remove(user.login);
-          }
-          if (authors.contains(user.email)) {
-            authors.remove(user.email);
-          }
-          if (!authors.isEmpty()) {
-            update
-              .setString(1, encodeScmAccounts(authors))
-              .setLong(2, now)
-              .setLong(3, user.id)
-              .addBatch();
-            counter.getAndIncrement();
-          }
+        if (users.size() != 1) {
+          continue;
+        }
+        User user = users.get(0);
+        if (authors.contains(user.login)) {
+          authors.remove(user.login);
+        }
+        if (authors.contains(user.email)) {
+          authors.remove(user.email);
+        }
+        if (!authors.isEmpty()) {
+          update
+            .setString(1, encodeScmAccounts(authors))
+            .setLong(2, now)
+            .setLong(3, user.id)
+            .addBatch();
+          counter.getAndIncrement();
         }
       }
       if (((UpsertImpl) update).getBatchCount() > 0L) {
index bb18090b3788839402591690b65a0c15354b3619..61e5a0a5b3d21a244f6c6690013cc758abee557a 100644 (file)
@@ -27,18 +27,6 @@ public class ActiveRuleParam implements Cloneable {
   private String paramKey;
   private String value;
 
-  public Integer getId() {
-    return id;
-  }
-
-  /**
-   * @deprecated visibility should be decreased to protected or package
-   */
-  @Deprecated
-  void setId(Integer id) {
-    this.id = id;
-  }
-
   /**
    * @deprecated visibility should be decreased to protected or package
    */
@@ -57,6 +45,18 @@ public class ActiveRuleParam implements Cloneable {
     this.paramKey = ruleParam.getKey();
   }
 
+  public Integer getId() {
+    return id;
+  }
+
+  /**
+   * @deprecated visibility should be decreased to protected or package
+   */
+  @Deprecated
+  void setId(Integer id) {
+    this.id = id;
+  }
+
   public ActiveRule getActiveRule() {
     return activeRule;
   }