1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-03-22 05:09:12 +00:00

maven_artifact: resolve SNAPSHOT to latest using snapshot metadata block (#11501)

* fix(maven_artifact): resolve SNAPSHOT to latest using snapshot metadata block

Prefer the <snapshot> block (timestamp + buildNumber) from maven-metadata.xml
which always points to the latest build, instead of scanning <snapshotVersions>
and returning on the first match. Repositories like GitHub Packages keep all
historical entries in <snapshotVersions> (oldest first), causing the module to
resolve to the oldest snapshot instead of the latest.

Fixes #5117
Fixes #11489

* fix(maven_artifact): address review feedback

- Check both timestamp and buildNumber before using snapshot block,
  preventing IndexError when buildNumber is missing
- Remove unreliable snapshotVersions scanning fallback; use literal
  -SNAPSHOT version for non-unique snapshot repos instead
- Add tests for incomplete snapshot block and non-SNAPSHOT versions

* fix(maven_artifact): restore snapshotVersions scanning with last-match

Restore <snapshotVersions> scanning as primary resolution (needed for
per-extension accuracy per MNG-5459), but collect the last match instead
of returning on the first. Fall back to <snapshot> block when no
<snapshotVersions> match is found, then to literal -SNAPSHOT version.

* docs: update changelog fragment to match final implementation

* fix(maven_artifact): use updated timestamp for snapshot resolution

Use the <updated> attribute to select the newest snapshotVersion entry
instead of relying on list order. This works independently of how the
repository manager sorts entries in maven-metadata.xml.

Also fix test docstring and update changelog fragment per reviewer
feedback.

* test(maven_artifact): shuffle entries to verify updated timestamp sorting

Reorder snapshotVersion entries so the newest JAR is in the middle,
not at the end. This ensures the test actually validates that resolution
uses the <updated> timestamp rather than relying on list position.
This commit is contained in:
Adam R. 2026-02-14 21:03:00 +01:00 committed by GitHub
parent c9313af971
commit ed7ccbe3d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 182 additions and 9 deletions

View file

@ -465,17 +465,25 @@ class MavenDownloader:
content = self._getContent(self.base + path, f"Failed to retrieve the maven metadata file: {path}")
xml = etree.fromstring(content)
for snapshotArtifact in xml.xpath("/metadata/versioning/snapshotVersions/snapshotVersion"):
classifier = snapshotArtifact.xpath("classifier/text()")
artifact_classifier = classifier[0] if classifier else ""
extension = snapshotArtifact.xpath("extension/text()")
artifact_extension = extension[0] if extension else ""
if artifact_classifier == artifact.classifier and artifact_extension == artifact.extension:
return self._uri_for_artifact(artifact, snapshotArtifact.xpath("value/text()")[0])
candidates = []
for snapshot_artifact in xml.xpath("/metadata/versioning/snapshotVersions/snapshotVersion"):
classifier = snapshot_artifact.xpath("classifier/text()")
extension = snapshot_artifact.xpath("extension/text()")
if (classifier[0] if classifier else "") == artifact.classifier and (
extension[0] if extension else ""
) == artifact.extension:
value = snapshot_artifact.xpath("value/text()")
updated = snapshot_artifact.xpath("updated/text()")
if value:
candidates.append((updated[0] if updated else "", value[0]))
if candidates:
# updated is yyyymmddHHMMSS, so lexical max == newest
return self._uri_for_artifact(artifact, max(candidates, key=lambda item: item[0])[1])
timestamp_xmlpath = xml.xpath("/metadata/versioning/snapshot/timestamp/text()")
if timestamp_xmlpath:
build_number_xmlpath = xml.xpath("/metadata/versioning/snapshot/buildNumber/text()")
if timestamp_xmlpath and build_number_xmlpath:
timestamp = timestamp_xmlpath[0]
build_number = xml.xpath("/metadata/versioning/snapshot/buildNumber/text()")[0]
build_number = build_number_xmlpath[0]
return self._uri_for_artifact(
artifact, artifact.version.replace("SNAPSHOT", f"{timestamp}-{build_number}")
)