Skip to content

Commit

Permalink
SONAR-10544 Dont load descriptionUrl and ruleTitle from scanner report
Browse files Browse the repository at this point in the history
  • Loading branch information
dbmeneses authored and SonarTech committed Apr 26, 2018
1 parent 6c5aad9 commit 4754c43
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.util.Collections;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.concurrent.Immutable;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.rule.RuleKey;
Expand All @@ -32,16 +31,12 @@
@Immutable
public class NewExternalRule implements Rule {
private final RuleKey key;
private final String name;
private final String descriptionUrl;
private final String severity;
private final RuleType type;
private final String pluginKey;

private NewExternalRule(Builder builder) {
this.key = checkNotNull(builder.key, "key");
this.name = checkNotEmpty(builder.name, "name");
this.descriptionUrl = builder.descriptionUrl;
this.severity = checkNotEmpty(builder.severity, "severity");
this.type = checkNotNull(builder.type, "type");
this.pluginKey = builder.pluginKey;
Expand All @@ -61,11 +56,6 @@ private static <T> T checkNotNull(T obj, String name) {
return obj;
}

@CheckForNull
public String getDescriptionUrl() {
return descriptionUrl;
}

public String getSeverity() {
return severity;
}
Expand All @@ -82,7 +72,7 @@ public RuleKey getKey() {

@Override
public String getName() {
return name;
return key.toString();
}

@Override
Expand Down Expand Up @@ -117,8 +107,6 @@ public String getPluginKey() {

public static class Builder {
private RuleKey key;
private String name;
private String descriptionUrl;
private String severity;
private RuleType type;
private String pluginKey;
Expand All @@ -128,16 +116,6 @@ public Builder setKey(RuleKey key) {
return this;
}

public Builder setName(String name) {
this.name = StringUtils.trimToNull(name);
return this;
}

public Builder setDescriptionUrl(String descriptionUrl) {
this.descriptionUrl = StringUtils.trimToNull(descriptionUrl);
return this;
}

public Builder setSeverity(String severity) {
this.severity = StringUtils.trimToNull(severity);
return this;
Expand All @@ -148,14 +126,6 @@ public Builder setType(RuleType type) {
return this;
}

public String name() {
return name;
}

public String descriptionUrl() {
return descriptionUrl;
}

public String severity() {
return severity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.issue.Issue;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.RuleType;
Expand Down Expand Up @@ -215,11 +214,9 @@ private DefaultIssue toExternalIssue(LineHashSequence lineHashSeq, ScannerReport

private NewExternalRule toExternalRule(ScannerReport.ExternalIssue reportIssue) {
NewExternalRule.Builder builder = new NewExternalRule.Builder()
.setDescriptionUrl(StringUtils.stripToNull(reportIssue.getDescriptionUrl()))
.setType(toRuleType(reportIssue.getType()))
.setKey(RuleKey.of(RuleKey.EXTERNAL_RULE_REPO_PREFIX + reportIssue.getRuleRepository(), reportIssue.getRuleKey()))
.setPluginKey(reportIssue.getRuleRepository())
.setName(reportIssue.getRuleTitle());
.setPluginKey(reportIssue.getRuleRepository());

if (reportIssue.getSeverity() != Severity.UNSET_SEVERITY) {
builder.setSeverity(reportIssue.getSeverity().name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public Rule persistAndIndex(DbSession dbSession, NewExternalRule external) {
.setPluginKey(external.getPluginKey())
.setIsExternal(external.isExternal())
.setName(external.getName())
.setDescriptionURL(external.getDescriptionUrl())
.setType(external.getType())
.setScope(ALL)
.setStatus(RuleStatus.READY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,39 +33,30 @@ public class NewExternalRuleTest {
@Test
public void should_build_new_external_rule() {
NewExternalRule.Builder builder = new NewExternalRule.Builder()
.setDescriptionUrl("url")
.setKey(RuleKey.of("repo", "rule"))
.setPluginKey("repo")
.setName("name")
.setSeverity("MAJOR")
.setType(RuleType.BUG);

assertThat(builder.descriptionUrl()).isEqualTo("url");
assertThat(builder.name()).isEqualTo("name");
assertThat(builder.severity()).isEqualTo("MAJOR");
assertThat(builder.type()).isEqualTo(RuleType.BUG);
assertThat(builder.descriptionUrl()).isEqualTo("url");

NewExternalRule rule = builder.build();

assertThat(rule.getDescriptionUrl()).isEqualTo("url");
assertThat(rule.getName()).isEqualTo("name");
assertThat(rule.getName()).isEqualTo("repo:rule");
assertThat(rule.getPluginKey()).isEqualTo("repo");
assertThat(rule.getSeverity()).isEqualTo("MAJOR");
assertThat(rule.getType()).isEqualTo(RuleType.BUG);
assertThat(rule.getDescriptionUrl()).isEqualTo("url");
}

@Test
public void fail_if_name_is_not_set() {
public void fail_if_type_is_not_set() {
exception.expect(IllegalStateException.class);
exception.expectMessage("'name' not expected to be empty for an external rule");
exception.expectMessage("'type' not expected to be null for an external rule");

new NewExternalRule.Builder()
.setDescriptionUrl("url")
.setKey(RuleKey.of("repo", "rule"))
.setSeverity("MAJOR")
.setType(RuleType.BUG)
.build();
}

Expand All @@ -75,8 +66,6 @@ public void fail_if_rule_key_is_not_set() {
exception.expectMessage("'key' not expected to be null for an external rule");

new NewExternalRule.Builder()
.setDescriptionUrl("url")
.setName("name")
.setSeverity("MAJOR")
.setType(RuleType.BUG)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.util.List;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rules.RuleType;
Expand All @@ -39,9 +37,7 @@
import org.sonar.db.rule.RuleDefinitionDto;
import org.sonar.db.rule.RuleDto;
import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
import org.sonar.server.es.EsTester;
import org.sonar.server.rule.ExternalRuleCreator;
import org.sonar.server.rule.index.RuleIndexDefinition;
import org.sonar.server.rule.index.RuleIndexer;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -278,15 +274,12 @@ public void accept_new_externally_defined_Rules() {
underTest.insertNewExternalRuleIfAbsent(ruleKey, () -> new NewExternalRule.Builder()
.setKey(ruleKey)
.setPluginKey("eslint")
.setName("disallow assignment operators in conditional statements (no-cond-assign)")
.setDescriptionUrl("https://eslint.org/docs/rules/no-cond-assign")
.setSeverity(BLOCKER)
.setType(BUG)
.build());

assertThat(underTest.getByKey(ruleKey)).isNotNull();
assertThat(underTest.getByKey(ruleKey).getPluginKey()).isEqualTo("eslint");
assertThat(underTest.getByKey(ruleKey).getName()).isEqualTo("disallow assignment operators in conditional statements (no-cond-assign)");
assertThat(underTest.getByKey(ruleKey).getType()).isEqualTo(BUG);

RuleDao ruleDao = dbClient.ruleDao();
Expand All @@ -302,8 +295,6 @@ public void persist_new_externally_defined_Rules() {
underTest.insertNewExternalRuleIfAbsent(ruleKey, () -> new NewExternalRule.Builder()
.setKey(ruleKey)
.setPluginKey("eslint")
.setName("disallow assignment operators in conditional statements (no-cond-assign)")
.setDescriptionUrl("https://eslint.org/docs/rules/no-cond-assign")
.setSeverity(BLOCKER)
.setType(BUG)
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ public void persist_and_index_new_external_rules() {
ruleRepository.insertNewExternalRuleIfAbsent(ruleKey, () -> new NewExternalRule.Builder()
.setKey(ruleKey)
.setPluginKey("eslint")
.setName("disallow assignment operators in conditional statements (no-cond-assign)")
.setDescriptionUrl("https://eslint.org/docs/rules/no-cond-assign")
.setSeverity(BLOCKER)
.setType(BUG)
.build());
Expand All @@ -98,8 +96,7 @@ public void persist_and_index_new_external_rules() {
assertThat(reloaded.isExternal()).isTrue();
assertThat(reloaded.getType()).isEqualTo(2);
assertThat(reloaded.getSeverity()).isEqualTo(4);
assertThat(reloaded.getDescriptionURL()).isEqualTo("https://eslint.org/docs/rules/no-cond-assign");
assertThat(reloaded.getName()).isEqualTo("disallow assignment operators in conditional statements (no-cond-assign)");
assertThat(reloaded.getName()).isEqualTo("eslint:no-cond-assign");
assertThat(reloaded.getPluginKey()).isEqualTo("eslint");

assertThat(es.countDocuments(RuleIndexDefinition.INDEX_TYPE_RULE)).isEqualTo(1l);
Expand All @@ -113,8 +110,6 @@ public void do_not_persist_existing_external_rules() {
ruleRepository.insertNewExternalRuleIfAbsent(ruleKey, () -> new NewExternalRule.Builder()
.setKey(ruleKey)
.setPluginKey("eslint")
.setName("disallow assignment operators in conditional statements (no-cond-assign)")
.setDescriptionUrl("https://eslint.org/docs/rules/no-cond-assign")
.setSeverity(BLOCKER)
.setType(BUG)
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public void create_external_rule() {
NewExternalRule externalRule = new NewExternalRule.Builder()
.setKey(ruleKey)
.setPluginKey("eslint")
.setName("disallow assignment operators in conditional statements (no-cond-assign)")
.setDescriptionUrl("https://eslint.org/docs/rules/no-cond-assign")
.setSeverity(BLOCKER)
.setType(BUG)
.build();
Expand All @@ -63,7 +61,7 @@ public void create_external_rule() {
assertThat(rule1.getId()).isGreaterThan(0);
assertThat(rule1.getKey()).isEqualTo(ruleKey);
assertThat(rule1.getPluginKey()).isEqualTo("eslint");
assertThat(rule1.getName()).isEqualTo("disallow assignment operators in conditional statements (no-cond-assign)");
assertThat(rule1.getName()).isEqualTo(ruleKey.toString());
assertThat(rule1.getType()).isEqualTo(BUG);

}
Expand Down

0 comments on commit 4754c43

Please sign in to comment.