Skip to content

Commit c85b4cc

Browse files
author
Vitaliy
authored
Merge pull request #552 from bohdan-harniuk/error-handler-development
Developed error handler that help user to create new bug report
2 parents fa01a2c + 4728871 commit c85b4cc

File tree

7 files changed

+243
-1
lines changed

7 files changed

+243
-1
lines changed

resources/META-INF/plugin.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@
246246
<defaultLiveTemplates file="/liveTemplates/MagentoPWA.xml"/>
247247

248248
<postStartupActivity implementation="com.magento.idea.magento2plugin.project.startup.CheckIfMagentoPathIsValidActivity"/>
249+
250+
<errorHandler implementation="com.magento.idea.magento2plugin.project.diagnostic.DefaultErrorReportSubmitter"/>
249251
</extensions>
250252

251253
<extensions defaultExtensionNs="com.jetbrains.php">
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
**Describe the bug** (*)
2+
3+
${BUG_DESCRIPTION}
4+
5+
```
6+
${STACK_TRACE}
7+
```
8+
9+
**To Reproduce** (*)
10+
11+
Steps to reproduce the behavior:
12+
1. Go to '...'
13+
2. Click on '....'
14+
3. Scroll down to '....'
15+
4. See error
16+
17+
**Expected behavior** (*)
18+
19+
A clear and concise description of what you expected to happen.
20+
21+
**Screenshots**
22+
23+
If applicable, add screenshots to help explain your problem.
24+
25+
**Please complete the following information:** (*)
26+
27+
- OS: [e.g. MacOS or Ubuntu Linux 20.04]
28+
- PhpStorm/Intellij version: [e.g. 2019.3.3]
29+
- Plugin Version: [e.g. 1.0.0]
30+
31+
**Additional context**
32+
33+
Add any other context about the problem here.

resources/fileTemplates/code/GitHub New Bug Issue Body Template.txt.html

Whitespace-only changes.

resources/magento2/common.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,7 @@ common.template.id=Template ID
6868
common.template.label=Template Label
6969
common.template.filename=Template File Name
7070
common.template.subject=Email Subject
71-
common.template.type=Email Type
71+
common.template.type=Email Type
72+
common.diagnostic.reportButtonText=Report Me
73+
common.diagnostic.reportSubmittedTitle=The report is successfully submitted!
74+
common.diagnostic.reportSubmittedMessage=Thank you for submitting your report! We will check it as soon as possible.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.project.diagnostic;
7+
8+
import com.intellij.ide.BrowserUtil;
9+
import com.intellij.ide.DataManager;
10+
import com.intellij.openapi.actionSystem.CommonDataKeys;
11+
import com.intellij.openapi.actionSystem.DataContext;
12+
import com.intellij.openapi.application.ApplicationManager;
13+
import com.intellij.openapi.diagnostic.ErrorReportSubmitter;
14+
import com.intellij.openapi.diagnostic.IdeaLoggingEvent;
15+
import com.intellij.openapi.diagnostic.SubmittedReportInfo;
16+
import com.intellij.openapi.project.Project;
17+
import com.intellij.openapi.util.NlsActions;
18+
import com.intellij.util.Consumer;
19+
import com.magento.idea.magento2plugin.bundles.CommonBundle;
20+
import com.magento.idea.magento2plugin.project.diagnostic.github.GitHubNewIssueBodyBuilderUtil;
21+
import com.magento.idea.magento2plugin.project.diagnostic.github.GitHubNewIssueUrlBuilderUtil;
22+
import java.awt.Component;
23+
import java.time.LocalDateTime;
24+
import java.time.format.DateTimeFormatter;
25+
import javax.swing.JOptionPane;
26+
import org.jetbrains.annotations.NotNull;
27+
import org.jetbrains.annotations.Nullable;
28+
29+
public class DefaultErrorReportSubmitter extends ErrorReportSubmitter {
30+
31+
private static final String DEFAULT_ISSUE_TITLE = "Bug Report %date";
32+
private static final String DEFAULT_ISSUE_DESCRIPTION
33+
= "A clear and concise description of what the bug is.";
34+
private final CommonBundle commonBundle;
35+
36+
/**
37+
* Default error report submitter.
38+
*/
39+
public DefaultErrorReportSubmitter() {
40+
super();
41+
commonBundle = new CommonBundle();
42+
}
43+
44+
/**
45+
* Open GitHub link with creation of the new bug report issue.
46+
*
47+
* @param events IdeaLoggingEvent[]
48+
* @param additionalInfo String
49+
* @param parentComponent Component
50+
* @param consumer Consumer
51+
*
52+
* @return boolean
53+
*/
54+
@Override
55+
public boolean submit(
56+
final @NotNull IdeaLoggingEvent[] events,
57+
final @Nullable String additionalInfo,
58+
final @NotNull Component parentComponent,
59+
final @NotNull Consumer<? super SubmittedReportInfo> consumer
60+
) {
61+
final DataContext context = DataManager.getInstance().getDataContext(parentComponent);
62+
final Project project = CommonDataKeys.PROJECT.getData(context);
63+
64+
if (project == null) {
65+
return false;
66+
}
67+
final StringBuilder stackTrace = new StringBuilder();
68+
69+
for (final IdeaLoggingEvent event : events) {
70+
stackTrace.append(event.getThrowableText()).append("\r\n");
71+
}
72+
73+
final String bugReportBody = GitHubNewIssueBodyBuilderUtil.buildNewBugReportBody(
74+
project,
75+
additionalInfo == null ? DEFAULT_ISSUE_DESCRIPTION : additionalInfo,
76+
stackTrace.toString()
77+
);
78+
79+
BrowserUtil.browse(
80+
GitHubNewIssueUrlBuilderUtil.buildNewBugIssueUrl(
81+
getDefaultIssueTitle(),
82+
bugReportBody
83+
)
84+
);
85+
86+
ApplicationManager.getApplication().invokeLater(() -> {
87+
JOptionPane.showMessageDialog(
88+
parentComponent,
89+
commonBundle.message("common.diagnostic.reportSubmittedMessage"),
90+
commonBundle.message("common.diagnostic.reportSubmittedTitle"),
91+
JOptionPane.INFORMATION_MESSAGE
92+
);
93+
consumer.consume(
94+
new SubmittedReportInfo(SubmittedReportInfo.SubmissionStatus.NEW_ISSUE)
95+
);
96+
});
97+
98+
return true;
99+
}
100+
101+
@Override
102+
public @NlsActions.ActionText @NotNull String getReportActionText() {
103+
return commonBundle.message("common.diagnostic.reportButtonText");
104+
}
105+
106+
/**
107+
* Get default bug issue title.
108+
*
109+
* @return String
110+
*/
111+
private String getDefaultIssueTitle() {
112+
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
113+
114+
return DEFAULT_ISSUE_TITLE.replace("%date", formatter.format(LocalDateTime.now()));
115+
}
116+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.project.diagnostic.github;
7+
8+
import com.intellij.ide.fileTemplates.FileTemplate;
9+
import com.intellij.ide.fileTemplates.FileTemplateManager;
10+
import com.intellij.openapi.project.Project;
11+
import java.io.IOException;
12+
import java.util.Properties;
13+
import org.jetbrains.annotations.NotNull;
14+
15+
public final class GitHubNewIssueBodyBuilderUtil {
16+
17+
private static final String BUR_REPORT_TEMPLATE = "GitHub New Bug Issue Body Template";
18+
19+
private GitHubNewIssueBodyBuilderUtil() {}
20+
21+
/**
22+
* Build BUG report body.
23+
*
24+
* @param project Project
25+
* @param bugDescription String
26+
* @param stackTrace String
27+
*
28+
* @return String
29+
*/
30+
public static String buildNewBugReportBody(
31+
final @NotNull Project project,
32+
final @NotNull String bugDescription,
33+
final @NotNull String stackTrace
34+
) {
35+
final FileTemplateManager templateManager = FileTemplateManager.getInstance(project);
36+
final FileTemplate errorReportTemplate =
37+
templateManager.getCodeTemplate(BUR_REPORT_TEMPLATE);
38+
39+
final Properties properties = new Properties();
40+
properties.setProperty("BUG_DESCRIPTION", bugDescription);
41+
properties.setProperty("STACK_TRACE", stackTrace);
42+
43+
try {
44+
return errorReportTemplate.getText(properties);
45+
} catch (IOException exception) {
46+
return "";
47+
}
48+
}
49+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.project.diagnostic.github;
7+
8+
import java.net.URLEncoder;
9+
import java.nio.charset.StandardCharsets;
10+
import org.jetbrains.annotations.NotNull;
11+
12+
public final class GitHubNewIssueUrlBuilderUtil {
13+
14+
private static final String NEW_BUG_ISSUE_BASE_URL = "https://github.com/magento/"
15+
+ "magento2-phpstorm-plugin/issues/new"
16+
+ "?assignees=&labels=bug&template=bug_report.md";
17+
18+
private GitHubNewIssueUrlBuilderUtil() {}
19+
20+
/**
21+
* Build new issue url (template -> bug_report).
22+
*
23+
* @param title String
24+
* @param body String
25+
*
26+
* @return String
27+
*/
28+
public static String buildNewBugIssueUrl(
29+
final @NotNull String title,
30+
final @NotNull String body
31+
) {
32+
final String encodedTitle = URLEncoder.encode(title, StandardCharsets.UTF_8);
33+
final String encodedBody = URLEncoder.encode(body, StandardCharsets.UTF_8);
34+
35+
return NEW_BUG_ISSUE_BASE_URL
36+
.concat("&title=" + encodedTitle)
37+
.concat("&body=" + encodedBody);
38+
}
39+
}

0 commit comments

Comments
 (0)