Skip to content

Fixed issue with too long URI for generated bug report by diagnostic feature #592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@
<internalFileTemplate name="Magento Delete Entity By Id Command"/>
<internalFileTemplate name="Magento Entity Edit Action Controller Class"/>
<internalFileTemplate name="Magento Entity Delete Controller Class"/>
<internalFileTemplate name="Magento Web Api XML"/>
<internalFileTemplate name="Web Api Interface"/>
<internalFileTemplate name="Magento Web API XML"/>
<internalFileTemplate name="Web API Interface"/>

<defaultLiveTemplates file="/liveTemplates/MagentoPWA.xml"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -24,10 +27,44 @@ private GitHubNewIssueBodyBuilderUtil() {}
* @param project Project
* @param bugDescription String
* @param stackTrace String
* @param maxAllowedBodyLength int
*
* @return String
*/
public static String buildNewBugReportBody(
final @NotNull Project project,
final @NotNull String bugDescription,
final @NotNull String stackTrace,
final int maxAllowedBodyLength
) {
final int 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
Expand All @@ -46,4 +83,45 @@ public static String buildNewBugReportBody(
return "";
}
}

/**
* Get max allowed stacktrace length.
*
* @param project Project
* @param bugDescription String
* @param maxAllowedBodyLength String
*
* @return int
*/
private static int getMaxAllowedStackTraceLength(
final @NotNull Project project,
final @NotNull String bugDescription,
final int maxAllowedBodyLength
) {
final String builtTemplateWithoutStackTrace = buildTemplate(project, bugDescription, "");

return 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,26 +15,67 @@ 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 int MAX_URI_LENGTH = 8000;

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 int
*/
private static int getAllowedBodyLength(final @NotNull String title) {
return MAX_URI_LENGTH - formatNewBugIssueUrl(title, "").length();
}
}