Fix issue #871 - [service desk] Add option to trim replies send out by

1dev
This commit is contained in:
Robin Shen 2022-08-16 14:55:41 +08:00
parent 2955202e26
commit cbfa6091db
2 changed files with 37 additions and 7 deletions

View File

@ -429,7 +429,8 @@ public class DefaultMailManager implements MailManager {
throw new ExplicitException(errorMessage);
}
checkPermission(from, project, new AccessProject(), user, authorization);
issues.add(openIssue(message, project, from, user, authorization));
issues.add(openIssue(message, serviceDeskSetting.getPreserveBefore(),
project, from, user, authorization));
} else {
throw new ExplicitException("Unable to create issue from email as service desk is not enabled");
}
@ -514,7 +515,8 @@ public class DefaultMailManager implements MailManager {
if (serviceDeskSetting != null) {
checkPermission(from, project, new AccessProject(), user, authorization);
logger.debug("Creating issue via email (project: {})...", project.getPath());
issues.add(openIssue(message, project, from, user, authorization));
issues.add(openIssue(message, serviceDeskSetting.getPreserveBefore(), project,
from, user, authorization));
} else {
throw new ExplicitException("Unable to create issue from email as service desk is not enabled");
}
@ -638,6 +640,11 @@ public class DefaultMailManager implements MailManager {
}
}
return getContent(document);
}
@Nullable
private String getContent(Document document) {
AtomicReference<Node> lastContentNodeRef = new AtomicReference<>(null);
NodeTraversor.traverse(new NodeVisitor() {
@ -691,7 +698,7 @@ public class DefaultMailManager implements MailManager {
user = createUser(author, pullRequest.getProject(), authorization.getAuthorizedRole());
logger.trace("Creating pull request comment on behalf of user '" + user.getName() + "'");
comment.setUser(user);
String content = stripQuotation(sendSetting, readText(pullRequest.getProject(), pullRequest.getUUID(), message));
String content = stripQuotation(sendSetting, readText(pullRequest.getProject(), pullRequest.getUUID(), message, null));
if (content != null) {
comment.setContent("<div class='no-color'>" + content + "</div>");
pullRequestCommentManager.save(comment, receiverEmailAddresses);
@ -707,8 +714,9 @@ public class DefaultMailManager implements MailManager {
return null;
}
private Issue openIssue(Message message, Project project, InternetAddress submitter,
@Nullable User user, @Nullable SenderAuthorization authorization) throws MessagingException, IOException {
private Issue openIssue(Message message, @Nullable String preserveBefore,
Project project, InternetAddress submitter, @Nullable User user,
@Nullable SenderAuthorization authorization) throws MessagingException, IOException {
Issue issue = new Issue();
issue.setProject(project);
if (StringUtils.isNotBlank(message.getSubject()))
@ -721,8 +729,18 @@ public class DefaultMailManager implements MailManager {
issue.setThreadingReference(messageId);
String description = readText(project, issue.getUUID(), message);
if (StringUtils.isNotBlank(description))
issue.setDescription("<div class='no-color'>" + description + "</div>");
if (StringUtils.isNotBlank(description)) {
if (preserveBefore != null)
description = StringUtils.substringBefore(description, preserveBefore);
Document document = HtmlUtils.parse(description);
document.outputSettings().prettyPrint(false);
description = getContent(document);
if (description != null)
issue.setDescription("<div class='no-color'>" + description + "</div>");
}
if (user == null)
user = createUser(submitter, project, authorization.getAuthorizedRole());

View File

@ -38,6 +38,8 @@ public class ServiceDeskSetting implements Serializable {
private List<IssueCreationSetting> issueCreationSettings = new ArrayList<>();
private String preserveBefore;
@Editable(order=100, description="When sender email address can not be mapped to an existing user, "
+ "OneDev will use entries defined here to determine if the sender has permission to "
+ "create issues. For a particular sender, the first matching entry will take "
@ -80,6 +82,16 @@ public class ServiceDeskSetting implements Serializable {
this.issueCreationSettings = issueCreationSettings;
}
@Editable(order=400, name="Marker Line", description="Optionally specify a marker line. Only content before this line "
+ "will be preserved when create issue from email")
public String getPreserveBefore() {
return preserveBefore;
}
public void setPreserveBefore(String preserveBefore) {
this.preserveBefore = preserveBefore;
}
@Nullable
public SenderAuthorization getSenderAuthorization(String senderAddress) {
Matcher matcher = new StringMatcher();