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(analyzer): Support uppercase-letters in Go module version #7888

Merged
merged 1 commit into from
Nov 20, 2023
Merged

fix(analyzer): Support uppercase-letters in Go module version #7888

merged 1 commit into from
Nov 20, 2023

Conversation

wkl3nk
Copy link
Contributor

@wkl3nk wkl3nk commented Nov 17, 2023

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.

@wkl3nk wkl3nk requested a review from a team as a code owner November 17, 2023 10:08
@sschuberth
Copy link
Member

Thanks for the contribution! Please add Fixes #7880. as a trailing paragraph to the commit message body in order to auto-close the issue upon merge.

@fviernau
Copy link
Member

Nice finding, thank @wkl3nk !

Comment on lines 38 to 47
val escaped = StringBuilder()
for (char in version) {
if (char.isUpperCase()) {
escaped.append("!${char.lowercaseChar()}")
} else {
escaped.append(char)
}
}

return escaped.toString()
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link

codecov bot commented Nov 17, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (67194c5) 66.96% compared to head (c5342de) 66.90%.

❗ Current head c5342de differs from pull request most recent head 655eada. Consider uploading reports for the commit 655eada to get more accurate results

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              
Flag Coverage Δ
test 36.04% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@wkl3nk
Copy link
Contributor Author

wkl3nk commented Nov 17, 2023

@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 ...

fviernau
fviernau previously approved these changes Nov 17, 2023
@fviernau fviernau enabled auto-merge (rebase) November 17, 2023 15:16
auto-merge was automatically disabled November 17, 2023 15:20

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("+")
Copy link
Member

@fviernau fviernau Nov 17, 2023

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.

Copy link
Contributor Author

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 {
Copy link
Member

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 {.

Copy link
Contributor Author

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)
Copy link
Member

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).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@wkl3nk wkl3nk requested a review from sschuberth November 20, 2023 09:50
sschuberth
sschuberth previously approved these changes Nov 20, 2023
Copy link
Member

@sschuberth sschuberth left a 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.

fviernau
fviernau previously approved these changes Nov 20, 2023
@fviernau fviernau enabled auto-merge (rebase) November 20, 2023 10:23
@sschuberth
Copy link
Member

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>
@sschuberth sschuberth dismissed stale reviews from fviernau and themself via 655eada November 20, 2023 10:38
@fviernau fviernau merged commit a9bd271 into oss-review-toolkit:main Nov 20, 2023
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants