From e15caab90b445b1d28ee686efb0697002b562434 Mon Sep 17 00:00:00 2001 From: Markus Hoffrogge Date: Mon, 25 Nov 2024 21:00:30 +0100 Subject: [PATCH] fix JENKINS-69410: Links to Attached Artifacts broken (#854) * fix JENKINS-69410: Links to Attached Artifacts broken - pipeline-maven: - pipeline/maven/util/XmlUtils.java: - add missing attachedMavenArtifact.setVersion(version from deployed artifact) - pipeline-maven-api: - pipeline/maven/MavenArtifact.java: - avoid double // in deployed artifact URL if deploy repository URL ends with / * fix unit tests --------- Co-authored-by: Benoit GUERIN --- .../plugins/pipeline/maven/MavenArtifact.java | 5 ++-- .../plugins/pipeline/maven/util/XmlUtils.java | 3 +++ .../pipeline/maven/util/XmlUtilsTest.java | 27 ++++++++++--------- .../pipeline/maven/maven-spy-deploy-jar.xml | 4 +-- ... maven-spy-include-attached-artifacts.xml} | 4 +-- 5 files changed, 25 insertions(+), 18 deletions(-) rename pipeline-maven/src/test/resources/org/jenkinsci/plugins/pipeline/maven/{maven-spy-include-attached-artifacts.log => maven-spy-include-attached-artifacts.xml} (99%) diff --git a/pipeline-maven-api/src/main/java/org/jenkinsci/plugins/pipeline/maven/MavenArtifact.java b/pipeline-maven-api/src/main/java/org/jenkinsci/plugins/pipeline/maven/MavenArtifact.java index 7bf27f88..93b57610 100644 --- a/pipeline-maven-api/src/main/java/org/jenkinsci/plugins/pipeline/maven/MavenArtifact.java +++ b/pipeline-maven-api/src/main/java/org/jenkinsci/plugins/pipeline/maven/MavenArtifact.java @@ -137,8 +137,9 @@ public String getShortDescription() { @Nullable public String getUrl() { if (getRepositoryUrl() == null) return null; - return getRepositoryUrl() + "/" + getGroupId().replace('.', '/') + "/" + getArtifactId() + "/" - + getBaseVersion() + "/" + getFileNameWithVersion(); + return getRepositoryUrl() + (getRepositoryUrl().endsWith("/") ? "" : "/") + + getGroupId().replace('.', '/') + "/" + getArtifactId() + "/" + getBaseVersion() + "/" + + getFileNameWithVersion(); } @Override diff --git a/pipeline-maven/src/main/java/org/jenkinsci/plugins/pipeline/maven/util/XmlUtils.java b/pipeline-maven/src/main/java/org/jenkinsci/plugins/pipeline/maven/util/XmlUtils.java index 845f008a..bdbc59b0 100644 --- a/pipeline-maven/src/main/java/org/jenkinsci/plugins/pipeline/maven/util/XmlUtils.java +++ b/pipeline-maven/src/main/java/org/jenkinsci/plugins/pipeline/maven/util/XmlUtils.java @@ -545,6 +545,9 @@ public static List listGeneratedArtifacts(Element mavenSpyLogs, b if (attachedArtifactDeployedEvent == null) { // artifact has not been deployed ("mvn deploy") } else { + attachedMavenArtifact.setVersion( + XmlUtils.getUniqueChildElement(attachedArtifactDeployedEvent, "artifact") + .getAttribute("version")); attachedMavenArtifact.setRepositoryUrl( XmlUtils.getUniqueChildElement(attachedArtifactDeployedEvent, "repository") .getAttribute("url")); diff --git a/pipeline-maven/src/test/java/org/jenkinsci/plugins/pipeline/maven/util/XmlUtilsTest.java b/pipeline-maven/src/test/java/org/jenkinsci/plugins/pipeline/maven/util/XmlUtilsTest.java index b15872da..83ed7022 100644 --- a/pipeline-maven/src/test/java/org/jenkinsci/plugins/pipeline/maven/util/XmlUtilsTest.java +++ b/pipeline-maven/src/test/java/org/jenkinsci/plugins/pipeline/maven/util/XmlUtilsTest.java @@ -242,7 +242,7 @@ public void test_getExecutedLifecyclePhases() throws Exception { InputStream in = Thread.currentThread() .getContextClassLoader() .getResourceAsStream("org/jenkinsci/plugins/pipeline/maven/maven-spy-package-jar.xml"); - in.getClass(); // check non null + assertThat(in).isNotNull(); Element mavenSpyLogs = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(in) @@ -258,7 +258,7 @@ public void test_getArtifactDeployedEvent() throws Exception { InputStream in = Thread.currentThread() .getContextClassLoader() .getResourceAsStream("org/jenkinsci/plugins/pipeline/maven/maven-spy-deploy-jar.xml"); - in.getClass(); // check non null + assertThat(in).isNotNull(); Element mavenSpyLogs = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(in) @@ -278,7 +278,7 @@ public void test_getExecutionEventsByPlugin() throws Exception { InputStream in = Thread.currentThread() .getContextClassLoader() .getResourceAsStream("org/jenkinsci/plugins/pipeline/maven/maven-spy-deploy-jar.xml"); - in.getClass(); // check non null + assertThat(in).isNotNull(); Element mavenSpyLogs = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(in) @@ -302,13 +302,12 @@ public void test_listGeneratedArtifacts() throws Exception { InputStream in = Thread.currentThread() .getContextClassLoader() .getResourceAsStream("org/jenkinsci/plugins/pipeline/maven/maven-spy-deploy-jar.xml"); - in.getClass(); // check non null + assertThat(in).isNotNull(); Element mavenSpyLogs = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(in) .getDocumentElement(); List generatedArtifacts = XmlUtils.listGeneratedArtifacts(mavenSpyLogs, false); - System.out.println(generatedArtifacts); assertThat(generatedArtifacts.size()).isEqualTo(2); // a jar file and a pom file are generated for (MavenArtifact mavenArtifact : generatedArtifacts) { @@ -331,7 +330,7 @@ public void test_listGeneratedArtifacts_deploy_2_8() throws Exception { InputStream in = Thread.currentThread() .getContextClassLoader() .getResourceAsStream("org/jenkinsci/plugins/pipeline/maven/maven-spy-deploy-2.8.xml"); - in.getClass(); // check non null + assertThat(in).isNotNull(); Element mavenSpyLogs = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(in) @@ -365,7 +364,7 @@ public void test_listGeneratedArtifacts_deploy_3_0() throws Exception { InputStream in = Thread.currentThread() .getContextClassLoader() .getResourceAsStream("org/jenkinsci/plugins/pipeline/maven/maven-spy-deploy-3.0.xml"); - in.getClass(); // check non null + assertThat(in).isNotNull(); Element mavenSpyLogs = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(in) @@ -399,18 +398,20 @@ public void test_listGeneratedArtifacts_including_generated_artifacts() throws E InputStream in = Thread.currentThread() .getContextClassLoader() .getResourceAsStream("org/jenkinsci/plugins/pipeline/maven/maven-spy-deploy-jar.xml"); - in.getClass(); // check non null + assertThat(in).isNotNull(); Element mavenSpyLogs = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(in) .getDocumentElement(); List generatedArtifacts = XmlUtils.listGeneratedArtifacts(mavenSpyLogs, true); - System.out.println(generatedArtifacts); assertThat(generatedArtifacts.size()).isEqualTo(3); // a jar file and a pom file are generated for (MavenArtifact mavenArtifact : generatedArtifacts) { assertThat(mavenArtifact.getGroupId()).isEqualTo("com.example"); assertThat(mavenArtifact.getArtifactId()).isEqualTo("my-jar"); + assertThat(mavenArtifact.getBaseVersion()).isEqualTo("0.5-SNAPSHOT"); + assertThat(mavenArtifact.getVersion()).isEqualTo("0.5-20180304.184830-1"); + assertThat(mavenArtifact.isSnapshot()).isTrue(); if ("pom".equals(mavenArtifact.getType())) { assertThat(mavenArtifact.getExtension()).isEqualTo("pom"); assertThat(mavenArtifact.getClassifier()).isNullOrEmpty(); @@ -430,19 +431,21 @@ public void test_listGeneratedArtifacts_including_generated_artifacts() throws E public void test_listGeneratedArtifacts_includeAttachedArtifacts() throws Exception { InputStream in = Thread.currentThread() .getContextClassLoader() - .getResourceAsStream("org/jenkinsci/plugins/pipeline/maven/maven-spy-include-attached-artifacts.log"); - in.getClass(); // check non null + .getResourceAsStream("org/jenkinsci/plugins/pipeline/maven/maven-spy-include-attached-artifacts.xml"); + assertThat(in).isNotNull(); Element mavenSpyLogs = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(in) .getDocumentElement(); List generatedArtifacts = XmlUtils.listGeneratedArtifacts(mavenSpyLogs, true); - System.out.println(generatedArtifacts); assertThat(generatedArtifacts.size()).isEqualTo(2); // pom artifact plus 1 attachment for (MavenArtifact mavenArtifact : generatedArtifacts) { assertThat(mavenArtifact.getGroupId()).isEqualTo("com.example"); assertThat(mavenArtifact.getArtifactId()).isEqualTo("my-jar"); + assertThat(mavenArtifact.getBaseVersion()).isEqualTo("0.5-SNAPSHOT"); + assertThat(mavenArtifact.getVersion()).isEqualTo("0.5-20180410.070244-14"); + assertThat(mavenArtifact.isSnapshot()).isTrue(); if ("pom".equals(mavenArtifact.getType())) { assertThat(mavenArtifact.getExtension()).isEqualTo("pom"); assertThat(mavenArtifact.getClassifier()).isNullOrEmpty(); diff --git a/pipeline-maven/src/test/resources/org/jenkinsci/plugins/pipeline/maven/maven-spy-deploy-jar.xml b/pipeline-maven/src/test/resources/org/jenkinsci/plugins/pipeline/maven/maven-spy-deploy-jar.xml index 53cc1091..4738bbf1 100644 --- a/pipeline-maven/src/test/resources/org/jenkinsci/plugins/pipeline/maven/maven-spy-deploy-jar.xml +++ b/pipeline-maven/src/test/resources/org/jenkinsci/plugins/pipeline/maven/maven-spy-deploy-jar.xml @@ -961,11 +961,11 @@ - + /path/to/my-jar/target/my-jar-0.5-SNAPSHOT.jar - + /path/to/my-jar/target/my-jar-0.5-SNAPSHOT-sources.jar diff --git a/pipeline-maven/src/test/resources/org/jenkinsci/plugins/pipeline/maven/maven-spy-include-attached-artifacts.log b/pipeline-maven/src/test/resources/org/jenkinsci/plugins/pipeline/maven/maven-spy-include-attached-artifacts.xml similarity index 99% rename from pipeline-maven/src/test/resources/org/jenkinsci/plugins/pipeline/maven/maven-spy-include-attached-artifacts.log rename to pipeline-maven/src/test/resources/org/jenkinsci/plugins/pipeline/maven/maven-spy-include-attached-artifacts.xml index 8b3e5009..218dba02 100644 --- a/pipeline-maven/src/test/resources/org/jenkinsci/plugins/pipeline/maven/maven-spy-include-attached-artifacts.log +++ b/pipeline-maven/src/test/resources/org/jenkinsci/plugins/pipeline/maven/maven-spy-include-attached-artifacts.xml @@ -185,11 +185,11 @@ - + - + /Users/Shared/Jenkins/Home/workspace/my-jar/my-jar-0.5-SNAPSHOT.ova