From 18afe80d5a3bb8c71a1fe0559eb8c39c6591373e Mon Sep 17 00:00:00 2001 From: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> Date: Sun, 8 Dec 2024 21:48:40 +1100 Subject: [PATCH] Backend: Add the regex 101 link when a pattern fails (#3034) --- detekt/src/main/kotlin/RepoPatternElement.kt | 7 +++++++ detekt/src/main/kotlin/repo/RepoPatternRegexTest.kt | 6 ++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/detekt/src/main/kotlin/RepoPatternElement.kt b/detekt/src/main/kotlin/RepoPatternElement.kt index 3f400fa25693..2da17ce07737 100644 --- a/detekt/src/main/kotlin/RepoPatternElement.kt +++ b/detekt/src/main/kotlin/RepoPatternElement.kt @@ -8,6 +8,7 @@ import org.jetbrains.kotlin.psi.KtProperty import org.jetbrains.kotlin.psi.KtPropertyDelegate import org.jetbrains.kotlin.psi.KtStringTemplateEntryWithExpression import org.jetbrains.kotlin.psi.KtStringTemplateExpression +import java.net.URLEncoder class RepoPatternElement private constructor( val variableName: String, @@ -18,6 +19,12 @@ class RepoPatternElement private constructor( val pattern by lazy { rawPattern.toPattern() } + val regex101Url: String by lazy { + val encodedPattern = URLEncoder.encode(rawPattern, "UTF-8") + val encodedTests = regexTests.joinToString("\n") { URLEncoder.encode(it, "UTF-8") } + "https://regex101.com/?regex=$encodedPattern&testString=$encodedTests" + } + companion object { fun KtPropertyDelegate.asRepoPatternElement(): RepoPatternElement? { val expression = this.expression as? KtDotQualifiedExpression ?: return null diff --git a/detekt/src/main/kotlin/repo/RepoPatternRegexTest.kt b/detekt/src/main/kotlin/repo/RepoPatternRegexTest.kt index ac68ed5e4a3d..92fc5e1943dc 100644 --- a/detekt/src/main/kotlin/repo/RepoPatternRegexTest.kt +++ b/detekt/src/main/kotlin/repo/RepoPatternRegexTest.kt @@ -32,13 +32,15 @@ class RepoPatternRegexTest(config: Config) : SkyHanniRule(config) { repoPatternElement.regexTests.forEach { test -> if (!repoPatternElement.pattern.matcher(test).find()) { - delegate.reportIssue("Repo pattern `$variableName` failed regex test: `$test` pattern: `$rawPattern`.") + delegate.reportIssue("Repo pattern `$variableName` failed regex test: `$test` pattern: `$rawPattern`. " + + "[View on Regex101](${repoPatternElement.regex101Url})") } } repoPatternElement.failingRegexTests.forEach { test -> if (repoPatternElement.pattern.matcher(test).find()) { - delegate.reportIssue("Repo pattern `$variableName` passed regex test: `$test` pattern: `$rawPattern` even though it was set to fail.") + delegate.reportIssue("Repo pattern `$variableName` passed regex test: `$test` pattern: `$rawPattern` " + + "even though it was set to fail. [View on Regex101](${repoPatternElement.regex101Url})") } } }