Skip to content

The Unified and Autonomous Development Platform

Improve default notification template

Robin Shenbace7fc14d86
diff --git a/server-core/src/main/java/io/onedev/server/model/support/administration/notificationtemplate/NotificationTemplateSetting.java b/server-core/src/main/java/io/onedev/server/model/support/administration/notificationtemplate/NotificationTemplateSetting.java
index f46cfbdc11..cfe54f1d15 100644
--- a/server-core/src/main/java/io/onedev/server/model/support/administration/notificationtemplate/NotificationTemplateSetting.java
+++ b/server-core/src/main/java/io/onedev/server/model/support/administration/notificationtemplate/NotificationTemplateSetting.java
@@ -33,6 +33,7 @@ public class NotificationTemplateSetting implements Serializable {
+ "<li><code>event:</code> <a href='https://code.onedev.io/projects/onedev-server/blob/main/server-core/src/main/java/io/onedev/server/event/Event.java' target='_blank'>event object</a> triggering the notification"
+ "<li><code>eventBody:</code> string type representing body of the event, for instance, comment made by user"
+ "<li><code>eventUrl:</code> string type representing event detail url"
+ + "<li><code>replyable:</code> boolean type indiciating whether or not topic comment can be created directly by replying the email"
+ "<li><code>unsubscribable:</code> a non-null value indicates that the notification can be unsuscribed. Check <a href='https://code.onedev.io/projects/onedev-server/blob/main/server-core/src/main/java/io/onedev/server/notification/Unsubscribable.java' target='_blank'>document</a> for details";
static {
diff --git a/server-core/src/main/java/io/onedev/server/model/support/administration/notificationtemplate/default-notification-template.html b/server-core/src/main/java/io/onedev/server/model/support/administration/notificationtemplate/default-notification-template.html
index d69866dc85..7c880100bd 100644
--- a/server-core/src/main/java/io/onedev/server/model/support/administration/notificationtemplate/default-notification-template.html
+++ b/server-core/src/main/java/io/onedev/server/model/support/administration/notificationtemplate/default-notification-template.html
@@ -3,24 +3,31 @@
print eventBody
print "<br>"
}
+ if (replyable)
+ print """
+ <div>Reply this email to post comment, or click <a href='${eventUrl}'>this link</a> for details</div>
+ """
+ else
+ print """
+ <div>Click <a href='${eventUrl}'>this link</a> for details</div>
+ """
%>
-<div>Visit <a href='${eventUrl}'>${eventUrl}</a> for details</div>
+
<%
if (unsubscribable != null) {
print """
<div style='border-top:1px solid #EEE; margin-top:1em; padding-top:1em; color:#666; font-size:0.9em;'>You received this as you
are participating or participated previously in this topic.
"""
- if (unsubscribable.getEmailAddress() != null) {
+ if (unsubscribable.getEmailAddress() != null)
print """
Mail to <a href='mailto:${unsubscribable.emailAddress}?subject=Unsubscribe&body=I would like not to get any notifications from this topic'>this address</a> to unsubscribe
</div>
"""
- } else {
+ else
print """
To stop receiving notifications of this topic, please visit detail link above and unwatch it
</div>
"""
- }
}
%>
\ No newline at end of file
diff --git a/server-core/src/main/java/io/onedev/server/notification/AbstractNotificationManager.java b/server-core/src/main/java/io/onedev/server/notification/AbstractNotificationManager.java
index 5364084aef..f7d66127ad 100644
--- a/server-core/src/main/java/io/onedev/server/notification/AbstractNotificationManager.java
+++ b/server-core/src/main/java/io/onedev/server/notification/AbstractNotificationManager.java
@@ -35,7 +35,7 @@ public abstract class AbstractNotificationManager {
this.settingManager = settingManager;
}
- protected String getHtmlBody(Event event, String url, @Nullable Unsubscribable unsubscribable) {
+ protected String getHtmlBody(Event event, String eventUrl, boolean replyable, @Nullable Unsubscribable unsubscribable) {
String eventBody = null;
if (event instanceof MarkdownAware) {
eventBody = ((MarkdownAware) event).getMarkdown();
@@ -54,7 +54,8 @@ public abstract class AbstractNotificationManager {
bindings.put("event", event);
bindings.put("eventBody", eventBody);
bindings.put("unsubscribable", unsubscribable);
- bindings.put("eventUrl", url);
+ bindings.put("replyable", replyable);
+ bindings.put("eventUrl", eventUrl);
if (event instanceof IssueEvent) {
template = StringUtils.join(settingManager.getNotificationTemplateSetting().getIssueNotificationTemplate(), "\n");
@@ -83,20 +84,21 @@ public abstract class AbstractNotificationManager {
}
}
- protected String getTextBody(Event event, String url, @Nullable Unsubscribable unsubscribable) {
+ protected String getTextBody(Event event, String eventUrl, boolean replyable, @Nullable Unsubscribable unsubscribable) {
String textBody = null;
- if (event instanceof MarkdownAware) {
- String markdown = ((MarkdownAware) event).getMarkdown();
- if (markdown != null) {
- textBody = String.format(""
- + "%s"
- + "\n"
- + "Visit %s for details",
- markdown, url);
- }
- }
- if (textBody == null)
- textBody = String.format("Visit %s for details", url);
+ if (event instanceof MarkdownAware)
+ textBody = ((MarkdownAware) event).getMarkdown();
+
+ if (textBody != null)
+ textBody += "\n";
+ else
+ textBody = "";
+
+ if (replyable)
+ textBody += "Reply this email to post comment, or visit " + eventUrl + " for details";
+ else
+ textBody += "Visit " + eventUrl + " for details";
+
if (unsubscribable != null) {
textBody += "\n\n---------------------------------------------\nYou received this as you "
+ "are participating or participated previously in this topic. ";
diff --git a/server-core/src/main/java/io/onedev/server/notification/BuildNotificationManager.java b/server-core/src/main/java/io/onedev/server/notification/BuildNotificationManager.java
index 1e8783aba6..f205e39236 100644
--- a/server-core/src/main/java/io/onedev/server/notification/BuildNotificationManager.java
+++ b/server-core/src/main/java/io/onedev/server/notification/BuildNotificationManager.java
@@ -73,8 +73,8 @@ public class BuildNotificationManager extends AbstractNotificationManager {
}
String url = urlManager.urlFor(build);
String threadingReferences = build.getProject().getName() + "-build" + build.getNumber() + "@onedev";
- String htmlBody = getHtmlBody(event, url, null);
- String textBody = getTextBody(event, url, null);
+ String htmlBody = getHtmlBody(event, url, false, null);
+ String textBody = getTextBody(event, url, false, null);
mailManager.sendMailAsync(Lists.newArrayList(), emails, subject, htmlBody,
textBody, null, threadingReferences);
}
diff --git a/server-core/src/main/java/io/onedev/server/notification/CodeCommentNotificationManager.java b/server-core/src/main/java/io/onedev/server/notification/CodeCommentNotificationManager.java
index a7674f0344..aba28ec5b9 100644
--- a/server-core/src/main/java/io/onedev/server/notification/CodeCommentNotificationManager.java
+++ b/server-core/src/main/java/io/onedev/server/notification/CodeCommentNotificationManager.java
@@ -62,7 +62,8 @@ public class CodeCommentNotificationManager extends AbstractNotificationManager
String threadingReferences = event.getComment().getProject().getName()
+ "-codecomment" + event.getComment().getId() + "@onedev";
mailManager.sendMailAsync(Sets.newHashSet(user.getEmail()), Lists.newArrayList(), subject,
- getHtmlBody(event, url, null), getTextBody(event, url, null), null, threadingReferences);
+ getHtmlBody(event, url, false, null), getTextBody(event, url, false, null), null,
+ threadingReferences);
}
}
}
diff --git a/server-core/src/main/java/io/onedev/server/notification/CommitNotificationManager.java b/server-core/src/main/java/io/onedev/server/notification/CommitNotificationManager.java
index 7bd0ac12f6..d5a2836e99 100644
--- a/server-core/src/main/java/io/onedev/server/notification/CommitNotificationManager.java
+++ b/server-core/src/main/java/io/onedev/server/notification/CommitNotificationManager.java
@@ -117,7 +117,8 @@ public class CommitNotificationManager extends AbstractNotificationManager {
String url = urlManager.urlFor(project, commit);
mailManager.sendMailAsync(Lists.newArrayList(), notifyEmails, subject,
- getHtmlBody(event, url, null), getTextBody(event, url, null), null, null);
+ getHtmlBody(event, url, false, null), getTextBody(event, url, false, null),
+ null, null);
}
}
}
diff --git a/server-core/src/main/java/io/onedev/server/notification/DefaultMailManager.java b/server-core/src/main/java/io/onedev/server/notification/DefaultMailManager.java
index d7e6857dcc..723e6d8374 100644
--- a/server-core/src/main/java/io/onedev/server/notification/DefaultMailManager.java
+++ b/server-core/src/main/java/io/onedev/server/notification/DefaultMailManager.java
@@ -727,7 +727,7 @@ public class DefaultMailManager implements MailManager {
@Override
public String getReplyAddress(Issue issue) {
MailSetting mailSetting = settingManager.getMailSetting();
- if (mailSetting != null) {
+ if (mailSetting != null && mailSetting.getReceiveMailSetting() != null) {
EmailAddress systemAddress = EmailAddress.parse(mailSetting.getEmailAddress());
return systemAddress.getPrefix() + "+" + issue.getProject().getName() + "~issue" + issue.getNumber()
+ "@" + systemAddress.getDomain();
@@ -739,7 +739,7 @@ public class DefaultMailManager implements MailManager {
@Override
public String getReplyAddress(PullRequest request) {
MailSetting mailSetting = settingManager.getMailSetting();
- if (mailSetting != null) {
+ if (mailSetting != null && mailSetting.getReceiveMailSetting() != null) {
EmailAddress systemAddress = EmailAddress.parse(mailSetting.getEmailAddress());
return systemAddress.getPrefix() + "+" + request.getProject().getName() + "~pullrequest" + request.getNumber()
+ "@" + systemAddress.getDomain();
diff --git a/server-core/src/main/java/io/onedev/server/notification/IssueNotificationManager.java b/server-core/src/main/java/io/onedev/server/notification/IssueNotificationManager.java
index 70ef282303..9a433ffdc9 100644
--- a/server-core/src/main/java/io/onedev/server/notification/IssueNotificationManager.java
+++ b/server-core/src/main/java/io/onedev/server/notification/IssueNotificationManager.java
@@ -144,6 +144,7 @@ public class IssueNotificationManager extends AbstractNotificationManager {
Map<String, Collection<User>> newUsers = event.getNewUsers();
String replyAddress = mailManager.getReplyAddress(issue);
+ boolean replyable = replyAddress != null;
String threadingReferences = issue.getThreadingReference();
if (threadingReferences == null)
threadingReferences = issue.getUUID() + "@onedev";
@@ -156,7 +157,7 @@ public class IssueNotificationManager extends AbstractNotificationManager {
.map(it->it.getEmail())
.collect(Collectors.toSet());
mailManager.sendMailAsync(emails, Lists.newArrayList(), subject,
- getHtmlBody(event, url, null), getTextBody(event, url, null),
+ getHtmlBody(event, url, replyable, null), getTextBody(event, url, replyable, null),
replyAddress, threadingReferences);
for (User member: entry.getValue().getMembers())
@@ -173,7 +174,7 @@ public class IssueNotificationManager extends AbstractNotificationManager {
.map(it->it.getEmail())
.collect(Collectors.toSet());
mailManager.sendMailAsync(emails, Lists.newArrayList(), subject,
- getHtmlBody(event, url, null), getTextBody(event, url, null),
+ getHtmlBody(event, url, replyable, null), getTextBody(event, url, replyable, null),
replyAddress, threadingReferences);
for (User each: entry.getValue())
@@ -238,8 +239,8 @@ public class IssueNotificationManager extends AbstractNotificationManager {
subject = "[" + issue.getState() + "] " + subject;
String unsubscribeAddress = mailManager.getUnsubscribeAddress(issue);
- String htmlBody = getHtmlBody(event, url, new Unsubscribable(unsubscribeAddress));
- String textBody = getTextBody(event, url, new Unsubscribable(unsubscribeAddress));
+ String htmlBody = getHtmlBody(event, url, replyable, new Unsubscribable(unsubscribeAddress));
+ String textBody = getTextBody(event, url, replyable, new Unsubscribable(unsubscribeAddress));
mailManager.sendMailAsync(
mentionedUsers.stream().map(User::getEmail).collect(Collectors.toList()),
ccUsers.stream().map(User::getEmail).collect(Collectors.toList()),
diff --git a/server-core/src/main/java/io/onedev/server/notification/PullRequestNotificationManager.java b/server-core/src/main/java/io/onedev/server/notification/PullRequestNotificationManager.java
index 3d0b232d97..1d7eacc7d9 100644
--- a/server-core/src/main/java/io/onedev/server/notification/PullRequestNotificationManager.java
+++ b/server-core/src/main/java/io/onedev/server/notification/PullRequestNotificationManager.java
@@ -177,6 +177,7 @@ public class PullRequestNotificationManager extends AbstractNotificationManager
}
String replyAddress = mailManager.getReplyAddress(request);
+ boolean replyable = replyAddress != null;
String threadingReferences = getThreadingReferences(request);
if (event instanceof PullRequestChangeEvent
&& request.getSubmitter() != null
@@ -193,8 +194,8 @@ public class PullRequestNotificationManager extends AbstractNotificationManager
if (subject != null) {
subject = "[" + getState(request) + "] " + subject;
mailManager.sendMailAsync(Lists.newArrayList(request.getSubmitter().getEmail()),
- Lists.newArrayList(), subject, getHtmlBody(event, url, null),
- getTextBody(event, url, null), replyAddress, threadingReferences);
+ Lists.newArrayList(), subject, getHtmlBody(event, url, replyable, null),
+ getTextBody(event, url, replyable, null), replyAddress, threadingReferences);
notifiedUsers.add(request.getSubmitter());
}
}
@@ -260,8 +261,8 @@ public class PullRequestNotificationManager extends AbstractNotificationManager
String unsubscribeAddress = mailManager.getUnsubscribeAddress(request);
subject = "[" + getState(request) + "] " + subject;
- String htmlBody = getHtmlBody(event, url, new Unsubscribable(unsubscribeAddress));
- String textBody = getTextBody(event, url, new Unsubscribable(unsubscribeAddress));
+ String htmlBody = getHtmlBody(event, url, replyable, new Unsubscribable(unsubscribeAddress));
+ String textBody = getTextBody(event, url, replyable, new Unsubscribable(unsubscribeAddress));
mailManager.sendMailAsync(
mentionedUsers.stream().map(User::getEmail).collect(Collectors.toList()),
ccUsers.stream().map(User::getEmail).collect(Collectors.toList()),
@@ -297,7 +298,8 @@ public class PullRequestNotificationManager extends AbstractNotificationManager
getState(request), request.getNumberAndTitle());
String replyAddress = mailManager.getReplyAddress(request);
mailManager.sendMailAsync(Lists.newArrayList(review.getUser().getEmail()), Lists.newArrayList(),
- subject, getHtmlBody(event, url, null), getTextBody(event, url, null), replyAddress,
+ subject, getHtmlBody(event, url, replyAddress != null, null),
+ getTextBody(event, url, replyAddress != null, null), replyAddress,
getThreadingReferences(request));
}
} else if (event.getEntity() instanceof PullRequestAssignment) {
@@ -310,8 +312,9 @@ public class PullRequestNotificationManager extends AbstractNotificationManager
getState(request), request.getNumberAndTitle());
String replyAddress = mailManager.getReplyAddress(request);
mailManager.sendMailAsync(Lists.newArrayList(assignment.getUser().getEmail()), Lists.newArrayList(),
- subject, getHtmlBody(event, url, null), getTextBody(event, url, null), replyAddress,
- getThreadingReferences(request));
+ subject, getHtmlBody(event, url, replyAddress != null, null),
+ getTextBody(event, url, replyAddress != null, null),
+ replyAddress, getThreadingReferences(request));
}
}
}