-
Notifications
You must be signed in to change notification settings - Fork 315
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(analyzer): Support uppercase-letters in Go module version #7888
fix(analyzer): Support uppercase-letters in Go module version #7888
Conversation
Thanks for the contribution! Please add |
plugins/package-managers/go/src/funTest/kotlin/utils/GoSupportFunTest.kt
Outdated
Show resolved
Hide resolved
plugins/package-managers/go/src/funTest/kotlin/utils/GoSupportFunTest.kt
Outdated
Show resolved
Hide resolved
plugins/package-managers/go/src/funTest/kotlin/utils/GoSupportFunTest.kt
Outdated
Show resolved
Hide resolved
plugins/package-managers/go/src/funTest/kotlin/utils/GoSupportFunTest.kt
Outdated
Show resolved
Hide resolved
plugins/package-managers/go/src/funTest/kotlin/utils/GoSupportFunTest.kt
Outdated
Show resolved
Hide resolved
Nice finding, thank @wkl3nk ! |
plugins/package-managers/go/src/funTest/kotlin/utils/GoSupportFunTest.kt
Outdated
Show resolved
Hide resolved
plugins/package-managers/go/src/funTest/kotlin/utils/GoSupportFunTest.kt
Outdated
Show resolved
Hide resolved
plugins/package-managers/go/src/funTest/kotlin/utils/GoSupportFunTest.kt
Outdated
Show resolved
Hide resolved
val escaped = StringBuilder() | ||
for (char in version) { | ||
if (char.isUpperCase()) { | ||
escaped.append("!${char.lowercaseChar()}") | ||
} else { | ||
escaped.append(char) | ||
} | ||
} | ||
|
||
return escaped.toString() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternative implementation:
return version.replace(Regex("[A-Z]")) {
"!${it.value.lowercase()}"
}
This could also extract Regex("[A-Z]")
to a private property to the Regex is not recreated on each function call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Come on. Don't need a Regex for such an easy thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure you don't. But I'd bet it's even faster (if the Regex is precompiled to a constant) and it's more readable (or at least shorter, if you're not used to Regexes) than your solution that manually iterates over each character in the string. But I'm also fine with keeping your approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, using a Regex now, extended the test case to make sure all patterns matching are replaced.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #7888 +/- ##
============================================
- Coverage 66.96% 66.90% -0.07%
Complexity 2041 2041
============================================
Files 356 356
Lines 17084 17100 +16
Branches 2443 2443
============================================
Hits 11440 11440
- Misses 4623 4639 +16
Partials 1021 1021
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
@sschuberth @fviernau Thanks for your comments, would you have a look at the PR again, please? And don't be too hard, I did not code Kotlin before ... |
Head branch was pushed to by a user without write access
@@ -24,4 +24,4 @@ package org.ossreviewtoolkit.plugins.packagemanagers.go.utils | |||
* also any suffix starting with '+', because build metadata is not involved in version comparison according to | |||
* https://go.dev/ref/mod#incompatible-versions. | |||
*/ | |||
fun normalizeModuleVersion(moduleVersion: String): String = moduleVersion.removePrefix("v").substringBefore("+") | |||
internal fun normalizeModuleVersion(moduleVersion: String): String = moduleVersion.removePrefix("v").substringBefore("+") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO the consistency argument (as written) has vanished a bit, since escapeVersion()
no more is in this file.
In my view, reducing visibility does not necessarily be done in this PR. But I'm ok to keep it.
However, the commit message should adhere to conventional commits.
I propose to just say
refactor(go)!: Reduce the visibility of `normalizeModuleVersion()`
There is no need to expose this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Removed the change, so this way sticking to just one commit in the PR.
internal fun escapeVersion(version: String): String { | ||
require("!" !in version) { "Module versions must not contain exclamation marks: $version"} | ||
|
||
val escaped = buildString { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: You can omit the escaped
variable and directly return from here: return buildString {
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
"escapeVersion" should { | ||
"escape uppercase letters" { | ||
val version = "v0.1.0-M4.0.20231102094829-08e0c3cd016c" | ||
val escapedVersion = escapeVersion(version) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: The empty line before this one is still missing (the code that performs the action to test should stand by its own).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, I'll fixup some nits as a follow-up.
I'll make my nit-fixes in-place in this PR as detekt fails. |
Uppercase-letters in version strings of dependent Go modules caused the analyzer to crash. With this fix, uppercase-letters are now properly escaped as any other paths of the Go modules in the file system. Fixes #7880. Signed-off-by: Wolfgang Klenk <wolfgang.klenk2@bosch.com>
Uppercase-letters in version strings of dependent Go modules caused the analyzer to crash.
With this fix, uppercase-letters are now properly escaped as any other paths of the Go modules in the file system.