import org.sonar.api.utils.Durations;
import org.sonar.db.DbClient;
-import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.user.index.UserIndex;
import static org.sonar.server.issue.notification.AbstractNewIssuesEmailTemplate.FIELD_ASSIGNEE;
public static final String MY_NEW_ISSUES_NOTIF_TYPE = "my-new-issues";
- MyNewIssuesNotification(UserIndex userIndex, RuleIndex ruleIndex, DbClient dbClient, Durations durations) {
- super(MY_NEW_ISSUES_NOTIF_TYPE, userIndex, ruleIndex, dbClient, durations);
+ MyNewIssuesNotification(UserIndex userIndex, DbClient dbClient, Durations durations) {
+ super(MY_NEW_ISSUES_NOTIF_TYPE, userIndex, dbClient, durations);
}
public MyNewIssuesNotification setAssignee(String assignee) {
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;
+import org.sonar.db.rule.RuleDto;
import org.sonar.server.issue.notification.NewIssuesStatistics.Metric;
-import org.sonar.server.rule.Rule;
-import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.user.index.UserDoc;
import org.sonar.server.user.index.UserIndex;
private static final String DOT = ".";
private final transient UserIndex userIndex;
- private final transient RuleIndex ruleIndex;
private final transient DbClient dbClient;
private final transient Durations durations;
- NewIssuesNotification(UserIndex userIndex, RuleIndex ruleIndex, DbClient dbClient, Durations durations) {
- this(TYPE, userIndex, ruleIndex, dbClient, durations);
+ NewIssuesNotification(UserIndex userIndex, DbClient dbClient, Durations durations) {
+ this(TYPE, userIndex, dbClient, durations);
}
- protected NewIssuesNotification(String type, UserIndex userIndex, RuleIndex ruleIndex, DbClient dbClient, Durations durations) {
+ protected NewIssuesNotification(String type, UserIndex userIndex, DbClient dbClient, Durations durations) {
super(type);
this.userIndex = userIndex;
- this.ruleIndex = ruleIndex;
this.dbClient = dbClient;
this.durations = durations;
}
public NewIssuesNotification setStatistics(String projectName, NewIssuesStatistics.Stats stats) {
setDefaultMessage(stats.countForMetric(SEVERITY) + " new issues on " + projectName + ".\n");
- setSeverityStatistics(stats);
- setAssigneesStatistics(stats);
- setTagsStatistics(stats);
- setComponentsStatistics(stats);
- setRuleStatistics(stats);
+ DbSession dbSession = dbClient.openSession(false);
+ try {
+ setSeverityStatistics(stats);
+ setAssigneesStatistics(stats);
+ setTagsStatistics(stats);
+ setComponentsStatistics(dbSession, stats);
+ setRuleStatistics(dbSession, stats);
+ } finally {
+ MyBatis.closeQuietly(dbSession);
+ }
return this;
}
- protected void setRuleStatistics(NewIssuesStatistics.Stats stats) {
+ protected void setRuleStatistics(DbSession dbSession, NewIssuesStatistics.Stats stats) {
Metric metric = Metric.RULE;
List<Multiset.Entry<String>> metricStats = stats.statsForMetric(metric);
for (int i = 0; i < 5 && i < metricStats.size(); i++) {
String ruleKey = metricStats.get(i).getElement();
- Rule rule = ruleIndex.getByKey(RuleKey.parse(ruleKey));
- String name = rule.name() + " (" + rule.language() + ")";
+ RuleDto rule = dbClient.ruleDao().selectOrFailByKey(dbSession, RuleKey.parse(ruleKey));
+ String name = rule.getName() + " (" + rule.getLanguage() + ")";
setFieldValue(metric + DOT + (i + 1) + LABEL, name);
setFieldValue(metric + DOT + (i + 1) + COUNT, String.valueOf(metricStats.get(i).getCount()));
}
}
- protected void setComponentsStatistics(NewIssuesStatistics.Stats stats) {
+ protected void setComponentsStatistics(DbSession dbSession, NewIssuesStatistics.Stats stats) {
Metric metric = Metric.COMPONENT;
List<Multiset.Entry<String>> componentStats = stats.statsForMetric(metric);
- DbSession dbSession = dbClient.openSession(false);
- try {
- for (int i = 0; i < 5 && i < componentStats.size(); i++) {
- String uuid = componentStats.get(i).getElement();
- String componentName = dbClient.componentDao().selectOrFailByUuid(dbSession, uuid).name();
- setFieldValue(metric + DOT + (i + 1) + LABEL, componentName);
- setFieldValue(metric + DOT + (i + 1) + COUNT, String.valueOf(componentStats.get(i).getCount()));
- }
- } finally {
- MyBatis.closeQuietly(dbSession);
+ for (int i = 0; i < 5 && i < componentStats.size(); i++) {
+ String uuid = componentStats.get(i).getElement();
+ String componentName = dbClient.componentDao().selectOrFailByUuid(dbSession, uuid).name();
+ setFieldValue(metric + DOT + (i + 1) + LABEL, componentName);
+ setFieldValue(metric + DOT + (i + 1) + COUNT, String.valueOf(componentStats.get(i).getCount()));
}
}
import org.sonar.api.server.ServerSide;
import org.sonar.api.utils.Durations;
import org.sonar.db.DbClient;
-import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.user.index.UserIndex;
@ServerSide
public class NewIssuesNotificationFactory {
private final UserIndex userIndex;
- private final RuleIndex ruleIndex;
private final DbClient dbClient;
private final Durations durations;
- public NewIssuesNotificationFactory(UserIndex userIndex, RuleIndex ruleIndex, DbClient dbClient, Durations durations) {
+ public NewIssuesNotificationFactory(UserIndex userIndex, DbClient dbClient, Durations durations) {
this.userIndex = userIndex;
- this.ruleIndex = ruleIndex;
this.dbClient = dbClient;
this.durations = durations;
}
public MyNewIssuesNotification newMyNewIssuesNotification() {
- return new MyNewIssuesNotification(userIndex, ruleIndex, dbClient, durations);
+ return new MyNewIssuesNotification(userIndex, dbClient, durations);
}
public NewIssuesNotification newNewIssuesNotication() {
- return new NewIssuesNotification(userIndex, ruleIndex, dbClient, durations);
+ return new NewIssuesNotification(userIndex, dbClient, durations);
}
}
import org.junit.Test;
import org.sonar.api.utils.Durations;
import org.sonar.db.DbClient;
-import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.user.index.UserIndex;
import static org.assertj.core.api.Assertions.assertThat;
public class MyNewIssuesNotificationTest {
- MyNewIssuesNotification underTest = new MyNewIssuesNotification(mock(UserIndex.class), mock(RuleIndex.class), mock(DbClient.class), mock(Durations.class));
+ MyNewIssuesNotification underTest = new MyNewIssuesNotification(mock(UserIndex.class), mock(DbClient.class), mock(Durations.class));
@Test
public void set_assignee() {
*/
package org.sonar.server.issue.notification;
-import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import java.util.Date;
import java.util.Locale;
import org.sonar.core.issue.DefaultIssue;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.server.rule.Rule;
-import org.sonar.server.rule.index.RuleDoc;
-import org.sonar.server.rule.index.RuleIndex;
-import org.sonar.server.rule.index.RuleNormalizer;
+import org.sonar.db.rule.RuleDto;
import org.sonar.server.user.index.UserIndex;
import static org.assertj.core.api.Assertions.assertThat;
NewIssuesStatistics.Stats stats = new NewIssuesStatistics.Stats();
UserIndex userIndex = mock(UserIndex.class);
- RuleIndex ruleIndex = mock(RuleIndex.class);
DbClient dbClient = mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS);
Durations durations = mock(Durations.class);
- NewIssuesNotification underTest = new NewIssuesNotification(userIndex, ruleIndex, dbClient, durations);
+ NewIssuesNotification underTest = new NewIssuesNotification(userIndex, dbClient, durations);
@Test
public void set_project() {
addIssueNTimes(newIssue2(), 3);
when(dbClient.componentDao().selectOrFailByUuid(any(DbSession.class), eq("file-uuid")).name()).thenReturn("file-name");
when(dbClient.componentDao().selectOrFailByUuid(any(DbSession.class), eq("directory-uuid")).name()).thenReturn("directory-name");
- when(ruleIndex.getByKey(RuleKey.of("SonarQube", "rule-the-world"))).thenReturn(newRule("Rule the World", "Java"));
- when(ruleIndex.getByKey(RuleKey.of("SonarQube", "rule-the-universe"))).thenReturn(newRule("Rule the Universe", "Clojure"));
+ when(dbClient.ruleDao().selectOrFailByKey(any(DbSession.class), eq(RuleKey.of("SonarQube", "rule-the-world")))).thenReturn(newRule("Rule the World", "Java"));
+ when(dbClient.ruleDao().selectOrFailByKey(any(DbSession.class), eq(RuleKey.of("SonarQube", "rule-the-universe")))).thenReturn(newRule("Rule the Universe", "Clojure"));
underTest.setStatistics("project-long-name", stats);
.setDebt(Duration.create(10L));
}
- private Rule newRule(String name, String language) {
- return new RuleDoc(ImmutableMap.<String, Object>of(
- RuleNormalizer.RuleField.NAME.field(), name,
- RuleNormalizer.RuleField.LANGUAGE.field(), language
- ));
+ private RuleDto newRule(String name, String language) {
+ return new RuleDto()
+ .setName(name)
+ .setLanguage(language);
}
}