|
| 1 | +package com.github.dinbtechit.vscodetheme.diagostic |
| 2 | + |
| 3 | +import com.intellij.ide.BrowserUtil |
| 4 | +import com.intellij.ide.plugins.PluginManagerCore |
| 5 | +import com.intellij.openapi.application.ApplicationInfo |
| 6 | +import com.intellij.openapi.diagnostic.ErrorReportSubmitter |
| 7 | +import com.intellij.openapi.diagnostic.IdeaLoggingEvent |
| 8 | +import com.intellij.openapi.diagnostic.SubmittedReportInfo |
| 9 | +import com.intellij.openapi.extensions.PluginId |
| 10 | +import org.apache.commons.lang.StringUtils |
| 11 | +import org.apache.http.client.utils.URIBuilder |
| 12 | +import java.awt.Component |
| 13 | +import java.io.BufferedReader |
| 14 | +import java.io.StringReader |
| 15 | +import java.net.URI |
| 16 | +import java.util.stream.Collectors |
| 17 | + |
| 18 | +class VSCodeErrorReportSubmitter: ErrorReportSubmitter() { |
| 19 | + |
| 20 | + private val packageAbbreviation: Map<String, String>? = null |
| 21 | + |
| 22 | + override fun getReportActionText(): String { |
| 23 | + return "Report to VSCode Theme" |
| 24 | + } |
| 25 | + |
| 26 | + override fun submit( |
| 27 | + events: Array<out IdeaLoggingEvent>, |
| 28 | + additionalInfo: String?, |
| 29 | + parentComponent: Component, |
| 30 | + consumer: com.intellij.util.Consumer<in SubmittedReportInfo> |
| 31 | + ): Boolean { |
| 32 | + getReportIssueUrl( |
| 33 | + getReportTitle(events), |
| 34 | + getReportBody(events, additionalInfo) |
| 35 | + )?.let { |
| 36 | + BrowserUtil.browse(it) |
| 37 | + } |
| 38 | + return true |
| 39 | + } |
| 40 | + |
| 41 | + private fun getReportTitle(events: Array<out IdeaLoggingEvent>): String { |
| 42 | + val event = events.firstOrNull() |
| 43 | + return event?.throwableText?.lineSequence()?.first() |
| 44 | + ?: event?.message |
| 45 | + ?: "Report bug" |
| 46 | + } |
| 47 | + |
| 48 | + private fun getReportBody(events: Array<out IdeaLoggingEvent>, additionalInfo: String?): String { |
| 49 | + val javaVendor = System.getProperty("java.vendor") |
| 50 | + val javaVersion = System.getProperty("java.version") |
| 51 | + |
| 52 | + val osName = System.getProperty("os.name") |
| 53 | + val osArch = System.getProperty("os.arch") |
| 54 | + |
| 55 | + val appName = ApplicationInfo.getInstance().fullApplicationName |
| 56 | + |
| 57 | + val plugin = PluginManagerCore.getPlugin(PluginId.getId("com.github.dinbtechit.vscodetheme")) |
| 58 | + val pluginVersion = plugin?.version |
| 59 | + |
| 60 | + var stackTrace = "" |
| 61 | + for (event in events) { |
| 62 | + val message = event.message |
| 63 | + if (!message.isNullOrBlank()) { |
| 64 | + stackTrace = stackTrace.plus(message).plus("\n") |
| 65 | + } |
| 66 | + val throwableText = event.throwableText |
| 67 | + if (!throwableText.isNullOrBlank()) { |
| 68 | + stackTrace = stackTrace.plus("\n```\n") |
| 69 | + stackTrace= stackTrace.plus( |
| 70 | + BufferedReader(StringReader(throwableText)).lines() |
| 71 | + .map { line -> |
| 72 | + var abbreviated = line |
| 73 | + packageAbbreviation?.entries?.forEach { entry -> |
| 74 | + abbreviated = StringUtils.replace(abbreviated, entry.key, entry.value) |
| 75 | + } |
| 76 | + abbreviated |
| 77 | + }.collect(Collectors.joining("\n")) |
| 78 | + ) |
| 79 | + stackTrace= stackTrace.plus("\n```\n\n") |
| 80 | + } |
| 81 | + } |
| 82 | + var description = additionalInfo |
| 83 | + if (description.isNullOrBlank()) { |
| 84 | + description = "A clear and concise description of what the bug is." |
| 85 | + } |
| 86 | + |
| 87 | + return """ |
| 88 | + |#Describe the bug |
| 89 | + | $description |
| 90 | + | |
| 91 | + |#To Reproduce |
| 92 | + |Steps to reproduce the behavior: |
| 93 | + |1. Go to '...' |
| 94 | + |2. Click on '....' |
| 95 | + |3. Scroll down to '....' |
| 96 | + |4. See error |
| 97 | + | |
| 98 | + |#Expected behavior |
| 99 | + |A clear and concise description of what you expected to happen. |
| 100 | + | |
| 101 | + |#Screenshots |
| 102 | + |If applicable, add screenshots to help explain your problem. |
| 103 | + | |
| 104 | + |#Environment |
| 105 | + |* Java: $javaVendor $javaVersion |
| 106 | + |* OS: $osName $osArch |
| 107 | + |* IDE: $appName |
| 108 | + |* Version: $pluginVersion |
| 109 | + | |
| 110 | + |#Additional context |
| 111 | + |${additionalInfo ?: "Add any other context about the problem here."} |
| 112 | + | |
| 113 | + |#Stacktrace# |
| 114 | + | $stackTrace |
| 115 | + """.trimMargin() |
| 116 | + } |
| 117 | + |
| 118 | + private fun getReportIssueUrl(title: String, body: String): URI? { |
| 119 | + val uriBuilder = URIBuilder("https://github.com/dinbtechit/vscode-theme/issues/new") |
| 120 | + uriBuilder.addParameter("title", "[Bug] $title") |
| 121 | + uriBuilder.addParameter("labels", "bug") |
| 122 | + if (body.isNotBlank()) { |
| 123 | + uriBuilder.addParameter("body", body) |
| 124 | + } else { |
| 125 | + uriBuilder.addParameter("template", "bug_report.md") |
| 126 | + } |
| 127 | + return uriBuilder.build() |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments