Skip to content

Commit

Permalink
Generate the whole bug report as scratch file and prompt to copy past…
Browse files Browse the repository at this point in the history
…e it (#200)
  • Loading branch information
arturbosch committed Jul 9, 2022
1 parent 48a18ab commit 083eeac
Showing 1 changed file with 38 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.intellij.ide.BrowserUtil
import com.intellij.ide.DataManager
import com.intellij.ide.scratch.ScratchRootType
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.invokeLater
import com.intellij.openapi.diagnostic.ErrorReportSubmitter
import com.intellij.openapi.diagnostic.IdeaLoggingEvent
import com.intellij.openapi.diagnostic.SubmittedReportInfo
Expand All @@ -23,18 +23,44 @@ class GitHubErrorReporting : ErrorReportSubmitter() {
events: Array<out IdeaLoggingEvent>,
additionalInfo: String?,
parentComponent: Component,
consumer: Consumer<in SubmittedReportInfo>
consumer: Consumer<in SubmittedReportInfo>,
): Boolean {
val uri = URIBuilder("https://github.com/detekt/detekt-intellij-plugin/issues/new")
.addParameter("title", events.mapNotNull { it.throwableText.firstLine() }.joinToString("; "))
.addParameter("labels", "bug")
.addParameter("body", formatIssueBody(additionalInfo))
.addParameter("body", "Please copy paste the generated bug report.")
.build()

val bugReport = buildString {
appendLine("## Bug description")
appendLine()
appendLine(additionalInfo ?: "Please include steps to reproduce expected and actual behavior.")
appendLine()
appendLine("## Environment")
appendLine()
appendLine("- detekt Idea Version: ${PluginUtils.pluginVersion()}")
appendLine("- Platform Version: ${PluginUtils.platformVersion()}")
appendLine("- Platform Vendor: ${SystemInfo.JAVA_VENDOR}")
appendLine("- Java Version: ${SystemInfo.JAVA_VERSION}")
appendLine("- OS Name: ${SystemInfo.OS_NAME}")
appendLine()
appendLine("## Stacktrace")
appendLine()
appendLine(
events.map { it.throwableText.trim() }
.joinToString(System.lineSeparator()) {
"""
|```
|$it
|```
""".trimMargin()
}
)
}

return runCatching {
BrowserUtil.browse(uri)
ApplicationManager.getApplication()
.invokeLater { openStacktraceScratchFile(parentComponent, formatStacktrace(events)) }
invokeLater { openStacktraceScratchFile(parentComponent, bugReport) }
consumer.consume(SubmittedReportInfo(SubmittedReportInfo.SubmissionStatus.NEW_ISSUE))
true
}.getOrElse {
Expand All @@ -43,45 +69,19 @@ class GitHubErrorReporting : ErrorReportSubmitter() {
}
}

private fun openStacktraceScratchFile(parentComponent: Component, stacktrace: String) {
private fun openStacktraceScratchFile(parentComponent: Component, bugReportText: String) {
val dataContext = DataManager.getInstance().getDataContext(parentComponent)
val project = CommonDataKeys.PROJECT.getData(dataContext)
requireNotNull(project)
val scratchFile = ScratchRootType.getInstance()
.createScratchFile(project, "detekt-idea-stacktrace.md", PlainTextLanguage.INSTANCE, stacktrace)
val scratchFile = ScratchRootType.getInstance().createScratchFile(
project,
"detekt-idea-stacktrace.md",
PlainTextLanguage.INSTANCE,
bugReportText
)
requireNotNull(scratchFile)
OpenFileDescriptor(project, scratchFile).navigate(true)
}

private fun formatStacktrace(events: Array<out IdeaLoggingEvent>): String =
"Please copy following stacktrace to the opened issue body." +
System.lineSeparator() +
System.lineSeparator() +
events.joinToString(System.lineSeparator()) {
"""
```
${it.throwableText.trim()}
```
""".trimIndent()
}

private fun formatIssueBody(additionalInfo: String?): String = """
## Bug description
${additionalInfo ?: "Please include steps to reproduce expected and actual behavior."}
## Environment
- detekt Idea Version: ${PluginUtils.pluginVersion()}
- Platform Version: ${PluginUtils.platformVersion()}
- Platform Vendor: ${SystemInfo.JAVA_VENDOR}
- Java Version: ${SystemInfo.JAVA_VERSION}
- OS Name: ${SystemInfo.OS_NAME}
## Stacktrace
```
Please include the stacktrace from the temporary scratch issue.
```
""".trimIndent()

private fun String.firstLine(): String? = trim().split(System.lineSeparator()).firstOrNull()
}

0 comments on commit 083eeac

Please sign in to comment.