Sanitized Issue Proposals #58
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user