Skip to content

Commit

Permalink
notify users when they are automatically assigned to issues - SONAR-6106
Browse files Browse the repository at this point in the history
  • Loading branch information
teryk committed Mar 10, 2015
1 parent d11e0cd commit 4411bd5
Show file tree
Hide file tree
Showing 24 changed files with 962 additions and 315 deletions.
Expand Up @@ -36,7 +36,6 @@
import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.exceptions.NotFoundException;


import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;

import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
Expand Down
Expand Up @@ -22,18 +22,16 @@
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import org.sonar.api.issue.internal.DefaultIssue; import org.sonar.api.issue.internal.DefaultIssue;
import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Qualifiers;
import org.sonar.api.utils.Durations;
import org.sonar.core.component.ComponentDto; import org.sonar.core.component.ComponentDto;
import org.sonar.server.computation.ComputationContext; import org.sonar.server.computation.ComputationContext;
import org.sonar.server.computation.issue.IssueCache; import org.sonar.server.computation.issue.IssueCache;
import org.sonar.server.computation.issue.RuleCache; import org.sonar.server.computation.issue.RuleCache;
import org.sonar.server.issue.notification.IssueChangeNotification; import org.sonar.server.issue.notification.*;
import org.sonar.server.issue.notification.NewIssuesNotification;
import org.sonar.server.issue.notification.NewIssuesStatistics;
import org.sonar.server.notifications.NotificationService; import org.sonar.server.notifications.NotificationService;
import org.sonar.server.util.CloseableIterator; import org.sonar.server.util.CloseableIterator;


import java.util.Date; import java.util.Date;
import java.util.Map;
import java.util.Set; import java.util.Set;


/** /**
Expand All @@ -45,18 +43,18 @@ public class SendIssueNotificationsStep implements ComputationStep {
/** /**
* Types of the notifications sent by this step * Types of the notifications sent by this step
*/ */
static final Set<String> NOTIF_TYPES = ImmutableSet.of(IssueChangeNotification.TYPE, NewIssuesNotification.TYPE); static final Set<String> NOTIF_TYPES = ImmutableSet.of(IssueChangeNotification.TYPE, NewIssuesNotification.TYPE, MyNewIssuesNotification.TYPE);


private final IssueCache issueCache; private final IssueCache issueCache;
private final RuleCache rules; private final RuleCache rules;
private final NotificationService service; private final NotificationService service;
private final Durations durations; private NewIssuesNotificationFactory newIssuesNotificationFactory;


public SendIssueNotificationsStep(IssueCache issueCache, RuleCache rules, NotificationService service, Durations durations) { public SendIssueNotificationsStep(IssueCache issueCache, RuleCache rules, NotificationService service, NewIssuesNotificationFactory newIssuesNotificationFactory) {
this.issueCache = issueCache; this.issueCache = issueCache;
this.rules = rules; this.rules = rules;
this.service = service; this.service = service;
this.durations = durations; this.newIssuesNotificationFactory = newIssuesNotificationFactory;
} }


@Override @Override
Expand Down Expand Up @@ -94,15 +92,33 @@ private void doExecute(ComputationContext context) {
sendNewIssuesStatistics(context, newIssuesStats); sendNewIssuesStatistics(context, newIssuesStats);
} }


private void sendNewIssuesStatistics(ComputationContext context, NewIssuesStatistics stats) { private void sendNewIssuesStatistics(ComputationContext context, NewIssuesStatistics statistics) {
if (stats.hasIssues()) { if (statistics.hasIssues()) {
NewIssuesStatistics.Stats globalStatistics = statistics.globalStatistics();
ComponentDto project = context.getProject(); ComponentDto project = context.getProject();
NewIssuesNotification notification = new NewIssuesNotification(); NewIssuesNotification notification = newIssuesNotificationFactory
notification.setProject(project); .newNewIssuesNotication()
notification.setAnalysisDate(new Date(context.getReportMetadata().getAnalysisDate())); .setProject(project)
notification.setStatistics(project, stats); .setAnalysisDate(new Date(context.getReportMetadata().getAnalysisDate()))
notification.setDebt(durations.encode(stats.debt())); .setStatistics(project, globalStatistics)
.setDebt(globalStatistics.debt());
service.deliver(notification); service.deliver(notification);

// send email to each user having issues
for (Map.Entry<String, NewIssuesStatistics.Stats> assigneeAndStatisticsTuple : statistics.assigneesStatistics().entrySet()) {
String assignee = assigneeAndStatisticsTuple.getKey();
NewIssuesStatistics.Stats assigneeStatistics = assigneeAndStatisticsTuple.getValue();
MyNewIssuesNotification myNewIssuesNotification = newIssuesNotificationFactory
.newMyNewIssuesNotification()
.setAssignee(assignee);
myNewIssuesNotification
.setProject(project)
.setAnalysisDate(new Date(context.getReportMetadata().getAnalysisDate()))
.setStatistics(project, assigneeStatistics)
.setDebt(assigneeStatistics.debt());

service.deliver(myNewIssuesNotification);
}
} }
} }


Expand Down
@@ -0,0 +1,188 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.server.issue.notification;

import com.google.common.collect.Lists;
import org.sonar.api.config.EmailSettings;
import org.sonar.api.i18n.I18n;
import org.sonar.api.notifications.Notification;
import org.sonar.api.rule.Severity;
import org.sonar.api.utils.DateUtils;
import org.sonar.plugins.emailnotifications.api.EmailMessage;
import org.sonar.plugins.emailnotifications.api.EmailTemplate;
import org.sonar.server.issue.notification.NewIssuesStatistics.METRIC;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Iterator;
import java.util.Locale;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* Base class to create emails for new issues
*/
public abstract class AbstractNewIssuesEmailTemplate extends EmailTemplate {

protected static final char NEW_LINE = '\n';
protected static final String TAB = " ";
protected static final String DOT = ".";
protected static final String COUNT = DOT + "count";
protected static final String LABEL = DOT + "label";

static final String FIELD_PROJECT_NAME = "projectName";
static final String FIELD_PROJECT_KEY = "projectKey";
static final String FIELD_PROJECT_DATE = "projectDate";
static final String FIELD_PROJECT_UUID = "projectUuid";
static final String FIELD_ASSIGNEE = "assignee";

protected final EmailSettings settings;
protected final I18n i18n;

public AbstractNewIssuesEmailTemplate(EmailSettings settings, I18n i18n) {
this.settings = settings;
this.i18n = i18n;
}

public static String encode(String toEncode) {
try {
return URLEncoder.encode(toEncode, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Encoding not supported", e);
}
}

@Override
public EmailMessage format(Notification notification) {
if (shouldNotFormat(notification)) {
return null;
}
String projectName = checkNotNull(notification.getFieldValue(FIELD_PROJECT_NAME));

StringBuilder message = new StringBuilder();
message.append("Project: ").append(projectName).append(NEW_LINE).append(NEW_LINE);
appendSeverity(message, notification);
appendAssignees(message, notification);
appendTags(message, notification);
appendComponents(message, notification);
appendFooter(message, notification);

return new EmailMessage()
.setMessageId(notification.getType() + "/" + notification.getFieldValue(FIELD_PROJECT_KEY))
.setSubject(subject(notification, projectName))
.setMessage(message.toString());
}

protected abstract boolean shouldNotFormat(Notification notification);

protected String subject(Notification notification, String projectName) {
return String.format("%s: %s new issues (new debt: %s)",
projectName,
notification.getFieldValue(METRIC.SEVERITY + COUNT),
notification.getFieldValue(METRIC.DEBT + COUNT));
}

private boolean doNotHaveValue(Notification notification, METRIC metric) {
return notification.getFieldValue(metric + DOT + "1" + LABEL) == null;
}

private void genericAppendOfMetric(METRIC metric, String label, StringBuilder message, Notification notification) {
if (doNotHaveValue(notification, metric)) {
return;
}

message
.append(TAB)
.append(label)
.append(NEW_LINE);
int i = 1;
while (notification.getFieldValue(metric + DOT + i + LABEL) != null && i <= 5) {
String name = notification.getFieldValue(metric + DOT + i + LABEL);
message
.append(TAB).append(TAB)
.append(name)
.append(": ")
.append(notification.getFieldValue(metric + DOT + i + COUNT))
.append(NEW_LINE);
i += 1;
}

message.append(NEW_LINE);
}

protected void appendAssignees(StringBuilder message, Notification notification) {
genericAppendOfMetric(METRIC.ASSIGNEE, "Assignees", message, notification);
}

protected void appendComponents(StringBuilder message, Notification notification) {
genericAppendOfMetric(METRIC.COMPONENT, "Most impacted files", message, notification);
}

protected void appendTags(StringBuilder message, Notification notification) {
genericAppendOfMetric(METRIC.TAGS, "Tags", message, notification);
}

protected void appendSeverity(StringBuilder message, Notification notification) {
message
.append(String.format("%s new issues (new debt: %s)",
notification.getFieldValue(METRIC.SEVERITY + COUNT),
notification.getFieldValue(METRIC.DEBT + COUNT)))
.append(NEW_LINE).append(NEW_LINE)
.append(TAB)
.append("Severity")
.append(NEW_LINE)
.append(TAB)
.append(TAB);

for (Iterator<String> severityIterator = Lists.reverse(Severity.ALL).iterator(); severityIterator.hasNext();) {
String severity = severityIterator.next();
String severityLabel = i18n.message(getLocale(), "severity." + severity, severity);
message.append(severityLabel).append(": ").append(notification.getFieldValue(METRIC.SEVERITY + DOT + severity + COUNT));
if (severityIterator.hasNext()) {
message.append(TAB);
}
}

message
.append(NEW_LINE)
.append(NEW_LINE);
}

protected void appendFooter(StringBuilder message, Notification notification) {
String projectUuid = notification.getFieldValue(FIELD_PROJECT_UUID);
String dateString = notification.getFieldValue(FIELD_PROJECT_DATE);
if (projectUuid != null && dateString != null) {
Date date = DateUtils.parseDateTime(dateString);
String url = String.format("%s/issues/search#projectUuids=%s|createdAt=%s",
settings.getServerBaseURL(), encode(projectUuid), encode(DateUtils.formatDateTime(date)));
message
.append("See it in SonarQube: ")
.append(url)
.append(NEW_LINE);
}
}

private Locale getLocale() {
return Locale.ENGLISH;
}

}
@@ -0,0 +1,72 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.server.issue.notification;

import org.sonar.api.config.EmailSettings;
import org.sonar.api.i18n.I18n;
import org.sonar.api.notifications.Notification;
import org.sonar.api.utils.DateUtils;
import org.sonar.server.issue.notification.NewIssuesStatistics.METRIC;

import java.util.Date;

/**
* Creates email message for notification "my-new-issues".
*/
public class MyNewIssuesEmailTemplate extends AbstractNewIssuesEmailTemplate {

public MyNewIssuesEmailTemplate(EmailSettings settings, I18n i18n) {
super(settings, i18n);
}

@Override
protected boolean shouldNotFormat(Notification notification) {
return !MyNewIssuesNotification.TYPE.equals(notification.getType());
}

@Override
protected void appendAssignees(StringBuilder message, Notification notification) {
// do nothing as we don't want to print assignees, it's a personalized email for one person
}

@Override
protected String subject(Notification notification, String projectName) {
return String.format("You have %s new issues on project %s",
notification.getFieldValue(METRIC.SEVERITY + COUNT),
projectName);
}

@Override
protected void appendFooter(StringBuilder message, Notification notification) {
String projectUuid = notification.getFieldValue(FIELD_PROJECT_UUID);
String dateString = notification.getFieldValue(FIELD_PROJECT_DATE);
String assignee = notification.getFieldValue(FIELD_ASSIGNEE);
if (projectUuid != null && dateString != null && assignee != null) {
Date date = DateUtils.parseDateTime(dateString);
String url = String.format("%s/issues/search#projectUuids=%s|assignees=%s|createdAt=%s",
settings.getServerBaseURL(),
encode(projectUuid),
encode(assignee),
encode(DateUtils.formatDateTime(date)));
message.append("See it in SonarQube: ").append(url).append(NEW_LINE);
}
}
}
@@ -0,0 +1,42 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.server.issue.notification;

import org.sonar.api.utils.Durations;
import org.sonar.server.db.DbClient;
import org.sonar.server.user.index.UserIndex;

import static org.sonar.server.issue.notification.AbstractNewIssuesEmailTemplate.FIELD_ASSIGNEE;

public class MyNewIssuesNotification extends NewIssuesNotification {

public static final String TYPE = "my-new-issues";

MyNewIssuesNotification(UserIndex userIndex, DbClient dbClient, Durations durations) {
super(TYPE, userIndex, dbClient, durations);
}

public MyNewIssuesNotification setAssignee(String assignee) {
setFieldValue(FIELD_ASSIGNEE, assignee);

return this;
}
}

0 comments on commit 4411bd5

Please sign in to comment.