Skip to content

Commit

Permalink
Merge pull request #55 from pestphp/github-error-reporter
Browse files Browse the repository at this point in the history
feat(report): Add error reporting to GitHub
  • Loading branch information
olivernybroe authored Aug 19, 2020
2 parents ee96633 + c0bbaa5 commit e896c3d
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Added support for showing pest version ([#52](https://github.com/pestphp/pest-intellij/pull/52))
- Type provider for Pest test functions ([#48](https://github.com/pestphp/pest-intellij/pull/48))
- Added support for navigation between tests and test subject ([#53](https://github.com/pestphp/pest-intellij/pull/53))
- Added error reporting to GitHub issues ([#55](https://github.com/pestphp/pest-intellij/pull/55))

### Changed

Expand Down
89 changes: 89 additions & 0 deletions src/main/kotlin/com/pestphp/pest/GithubErrorReporter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.pestphp.pest

import com.intellij.ide.BrowserUtil
import com.intellij.ide.plugins.PluginManagerCore
import com.intellij.openapi.diagnostic.ErrorReportSubmitter
import com.intellij.openapi.diagnostic.IdeaLoggingEvent
import com.intellij.openapi.diagnostic.SubmittedReportInfo
import com.intellij.openapi.extensions.PluginId
import com.intellij.util.Consumer
import java.awt.Component
import java.io.UnsupportedEncodingException
import java.net.URLEncoder

/**
* Generates a issue creation link
* https://github.com/pestphp/pest-intellij/issues/new?title=foo&body=bar
*/
class GithubErrorReporter : ErrorReportSubmitter() {
companion object {
private const val URL = "https://github.com/pestphp/pest-intellij/issues/new?title="
private const val ENCODING = "UTF-8"
private const val STACKTRACE_LENGTH = 7000
}

override fun getReportActionText(): String {
return "Report to Pest issue tracker"
}

override fun submit(
events: Array<IdeaLoggingEvent>,
additionalInfo: String?,
parentComponent: Component,
consumer: Consumer<SubmittedReportInfo>
): Boolean {
val event = events.firstOrNull()
val title = event?.throwableText?.lineSequence()?.first()
?: event?.message
?: "Crash Report: <Fill in title>"
val body = event?.throwableText ?: "Please paste the full stacktrace from the IDEA error popup."
val version = PluginManagerCore.getPlugin(PluginId.getId("com.pestphp.pest-intellij"))?.version

val builder = StringBuilder(URL)
try {
builder.append(URLEncoder.encode(title, ENCODING))
builder.append("&body=")
builder.append(
URLEncoder.encode(
"""
|| Q | A
|| ---------------- | -----
|| Bug report? | yes
|| Plugin version | $version
|| Pest version | x.y.z
|| OS | ${System.getProperty("os.name")}
|
|### Description
|${additionalInfo ?: ""}
|
|### Stacktrace
|```
|${body.take(STACKTRACE_LENGTH)}
|```
""".trimMargin(),
ENCODING
)
)
} catch (e: UnsupportedEncodingException) {
consumer.consume(
SubmittedReportInfo(
null,
null,
SubmittedReportInfo.SubmissionStatus.FAILED
)
)
return false
}

BrowserUtil.browse(builder.toString())
consumer.consume(
SubmittedReportInfo(
null,
"GitHub issue",
SubmittedReportInfo.SubmissionStatus.NEW_ISSUE
)
)
return true
}
}

1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<projectService serviceImplementation="com.pestphp.pest.PestSettings"/>
<testFinder implementation="com.pestphp.pest.goto.PestTestFinder"/>
<fileBasedIndex implementation="com.pestphp.pest.indexers.PestTestIndex"/>
<errorHandler implementation="com.pestphp.pest.GithubErrorReporter"/>
</extensions>

<extensions defaultExtensionNs="com.jetbrains.php">
Expand Down

0 comments on commit e896c3d

Please sign in to comment.