Sanitized issue proposals

Fixes #53
This commit is contained in:
Leon Hofmeister 2020-09-26 23:08:32 +02:00
parent f0e645c0ae
commit ecb792050a
Signed by: delvh
GPG Key ID: 3DECE05F6D9A647C
2 changed files with 71 additions and 25 deletions

View File

@ -7,7 +7,6 @@ import javafx.scene.input.InputEvent;
import envoy.client.event.SendEvent;
import envoy.client.util.IssueUtil;
import envoy.data.User;
import envoy.event.IssueProposal;
import dev.kske.eventbus.EventBus;
@ -70,8 +69,9 @@ public final class BugReportPane extends OnlyIfOnlineSettingsPane {
submitReportButton.setDisable(true);
submitReportButton.setOnAction(e -> {
EventBus.getInstance()
.dispatch(new SendEvent(new IssueProposal(titleTextField.getText(),
IssueUtil.sanitizeIssueDescription(errorDetailArea.getText(), showUsernameInBugReport.isSelected() ? user.getName() : null),
.dispatch(new SendEvent(IssueUtil.createIssueProposal(titleTextField.getText(),
errorDetailArea.getText(),
showUsernameInBugReport.isSelected() ? user.getName() : null,
true)));
});
getChildren().add(submitReportButton);

View File

@ -1,40 +1,86 @@
package envoy.client.util;
import java.util.regex.Pattern;
import envoy.event.IssueProposal;
/**
* Provides methods to handle outgoing issues.
* <p>
* Project: <strong>client</strong><br>
* File: <strong>IssueUtil.java</strong><br>
* Created: <strong>20.08.2020</strong><br>
*
* @author Leon Hofmeister
* @author Kai S. K. Engelbart
* @since Envoy Client v0.2-beta
*/
public final class IssueUtil {
/**
*
* @since Envoy Client v0.2-beta
*/
private static final Pattern removeBackslashes = Pattern.compile("\\\\");
private static final Pattern escapeQuotes = Pattern.compile("\"");
private IssueUtil() {}
/**
* Performs actions to ensure the description of an issue will be displayed as
* intended by the user.
* Creates a new {@code IssueProposal} from the given data.
*
* @param rawDescription the description to sanitize
* @param username the user who submitted the issue. Should be
* {@code null} if he does not want to be named.
* @return the sanitized description
* @param title the proposed title of the issue
* @param description the proposed description of the issue
* @param username the user who submitted the issue. Should be
* {@code null} if he does not want to be named. * @param
* isBug
* @param isBug whether this issue is a bug or a feature
* @return a sanitized IssueProposal that should not fail to be sent
* @since Envoy Client v0.2-beta
*/
public static String sanitizeIssueDescription(String rawDescription, String username) {
// Appending the submitter name, if this option was enabled
rawDescription += username != null
? (rawDescription.endsWith("\n") || rawDescription.endsWith("<br>") ? "" : "<br>") + String.format("Submitted by user %s.", username)
: "";
// Markdown does not support "normal" line breaks. It uses "<br>"
rawDescription = rawDescription.replaceAll(System.getProperty("line.separator", "\r?\n"), "<br>");
return rawDescription;
public static IssueProposal createIssueProposal(String title, String description, String username, boolean isBug) {
title = sanitizeIssueTitle(title);
description = sanitizeIssueDescription(description, username);
return new IssueProposal(title, description, isBug);
}
/**
* Escapes quotes and removes backslashes for a suggested issue title.
*
* @param title the title to sanitize
* @return the sanitized title
* @since Envoy Client v0.2-beta
*/
private static String sanitizeIssueTitle(String title) {
// Remove ALL backslashes as they are only error prone
title = removeBackslashes.matcher(title).replaceAll("");
// Escape quotes
title = escapeQuotes.matcher(title).replaceAll("\\\\\"");
return title;
}
/**
* Normalizes line breaks, <br>
* removes all backslashes, <br>
* escapes quotes and<br>
* appends the user name to the issue description if requested.
*
* @param description the description to sanitize
* @param username the user who submitted the issue. Should be
* {@code null} if he does not want to be named.
* @return the sanitized description
* @since Envoy Client v0.2-beta
* @apiNote the String returned might not be sanitized in case multiple
* backslashes are preceding a quote.
*/
public static String sanitizeIssueDescription(String description, String username) {
// Trim and replace line breaks by <br> tags
description = description.trim().replaceAll(System.getProperty("line.separator"), "<br>");
// Append user name if requested
if (username != null) description += String.format("<br>Submitted by user %s.", username);
// Remove ALL backslashes as they are only error prone
description = removeBackslashes.matcher(description).replaceAll("");
// Escape all quotes to avoid prematurely ending the string
description = escapeQuotes.matcher(description).replaceAll("\\\\\"");
return description;
}
}