Skip to content
Merged
Show file tree
Hide file tree
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 @@ -43,7 +43,7 @@
*/
public class AgentDownloader {

private static final String VERSION_EXTRACTION_REGEX = "<a href.+>([0-9]+.[0-9.]+.[0-9.]+)/</a>";
private static final Pattern VERSION_EXTRACTION_REGEX = Pattern.compile("<version>(.+?)</version>");
private static final String AGENT_GROUP_ID = "co.elastic.apm";
private static final String AGENT_ARTIFACT_ID = "elastic-apm-agent";
private static final String CLI_JAR_VERSION;
Expand Down Expand Up @@ -220,25 +220,27 @@ void verifyPgpSignature(final Path agentJarFile, final String mavenAgentUrlStrin
}

static String findLatestVersion() throws Exception {
String agentArtifactMavenBaseUrl = getAgentArtifactMavenBaseUrl();
HttpURLConnection httpURLConnection = openConnection(agentArtifactMavenBaseUrl);
TreeSet<Version> versions = parseMavenArtifactHtml(httpURLConnection.getInputStream());
String agentArtifactMavenMetadatUrl = getAgentArtifactMavenBaseUrl() + "/maven-metadata.xml";
HttpURLConnection httpURLConnection = openConnection(agentArtifactMavenMetadatUrl);
TreeSet<Version> versions = parseMavenMetadataXml(httpURLConnection.getInputStream());
if (versions.isEmpty()) {
throw new IllegalStateException("Failed to parse agent versions from the contents of " + agentArtifactMavenBaseUrl);
throw new IllegalStateException("Failed to parse agent versions from the contents of " + agentArtifactMavenMetadatUrl);
}
return versions.last().toString();
}

static TreeSet<Version> parseMavenArtifactHtml(InputStream htmlInputStream) throws IOException {
static TreeSet<Version> parseMavenMetadataXml(InputStream htmlInputStream) throws IOException {
TreeSet<Version> versions = new TreeSet<>();
BufferedReader versionsHtmlReader = new BufferedReader(new InputStreamReader(htmlInputStream));
Pattern pattern = Pattern.compile(VERSION_EXTRACTION_REGEX);
String line;
while ((line = versionsHtmlReader.readLine()) != null) {
try {
Matcher matcher = pattern.matcher(line);
Matcher matcher = VERSION_EXTRACTION_REGEX.matcher(line);
if (matcher.find()) {
versions.add(Version.of(matcher.group(1)));
Version version = Version.of(matcher.group(1));
if (!version.hasSuffix()) {
versions.add(version);
}
}
} catch (Exception e) {
// ignore, probably a regex false positive
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ void testLatestVersion() throws Exception {

@Test
void testAgentArtifactMavenPageParsing() throws IOException {
TreeSet<Version> versions = AgentDownloader.parseMavenArtifactHtml(AgentDownloaderTest.class.getResourceAsStream(
"/MavenCentral_agent_artifact.html"));
TreeSet<Version> versions = AgentDownloader.parseMavenMetadataXml(AgentDownloaderTest.class.getResourceAsStream(
"/maven-metadata.xml"));
assertThat(versions).hasSize(50);
assertThat(versions.first().toString()).isEqualTo("0.5.1");
assertThat(versions.last().toString()).isEqualTo("1.31.0");
assertThat(versions).doesNotContain(Version.of("1.0.0.RC1"));
}
}

This file was deleted.

65 changes: 65 additions & 0 deletions apm-agent-attach-cli/src/test/resources/maven-metadata.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<metadata>
<groupId>co.elastic.apm</groupId>
<artifactId>elastic-apm-agent</artifactId>
<versioning>
<latest>1.31.0</latest>
<release>1.31.0</release>
<versions>
<version>0.5.1</version>
<version>0.6.0</version>
<version>0.6.1</version>
<version>0.6.2</version>
<version>0.7.0</version>
<version>0.7.1</version>
<version>1.0.0.RC1</version>
<version>1.0.0</version>
<version>1.0.1</version>
<version>1.1.0</version>
<version>1.2.0</version>
<version>1.3.0</version>
<version>1.4.0</version>
<version>1.5.0</version>
<version>1.6.0</version>
<version>1.6.1</version>
<version>1.7.0</version>
<version>1.8.0</version>
<version>1.9.0</version>
<version>1.10.0</version>
<version>1.10.1</version>
<version>1.11.0</version>
<version>1.12.0</version>
<version>1.13.0</version>
<version>1.14.0</version>
<version>1.15.0</version>
<version>1.16.0</version>
<version>1.17.0</version>
<version>1.18.0.RC1</version>
<version>1.18.0</version>
<version>1.18.1</version>
<version>1.19.0</version>
<version>1.20.0</version>
<version>1.21.0</version>
<version>1.22.0</version>
<version>1.23.0</version>
<version>1.24.0</version>
<version>1.25.0</version>
<version>1.26.0</version>
<version>1.26.1</version>
<version>1.26.2</version>
<version>1.27.0</version>
<version>1.27.1</version>
<version>1.28.0</version>
<version>1.28.1</version>
<version>1.28.2</version>
<version>1.28.3</version>
<version>1.28.4</version>
<version>1.29.0</version>
<version>1.30.0</version>
<version>1.30.1</version>
<version>1.31.0</version>
<version>1.32.0-RC1</version>
<version>1.32.0.RC1</version>
</versions>
<lastUpdated>20220517064838</lastUpdated>
</versioning>
</metadata>
Loading