Skip to content

Commit 9fbde76

Browse files
author
Vitaliy
authored
Merge pull request #592 from bohdan-harniuk/fix-diagnostic-sent-too-long-description
Fixed issue with too long URI for generated bug report by diagnostic feature
2 parents 0b7041d + e367ae3 commit 9fbde76

File tree

4 files changed

+129
-14
lines changed

4 files changed

+129
-14
lines changed

resources/META-INF/plugin.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@
270270
<internalFileTemplate name="Magento Delete Entity By Id Command"/>
271271
<internalFileTemplate name="Magento Entity Edit Action Controller Class"/>
272272
<internalFileTemplate name="Magento Entity Delete Controller Class"/>
273-
<internalFileTemplate name="Magento Web Api XML"/>
274-
<internalFileTemplate name="Web Api Interface"/>
273+
<internalFileTemplate name="Magento Web API XML"/>
274+
<internalFileTemplate name="Web API Interface"/>
275275

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

src/com/magento/idea/magento2plugin/project/diagnostic/DefaultErrorReportSubmitter.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import com.intellij.openapi.util.NlsActions;
1818
import com.intellij.util.Consumer;
1919
import com.magento.idea.magento2plugin.bundles.CommonBundle;
20-
import com.magento.idea.magento2plugin.project.diagnostic.github.GitHubNewIssueBodyBuilderUtil;
2120
import com.magento.idea.magento2plugin.project.diagnostic.github.GitHubNewIssueUrlBuilderUtil;
2221
import java.awt.Component;
2322
import java.time.LocalDateTime;
@@ -70,16 +69,12 @@ public boolean submit(
7069
stackTrace.append(event.getThrowableText()).append("\r\n");
7170
}
7271

73-
final String bugReportBody = GitHubNewIssueBodyBuilderUtil.buildNewBugReportBody(
74-
project,
75-
additionalInfo == null ? DEFAULT_ISSUE_DESCRIPTION : additionalInfo,
76-
stackTrace.toString()
77-
);
78-
7972
BrowserUtil.browse(
8073
GitHubNewIssueUrlBuilderUtil.buildNewBugIssueUrl(
8174
getDefaultIssueTitle(),
82-
bugReportBody
75+
additionalInfo == null ? DEFAULT_ISSUE_DESCRIPTION : additionalInfo,
76+
stackTrace.toString(),
77+
project
8378
)
8479
);
8580

src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueBodyBuilderUtil.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import com.intellij.ide.fileTemplates.FileTemplateManager;
1010
import com.intellij.openapi.project.Project;
1111
import java.io.IOException;
12+
import java.net.URLDecoder;
13+
import java.net.URLEncoder;
14+
import java.nio.charset.StandardCharsets;
1215
import java.util.Properties;
1316
import org.jetbrains.annotations.NotNull;
1417

@@ -24,10 +27,44 @@ private GitHubNewIssueBodyBuilderUtil() {}
2427
* @param project Project
2528
* @param bugDescription String
2629
* @param stackTrace String
30+
* @param maxAllowedBodyLength int
2731
*
2832
* @return String
2933
*/
3034
public static String buildNewBugReportBody(
35+
final @NotNull Project project,
36+
final @NotNull String bugDescription,
37+
final @NotNull String stackTrace,
38+
final int maxAllowedBodyLength
39+
) {
40+
final int maxAllowedStackTraceLength = getMaxAllowedStackTraceLength(
41+
project,
42+
bugDescription,
43+
maxAllowedBodyLength
44+
);
45+
46+
if (encode(stackTrace).length() <= maxAllowedStackTraceLength) {
47+
return buildTemplate(project, bugDescription, stackTrace);
48+
}
49+
50+
final String cutStackTrace = encode(stackTrace).substring(
51+
0,
52+
maxAllowedStackTraceLength - 1
53+
);
54+
55+
return buildTemplate(project, bugDescription, decode(cutStackTrace));
56+
}
57+
58+
/**
59+
* Build bug report body template.
60+
*
61+
* @param project Project
62+
* @param bugDescription String
63+
* @param stackTrace String
64+
*
65+
* @return String
66+
*/
67+
private static String buildTemplate(
3168
final @NotNull Project project,
3269
final @NotNull String bugDescription,
3370
final @NotNull String stackTrace
@@ -46,4 +83,45 @@ public static String buildNewBugReportBody(
4683
return "";
4784
}
4885
}
86+
87+
/**
88+
* Get max allowed stacktrace length.
89+
*
90+
* @param project Project
91+
* @param bugDescription String
92+
* @param maxAllowedBodyLength String
93+
*
94+
* @return int
95+
*/
96+
private static int getMaxAllowedStackTraceLength(
97+
final @NotNull Project project,
98+
final @NotNull String bugDescription,
99+
final int maxAllowedBodyLength
100+
) {
101+
final String builtTemplateWithoutStackTrace = buildTemplate(project, bugDescription, "");
102+
103+
return maxAllowedBodyLength - encode(builtTemplateWithoutStackTrace).length();
104+
}
105+
106+
/**
107+
* Encode string to be used in URI.
108+
*
109+
* @param value String
110+
*
111+
* @return String
112+
*/
113+
private static String encode(final @NotNull String value) {
114+
return URLEncoder.encode(value, StandardCharsets.UTF_8);
115+
}
116+
117+
/**
118+
* Decode string that was encoded to be used in URI.
119+
*
120+
* @param value String
121+
*
122+
* @return String
123+
*/
124+
private static String decode(final @NotNull String value) {
125+
return URLDecoder.decode(value, StandardCharsets.UTF_8);
126+
}
49127
}

src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueUrlBuilderUtil.java

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package com.magento.idea.magento2plugin.project.diagnostic.github;
77

8+
import com.intellij.openapi.project.Project;
89
import java.net.URLEncoder;
910
import java.nio.charset.StandardCharsets;
1011
import org.jetbrains.annotations.NotNull;
@@ -14,26 +15,67 @@ public final class GitHubNewIssueUrlBuilderUtil {
1415
private static final String NEW_BUG_ISSUE_BASE_URL = "https://github.com/magento/"
1516
+ "magento2-phpstorm-plugin/issues/new"
1617
+ "?assignees=&labels=bug&template=bug_report.md";
18+
private static final String URI_PARAMS_PART = "&title=%title&body=%body";
19+
private static final int MAX_URI_LENGTH = 8000;
1720

1821
private GitHubNewIssueUrlBuilderUtil() {}
1922

2023
/**
2124
* Build new issue url (template -> bug_report).
2225
*
2326
* @param title String
24-
* @param body String
27+
* @param bugDescription String
28+
* @param stackTrace String
29+
* @param project Project
2530
*
2631
* @return String
2732
*/
2833
public static String buildNewBugIssueUrl(
34+
final @NotNull String title,
35+
final @NotNull String bugDescription,
36+
final @NotNull String stackTrace,
37+
final @NotNull Project project
38+
) {
39+
final String bugReportBody = GitHubNewIssueBodyBuilderUtil.buildNewBugReportBody(
40+
project,
41+
bugDescription,
42+
stackTrace,
43+
getAllowedBodyLength(title)
44+
);
45+
46+
return formatNewBugIssueUrl(title, bugReportBody);
47+
}
48+
49+
/**
50+
* Format URL with encoded url parameters.
51+
*
52+
* @param title String
53+
* @param body String
54+
*
55+
* @return String
56+
*/
57+
private static String formatNewBugIssueUrl(
2958
final @NotNull String title,
3059
final @NotNull String body
3160
) {
3261
final String encodedTitle = URLEncoder.encode(title, StandardCharsets.UTF_8);
3362
final String encodedBody = URLEncoder.encode(body, StandardCharsets.UTF_8);
3463

35-
return NEW_BUG_ISSUE_BASE_URL
36-
.concat("&title=" + encodedTitle)
37-
.concat("&body=" + encodedBody);
64+
final String paramsPart = URI_PARAMS_PART
65+
.replace("%title", encodedTitle)
66+
.replace("%body", encodedBody);
67+
68+
return NEW_BUG_ISSUE_BASE_URL.concat(paramsPart);
69+
}
70+
71+
/**
72+
* Calculate max allowed body length.
73+
*
74+
* @param title String
75+
*
76+
* @return int
77+
*/
78+
private static int getAllowedBodyLength(final @NotNull String title) {
79+
return MAX_URI_LENGTH - formatNewBugIssueUrl(title, "").length();
3880
}
3981
}

0 commit comments

Comments
 (0)