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

GradleInspector: Ignore artifacts of zero byte size #8305

Merged
merged 2 commits into from
Feb 19, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ private val GRADLE_USER_HOME = Os.env["GRADLE_USER_HOME"]?.let { File(it) } ?: O
*/
const val OPTION_GRADLE_VERSION = "gradleVersion"

/**
Copy link
Member

@sschuberth sschuberth Feb 19, 2024

Choose a reason for hiding this comment

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

Commit message nits:

  • Should we say "Maven" instead of "Gradle" artifacts?
  • Typo in "rot" -> "root".

* The sha1 sum for a zero by size file.
*/
private const val ZERO_BYTES_FILE_SHA1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709"
Copy link
Member

Choose a reason for hiding this comment

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

We already have this as EMPTY_PACKAGE_VERIFICATION_CODE. How about refactoring that first to a constant in HashAlgorithm?

Copy link
Member Author

Choose a reason for hiding this comment

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

EMPTY_PACKAGE_VERIFICATION_CODE is in spdx-utils and does not know about the HashAlgorithm class. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

It's only used in UtilsTest which could add the model as a dependency. I'll propose a follow-up refactoring.


/**
* The [Gradle](https://gradle.org/) package manager for Java.
*
Expand Down Expand Up @@ -350,15 +355,15 @@ private fun Collection<OrtDependency>.toPackageRefs(
}

/**
* Create a [RemoteArtifact] based on the given [pomUrl], [classifier], [extension] and hash [algorithm]. The hash value
* is retrieved remotely.
* Create a [RemoteArtifact] based on the given [pomUrl], [classifier] and [extension]. The hash value is retrieved
* remotely.
*/
private fun createRemoteArtifact(
private fun GradleInspector.createRemoteArtifact(
pomUrl: String?,
classifier: String? = null,
extension: String? = null,
algorithm: String = "sha1"
extension: String? = null
): RemoteArtifact {
val algorithm = "sha1"
val artifactBaseUrl = pomUrl?.removeSuffix(".pom") ?: return RemoteArtifact.EMPTY

val artifactUrl = buildString {
Expand All @@ -371,7 +376,14 @@ private fun createRemoteArtifact(
val checksum = okHttpClient.downloadText("$artifactUrl.$algorithm")
.getOrElse { return RemoteArtifact.EMPTY }

return RemoteArtifact(artifactUrl, parseChecksum(checksum, algorithm))
// Ignore file with zero byte size, because it cannot be a valid archive.
val hash = parseChecksum(checksum, algorithm).takeUnless { it.value == ZERO_BYTES_FILE_SHA1 }
?: run {
logger.info("Ignoring zero byte size artifact: $artifactUrl.")
return RemoteArtifact.EMPTY
}

return RemoteArtifact(artifactUrl, hash)
}

/**
Expand Down
Loading