Skip to content
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

Developed error handler that help user to create new bug report #552

Merged
2 changes: 2 additions & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@
<defaultLiveTemplates file="/liveTemplates/MagentoPWA.xml"/>

<postStartupActivity implementation="com.magento.idea.magento2plugin.project.startup.CheckIfMagentoPathIsValidActivity"/>

<errorHandler implementation="com.magento.idea.magento2plugin.project.diagnostic.DefaultErrorReportSubmitter"/>
</extensions>

<extensions defaultExtensionNs="com.jetbrains.php">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
**Describe the bug** (*)

${BUG_DESCRIPTION}

```
${STACK_TRACE}
```

**To Reproduce** (*)

Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**Expected behavior** (*)

A clear and concise description of what you expected to happen.

**Screenshots**

If applicable, add screenshots to help explain your problem.

**Please complete the following information:** (*)

- OS: [e.g. MacOS or Ubuntu Linux 20.04]
- PhpStorm/Intellij version: [e.g. 2019.3.3]
- Plugin Version: [e.g. 1.0.0]

**Additional context**

Add any other context about the problem here.
Empty file.
5 changes: 4 additions & 1 deletion resources/magento2/common.properties
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,7 @@ common.template.id=Template ID
common.template.label=Template Label
common.template.filename=Template File Name
common.template.subject=Email Subject
common.template.type=Email Type
common.template.type=Email Type
common.diagnostic.reportButtonText=Report Me
common.diagnostic.reportSubmittedTitle=The report is successfully submitted!
common.diagnostic.reportSubmittedMessage=Thank you for submitting your report! We will check it as soon as possible.
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.project.diagnostic;

import com.intellij.ide.BrowserUtil;
import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.ErrorReportSubmitter;
import com.intellij.openapi.diagnostic.IdeaLoggingEvent;
import com.intellij.openapi.diagnostic.SubmittedReportInfo;
import com.intellij.openapi.project.Project;
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;
import java.time.format.DateTimeFormatter;
import javax.swing.JOptionPane;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class DefaultErrorReportSubmitter extends ErrorReportSubmitter {

private static final String DEFAULT_ISSUE_TITLE = "Bug Report %date";
private static final String DEFAULT_ISSUE_DESCRIPTION
= "A clear and concise description of what the bug is.";
private final CommonBundle commonBundle;

/**
* Default error report submitter.
*/
public DefaultErrorReportSubmitter() {
super();
commonBundle = new CommonBundle();
}

/**
* Open GitHub link with creation of the new bug report issue.
*
* @param events IdeaLoggingEvent[]
* @param additionalInfo String
* @param parentComponent Component
* @param consumer Consumer
*
* @return boolean
*/
@Override
public boolean submit(
final @NotNull IdeaLoggingEvent[] events,
final @Nullable String additionalInfo,
final @NotNull Component parentComponent,
final @NotNull Consumer<? super SubmittedReportInfo> consumer
) {
final DataContext context = DataManager.getInstance().getDataContext(parentComponent);
final Project project = CommonDataKeys.PROJECT.getData(context);

if (project == null) {
return false;
}
final StringBuilder stackTrace = new StringBuilder();

for (final IdeaLoggingEvent event : events) {
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
)
);

ApplicationManager.getApplication().invokeLater(() -> {
JOptionPane.showMessageDialog(
parentComponent,
commonBundle.message("common.diagnostic.reportSubmittedMessage"),
commonBundle.message("common.diagnostic.reportSubmittedTitle"),
JOptionPane.INFORMATION_MESSAGE
);
consumer.consume(
new SubmittedReportInfo(SubmittedReportInfo.SubmissionStatus.NEW_ISSUE)
);
});

return true;
}

@Override
public @NlsActions.ActionText @NotNull String getReportActionText() {
return commonBundle.message("common.diagnostic.reportButtonText");
}

/**
* Get default bug issue title.
*
* @return String
*/
private String getDefaultIssueTitle() {
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");

return DEFAULT_ISSUE_TITLE.replace("%date", formatter.format(LocalDateTime.now()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

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

import com.intellij.ide.fileTemplates.FileTemplate;
import com.intellij.ide.fileTemplates.FileTemplateManager;
import com.intellij.openapi.project.Project;
import java.io.IOException;
import java.util.Properties;
import org.jetbrains.annotations.NotNull;

public final class GitHubNewIssueBodyBuilderUtil {

private static final String BUR_REPORT_TEMPLATE = "GitHub New Bug Issue Body Template";

private GitHubNewIssueBodyBuilderUtil() {}

/**
* Build BUG report body.
*
* @param project Project
* @param bugDescription String
* @param stackTrace String
*
* @return String
*/
public static String buildNewBugReportBody(
final @NotNull Project project,
final @NotNull String bugDescription,
final @NotNull String stackTrace
) {
final FileTemplateManager templateManager = FileTemplateManager.getInstance(project);
final FileTemplate errorReportTemplate =
templateManager.getCodeTemplate(BUR_REPORT_TEMPLATE);

final Properties properties = new Properties();
properties.setProperty("BUG_DESCRIPTION", bugDescription);
properties.setProperty("STACK_TRACE", stackTrace);

try {
return errorReportTemplate.getText(properties);
} catch (IOException exception) {
return "";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

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

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import org.jetbrains.annotations.NotNull;

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 GitHubNewIssueUrlBuilderUtil() {}

/**
* Build new issue url (template -> bug_report).
*
* @param title String
* @param body String
*
* @return String
*/
public static String buildNewBugIssueUrl(
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);
}
}