From 7c376b8428376eed39e33ce64afed85c3a2b7caf Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Sat, 8 May 2021 15:16:42 +0300 Subject: [PATCH 1/2] Fixed issue with too long URI for generated bug report by diagnostic feature --- resources/META-INF/plugin.xml | 4 +- .../DefaultErrorReportSubmitter.java | 11 +-- .../github/GitHubNewIssueBodyBuilderUtil.java | 78 +++++++++++++++++++ .../github/GitHubNewIssueUrlBuilderUtil.java | 50 +++++++++++- 4 files changed, 129 insertions(+), 14 deletions(-) diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index daee1b841..c46e50242 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -270,8 +270,8 @@ - - + + diff --git a/src/com/magento/idea/magento2plugin/project/diagnostic/DefaultErrorReportSubmitter.java b/src/com/magento/idea/magento2plugin/project/diagnostic/DefaultErrorReportSubmitter.java index 577e36393..2701008d0 100644 --- a/src/com/magento/idea/magento2plugin/project/diagnostic/DefaultErrorReportSubmitter.java +++ b/src/com/magento/idea/magento2plugin/project/diagnostic/DefaultErrorReportSubmitter.java @@ -17,7 +17,6 @@ import com.intellij.openapi.util.NlsActions; import com.intellij.util.Consumer; import com.magento.idea.magento2plugin.bundles.CommonBundle; -import com.magento.idea.magento2plugin.project.diagnostic.github.GitHubNewIssueBodyBuilderUtil; import com.magento.idea.magento2plugin.project.diagnostic.github.GitHubNewIssueUrlBuilderUtil; import java.awt.Component; import java.time.LocalDateTime; @@ -70,16 +69,12 @@ public boolean submit( stackTrace.append(event.getThrowableText()).append("\r\n"); } - final String bugReportBody = GitHubNewIssueBodyBuilderUtil.buildNewBugReportBody( - project, - additionalInfo == null ? DEFAULT_ISSUE_DESCRIPTION : additionalInfo, - stackTrace.toString() - ); - BrowserUtil.browse( GitHubNewIssueUrlBuilderUtil.buildNewBugIssueUrl( getDefaultIssueTitle(), - bugReportBody + additionalInfo == null ? DEFAULT_ISSUE_DESCRIPTION : additionalInfo, + stackTrace.toString(), + project ) ); diff --git a/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueBodyBuilderUtil.java b/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueBodyBuilderUtil.java index 4ea14112f..92f665833 100644 --- a/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueBodyBuilderUtil.java +++ b/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueBodyBuilderUtil.java @@ -9,6 +9,9 @@ import com.intellij.ide.fileTemplates.FileTemplateManager; import com.intellij.openapi.project.Project; import java.io.IOException; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.Properties; import org.jetbrains.annotations.NotNull; @@ -24,10 +27,44 @@ private GitHubNewIssueBodyBuilderUtil() {} * @param project Project * @param bugDescription String * @param stackTrace String + * @param maxAllowedBodyLength short * * @return String */ public static String buildNewBugReportBody( + final @NotNull Project project, + final @NotNull String bugDescription, + final @NotNull String stackTrace, + final short maxAllowedBodyLength + ) { + final short maxAllowedStackTraceLength = getMaxAllowedStackTraceLength( + project, + bugDescription, + maxAllowedBodyLength + ); + + if (encode(stackTrace).length() <= maxAllowedStackTraceLength) { + return buildTemplate(project, bugDescription, stackTrace); + } + + final String cutStackTrace = encode(stackTrace).substring( + 0, + maxAllowedStackTraceLength - 1 + ); + + return buildTemplate(project, bugDescription, decode(cutStackTrace)); + } + + /** + * Build bug report body template. + * + * @param project Project + * @param bugDescription String + * @param stackTrace String + * + * @return String + */ + private static String buildTemplate( final @NotNull Project project, final @NotNull String bugDescription, final @NotNull String stackTrace @@ -46,4 +83,45 @@ public static String buildNewBugReportBody( return ""; } } + + /** + * Get max allowed stacktrace length. + * + * @param project Project + * @param bugDescription String + * @param maxAllowedBodyLength String + * + * @return short + */ + private static short getMaxAllowedStackTraceLength( + final @NotNull Project project, + final @NotNull String bugDescription, + final short maxAllowedBodyLength + ) { + final String builtTemplateWithoutStackTrace = buildTemplate(project, bugDescription, ""); + + return (short) (maxAllowedBodyLength - encode(builtTemplateWithoutStackTrace).length()); + } + + /** + * Encode string to be used in URI. + * + * @param value String + * + * @return String + */ + private static String encode(final @NotNull String value) { + return URLEncoder.encode(value, StandardCharsets.UTF_8); + } + + /** + * Decode string that was encoded to be used in URI. + * + * @param value String + * + * @return String + */ + private static String decode(final @NotNull String value) { + return URLDecoder.decode(value, StandardCharsets.UTF_8); + } } diff --git a/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueUrlBuilderUtil.java b/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueUrlBuilderUtil.java index cf1d12296..af9bb3b96 100644 --- a/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueUrlBuilderUtil.java +++ b/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueUrlBuilderUtil.java @@ -5,6 +5,7 @@ package com.magento.idea.magento2plugin.project.diagnostic.github; +import com.intellij.openapi.project.Project; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import org.jetbrains.annotations.NotNull; @@ -14,6 +15,8 @@ public final class GitHubNewIssueUrlBuilderUtil { private static final String NEW_BUG_ISSUE_BASE_URL = "https://github.com/magento/" + "magento2-phpstorm-plugin/issues/new" + "?assignees=&labels=bug&template=bug_report.md"; + private static final String URI_PARAMS_PART = "&title=%title&body=%body"; + private static final short MAX_URI_LENGTH = 8000; private GitHubNewIssueUrlBuilderUtil() {} @@ -21,19 +24,58 @@ private GitHubNewIssueUrlBuilderUtil() {} * Build new issue url (template -> bug_report). * * @param title String - * @param body String + * @param bugDescription String + * @param stackTrace String + * @param project Project * * @return String */ public static String buildNewBugIssueUrl( + final @NotNull String title, + final @NotNull String bugDescription, + final @NotNull String stackTrace, + final @NotNull Project project + ) { + final String bugReportBody = GitHubNewIssueBodyBuilderUtil.buildNewBugReportBody( + project, + bugDescription, + stackTrace, + getAllowedBodyLength(title) + ); + + return formatNewBugIssueUrl(title, bugReportBody); + } + + /** + * Format URL with encoded url parameters. + * + * @param title String + * @param body String + * + * @return String + */ + private static String formatNewBugIssueUrl( final @NotNull String title, final @NotNull String body ) { final String encodedTitle = URLEncoder.encode(title, StandardCharsets.UTF_8); final String encodedBody = URLEncoder.encode(body, StandardCharsets.UTF_8); - return NEW_BUG_ISSUE_BASE_URL - .concat("&title=" + encodedTitle) - .concat("&body=" + encodedBody); + final String paramsPart = URI_PARAMS_PART + .replace("%title", encodedTitle) + .replace("%body", encodedBody); + + return NEW_BUG_ISSUE_BASE_URL.concat(paramsPart); + } + + /** + * Calculate max allowed body length. + * + * @param title String + * + * @return short + */ + private static short getAllowedBodyLength(final @NotNull String title) { + return (short) (MAX_URI_LENGTH - formatNewBugIssueUrl(title, "").length()); } } From e367ae3e0c40f8a95a8d36038c72afbc98e0e32f Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Sat, 8 May 2021 23:59:51 +0300 Subject: [PATCH 2/2] Fixed static checks issues --- .../github/GitHubNewIssueBodyBuilderUtil.java | 14 +++++++------- .../github/GitHubNewIssueUrlBuilderUtil.java | 8 ++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueBodyBuilderUtil.java b/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueBodyBuilderUtil.java index 92f665833..bf3abfcae 100644 --- a/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueBodyBuilderUtil.java +++ b/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueBodyBuilderUtil.java @@ -27,7 +27,7 @@ private GitHubNewIssueBodyBuilderUtil() {} * @param project Project * @param bugDescription String * @param stackTrace String - * @param maxAllowedBodyLength short + * @param maxAllowedBodyLength int * * @return String */ @@ -35,9 +35,9 @@ public static String buildNewBugReportBody( final @NotNull Project project, final @NotNull String bugDescription, final @NotNull String stackTrace, - final short maxAllowedBodyLength + final int maxAllowedBodyLength ) { - final short maxAllowedStackTraceLength = getMaxAllowedStackTraceLength( + final int maxAllowedStackTraceLength = getMaxAllowedStackTraceLength( project, bugDescription, maxAllowedBodyLength @@ -91,16 +91,16 @@ private static String buildTemplate( * @param bugDescription String * @param maxAllowedBodyLength String * - * @return short + * @return int */ - private static short getMaxAllowedStackTraceLength( + private static int getMaxAllowedStackTraceLength( final @NotNull Project project, final @NotNull String bugDescription, - final short maxAllowedBodyLength + final int maxAllowedBodyLength ) { final String builtTemplateWithoutStackTrace = buildTemplate(project, bugDescription, ""); - return (short) (maxAllowedBodyLength - encode(builtTemplateWithoutStackTrace).length()); + return maxAllowedBodyLength - encode(builtTemplateWithoutStackTrace).length(); } /** diff --git a/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueUrlBuilderUtil.java b/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueUrlBuilderUtil.java index af9bb3b96..50591e9c8 100644 --- a/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueUrlBuilderUtil.java +++ b/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueUrlBuilderUtil.java @@ -16,7 +16,7 @@ public final class GitHubNewIssueUrlBuilderUtil { + "magento2-phpstorm-plugin/issues/new" + "?assignees=&labels=bug&template=bug_report.md"; private static final String URI_PARAMS_PART = "&title=%title&body=%body"; - private static final short MAX_URI_LENGTH = 8000; + private static final int MAX_URI_LENGTH = 8000; private GitHubNewIssueUrlBuilderUtil() {} @@ -73,9 +73,9 @@ private static String formatNewBugIssueUrl( * * @param title String * - * @return short + * @return int */ - private static short getAllowedBodyLength(final @NotNull String title) { - return (short) (MAX_URI_LENGTH - formatNewBugIssueUrl(title, "").length()); + private static int getAllowedBodyLength(final @NotNull String title) { + return MAX_URI_LENGTH - formatNewBugIssueUrl(title, "").length(); } }