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

Fix Bad Request on Open GitHub Issue report when URL is too long #504

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/kotlin/com/sourcegraph/cody/error/CodyError.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.sourcegraph.cody.error

data class CodyError(
val title: String?,
val title: String,
val pluginVersion: String?,
val ideVersion: String?,
val ideVersion: String,
val additionalInfo: String?,
val stacktrace: String?
val stacktrace: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ object CodyErrorFormatter {
"Plugin version" to error.pluginVersion,
"IDE version" to error.ideVersion,
"Additional information" to error.additionalInfo,
"Exception" to error.title,
"Stacktrace" to error.stacktrace)
.filterValues { it != null }
.map { toLabeledCodeBlock(it.key, it.value!!) }
Expand Down
17 changes: 10 additions & 7 deletions src/main/kotlin/com/sourcegraph/cody/error/CodyErrorSubmitter.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.sourcegraph.cody.error

import com.intellij.ide.BrowserUtil
import com.intellij.ide.plugins.IdeaPluginDescriptor
import com.intellij.openapi.application.ApplicationInfo
import com.intellij.openapi.diagnostic.ErrorReportSubmitter
import com.intellij.openapi.diagnostic.IdeaLoggingEvent
Expand All @@ -25,9 +24,8 @@ class CodyErrorSubmitter : ErrorReportSubmitter() {
if (events.isNotEmpty()) {
val event = events.first()
val error = getErrorDetails(event, additionalInfo)
val issueTitle = "bug: ${error.title}"
val formattedError = CodyErrorFormatter.formatToMarkdown(error)
val url = encodeIssue(issueTitle, formattedError)
val markdownText = CodyErrorFormatter.formatToMarkdown(error)
val url = encodeIssue(error.title, markdownText)
BrowserUtil.browse(url)
}
} catch (e: Exception) {
Expand All @@ -40,11 +38,11 @@ class CodyErrorSubmitter : ErrorReportSubmitter() {

private fun getErrorDetails(event: IdeaLoggingEvent, additionalInfo: String?) =
CodyError(
title = event.throwableText.lines().first(),
pluginVersion = (pluginDescriptor as? IdeaPluginDescriptor)?.version,
title = trimPostfix("bug: " + event.throwableText.lines().first(), 128),
pluginVersion = pluginDescriptor?.version,
ideVersion = ApplicationInfo.getInstance().build.toString(),
additionalInfo = additionalInfo,
stacktrace = event.throwableText)
stacktrace = trimPostfix(event.throwableText, 6500)) // max length for gh links is 8192

private fun encodeIssue(title: String, body: String): String =
"https://github.com/sourcegraph/jetbrains/issues/new" +
Expand All @@ -53,4 +51,9 @@ class CodyErrorSubmitter : ErrorReportSubmitter() {
"&body=${encode(body)}"

private fun encode(text: String) = URLEncoder.encode(text, "UTF-8")

private fun trimPostfix(text: String, maxLength: Int): String {
val postfix = " (...)"
return if (text.length > maxLength) text.take(maxLength - postfix.length) + postfix else text
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class CodyErrorFormatterTest : TestCase() {
"""
Plugin version: ```5.2.18066-nightly```
IDE version: ```IU-233.11799.241```
Exception: ```java.lang.NullPointerException```
Stacktrace:
```text
java.lang.NullPointerException: Exception description
Expand All @@ -34,10 +33,4 @@ class CodyErrorFormatterTest : TestCase() {
.trimIndent()
assertEquals(expectedMarkdown, markdown)
}

fun `test null report results empty markdown`() {
val error = CodyError(null, null, null, null, null)
val markdown = CodyErrorFormatter.formatToMarkdown(error)
assertEquals("", markdown)
}
}
Loading