From: Julien Lancelot Date: Wed, 13 Jul 2016 17:11:09 +0000 (+0200) Subject: Fix quality flaws X-Git-Tag: 6.0-RC1~55 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=3f785d11349584c856b0c55a014e4714037ddd84;p=sonarqube.git Fix quality flaws --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/issue/TrackerRawInputFactory.java b/server/sonar-server/src/main/java/org/sonar/server/computation/issue/TrackerRawInputFactory.java index bb81467ab44..e5fa25fbc74 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/issue/TrackerRawInputFactory.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/issue/TrackerRawInputFactory.java @@ -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); } } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/debt/DebtModelBackup.java b/server/sonar-server/src/main/java/org/sonar/server/debt/DebtModelBackup.java index 60b47e10347..780b0a1190d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/debt/DebtModelBackup.java +++ b/server/sonar-server/src/main/java/org/sonar/server/debt/DebtModelBackup.java @@ -89,11 +89,12 @@ public class DebtModelBackup { try { List 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); diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueBulkChangeService.java b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueBulkChangeService.java index 9d1d7eba9e0..e41ceac9470 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueBulkChangeService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueBulkChangeService.java @@ -72,7 +72,7 @@ public class IssueBulkChangeService { private final UserSession userSession; public IssueBulkChangeService(DbClient dbClient, IssueIndex issueIndex, IssueStorage issueStorage, DefaultRuleFinder ruleFinder, - NotificationManager notificationService, List actions, UserSession userSession) { + NotificationManager notificationService, List 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; diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java index b3d62dfa3e2..54b77e51463 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java @@ -126,20 +126,21 @@ public class ActiveRuleIndex extends BaseIndex { return stats; } - private Multimap processAggregations(@Nullable Aggregations aggregations) { + private static Multimap processAggregations(@Nullable Aggregations aggregations) { Multimap 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; diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java b/server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java index dd8cd06cc9d..9da3af25f22 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java @@ -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 changes = new ArrayList<>(); for (RuleDto rule : removedRules) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/source/HtmlTextDecorator.java b/server/sonar-server/src/main/java/org/sonar/server/source/HtmlTextDecorator.java index 700f3d24fc1..6b582262c81 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/source/HtmlTextDecorator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/source/HtmlTextDecorator.java @@ -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 tagsToOpen, - StringBuilder decoratedText) { + StringBuilder decoratedText) { for (String tagToOpen : tagsToOpen) { injectOpeningHtmlForRule(tagToOpen, decoratedText); charactersReader.registerOpenTag(tagToOpen); diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java b/server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java index 2e869f5cdd3..25836f1d272 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java +++ b/server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java @@ -19,10 +19,6 @@ */ 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 matchingUsers = dbClient.userDao().selectByScmAccountOrLoginOrEmail(dbSession, scmAccount); List 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))); diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/index/UserResultSetIterator.java b/server/sonar-server/src/main/java/org/sonar/server/user/index/UserResultSetIterator.java index b23f7572a3b..6a9de0063ee 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/user/index/UserResultSetIterator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/user/index/UserResultSetIterator.java @@ -20,15 +20,14 @@ 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 { 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 { } } - private UserResultSetIterator(PreparedStatement stmt) throws SQLException { - super(stmt); - } - @Override protected UserDoc read(ResultSet rs) throws SQLException { UserDoc doc = new UserDoc(Maps.newHashMapWithExpectedSize(7)); diff --git a/sonar-db/src/main/java/org/sonar/db/version/v51/CopyScmAccountsFromAuthorsToUsers.java b/sonar-db/src/main/java/org/sonar/db/version/v51/CopyScmAccountsFromAuthorsToUsers.java index ddbed3a3f58..369ae4f1b11 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/v51/CopyScmAccountsFromAuthorsToUsers.java +++ b/sonar-db/src/main/java/org/sonar/db/version/v51/CopyScmAccountsFromAuthorsToUsers.java @@ -59,29 +59,29 @@ public class CopyScmAccountsFromAuthorsToUsers extends BaseDataChange { final Multimap 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 authors = newArrayList(authorsByPersonId.get(personId)); List 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) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRuleParam.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRuleParam.java index bb18090b378..61e5a0a5b3d 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRuleParam.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRuleParam.java @@ -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; }