diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index 03b762a0e..57dbfd2c8 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -246,6 +246,8 @@ + + diff --git a/resources/fileTemplates/code/GitHub New Bug Issue Body Template.txt.ft b/resources/fileTemplates/code/GitHub New Bug Issue Body Template.txt.ft new file mode 100644 index 000000000..2249608eb --- /dev/null +++ b/resources/fileTemplates/code/GitHub New Bug Issue Body Template.txt.ft @@ -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. diff --git a/resources/fileTemplates/code/GitHub New Bug Issue Body Template.txt.html b/resources/fileTemplates/code/GitHub New Bug Issue Body Template.txt.html new file mode 100644 index 000000000..e69de29bb diff --git a/resources/magento2/common.properties b/resources/magento2/common.properties index e6b406f14..a295a06f1 100644 --- a/resources/magento2/common.properties +++ b/resources/magento2/common.properties @@ -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 \ No newline at end of file +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. diff --git a/src/com/magento/idea/magento2plugin/project/diagnostic/DefaultErrorReportSubmitter.java b/src/com/magento/idea/magento2plugin/project/diagnostic/DefaultErrorReportSubmitter.java new file mode 100644 index 000000000..577e36393 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/project/diagnostic/DefaultErrorReportSubmitter.java @@ -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 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())); + } +} diff --git a/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueBodyBuilderUtil.java b/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueBodyBuilderUtil.java new file mode 100644 index 000000000..4ea14112f --- /dev/null +++ b/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueBodyBuilderUtil.java @@ -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 ""; + } + } +} diff --git a/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueUrlBuilderUtil.java b/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueUrlBuilderUtil.java new file mode 100644 index 000000000..cf1d12296 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/project/diagnostic/github/GitHubNewIssueUrlBuilderUtil.java @@ -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); + } +}