From 1223ad4589caec6851b91b50b5db6a1559ce3487 Mon Sep 17 00:00:00 2001 From: Eduard Tita <49167996+eduard-tita@users.noreply.github.com> Date: Wed, 19 Jul 2023 12:47:14 -0400 Subject: [PATCH] Better logging for upload failures - CLM-25075 (#264) --- .../nexus/ci/nxrm/ComponentUploader.groovy | 12 +++++- .../ci/nxrm/v2/ComponentUploaderNxrm2.groovy | 16 +++----- .../ci/nxrm/v3/ComponentUploaderNxrm3.groovy | 23 ++++-------- .../ci/nxrm/ComponentUploaderNxrm2Test.groovy | 34 +++++++++++++++++ .../ci/nxrm/ComponentUploaderNxrm3Test.groovy | 37 +++++++++++++++++++ 5 files changed, 93 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/sonatype/nexus/ci/nxrm/ComponentUploader.groovy b/src/main/java/org/sonatype/nexus/ci/nxrm/ComponentUploader.groovy index 8ecebf31..c0c959f8 100644 --- a/src/main/java/org/sonatype/nexus/ci/nxrm/ComponentUploader.groovy +++ b/src/main/java/org/sonatype/nexus/ci/nxrm/ComponentUploader.groovy @@ -43,7 +43,7 @@ abstract class ComponentUploader protected abstract void upload(final Map> remoteMavenComponents, final String nxrmRepositoryId, - final String tagName = null) + final String tagName = null) throws IOException @SuppressWarnings(['UnusedMethodParameter', 'EmptyMethodInAbstractClass']) void maybeCreateTag(final String tagName) { @@ -79,7 +79,15 @@ abstract class ComponentUploader } } - upload(remoteMavenComponents, nexusPublisher.nexusRepositoryId, tagName) + try { + upload(remoteMavenComponents, nexusPublisher.nexusRepositoryId, tagName) + } + catch (IOException ex) { + logger.println("${ex.getMessage()} - cause: ${ex.getCause()}") + logger.println('Failing build due to failure to upload file to Nexus Repository Manager Publisher') + run.setResult(Result.FAILURE) + throw ex + } } @SuppressWarnings('Instanceof') diff --git a/src/main/java/org/sonatype/nexus/ci/nxrm/v2/ComponentUploaderNxrm2.groovy b/src/main/java/org/sonatype/nexus/ci/nxrm/v2/ComponentUploaderNxrm2.groovy index e9150b42..14c4eda4 100644 --- a/src/main/java/org/sonatype/nexus/ci/nxrm/v2/ComponentUploaderNxrm2.groovy +++ b/src/main/java/org/sonatype/nexus/ci/nxrm/v2/ComponentUploaderNxrm2.groovy @@ -38,7 +38,7 @@ class ComponentUploaderNxrm2 @Override void upload(final Map> remoteMavenComponents, final String nxrmRepositoryId, - final String tag = null) + final String tag = null) throws IOException { def nxrmClient = getRepositoryManagerClient(nxrmConfiguration) @@ -61,20 +61,14 @@ class ComponentUploaderNxrm2 try { nxrmClient.uploadComponent(nxrmRepositoryId, gav, [mavenFile]) } - catch (RepositoryManagerException ex) { - throw new IOException(ex) - } finally { localFile.delete() } } - catch (IOException ex) { - final String uploadFailed = "Upload of ${remoteMavenAsset.Asset.filePath} failed" - - logger.println(uploadFailed) - logger.println('Failing build due to failure to upload file to Nexus Repository Manager Publisher') - run.setResult(Result.FAILURE) - throw new IOException(uploadFailed, ex) + catch (RepositoryManagerException | IOException ex) { + throw new IOException( + "Upload of ${remoteMavenAsset.Asset.filePath} failed due to ${ex.getMessage()}", + ex) } } diff --git a/src/main/java/org/sonatype/nexus/ci/nxrm/v3/ComponentUploaderNxrm3.groovy b/src/main/java/org/sonatype/nexus/ci/nxrm/v3/ComponentUploaderNxrm3.groovy index 3421f32f..fb337450 100644 --- a/src/main/java/org/sonatype/nexus/ci/nxrm/v3/ComponentUploaderNxrm3.groovy +++ b/src/main/java/org/sonatype/nexus/ci/nxrm/v3/ComponentUploaderNxrm3.groovy @@ -46,7 +46,7 @@ class ComponentUploaderNxrm3 @Override void upload(final Map> remoteMavenComponents, final String nxrmRepositoryId, - final String tagName = null) + final String tagName = null) throws IOException { def nxrmClient = getRepositoryManagerClient(nxrmConfiguration) @@ -76,22 +76,13 @@ class ComponentUploaderNxrm3 envVars.expand(remoteMavenAsset.Asset.classifier)) } - try { - nxrmClient. - upload(nxrmRepositoryId, mavenComponentBuilder.build(), resolvedTagName?.trim() ? resolvedTagName : null) - } - catch (RepositoryManagerException ex) { - throw new IOException(ex) - } + nxrmClient. + upload(nxrmRepositoryId, mavenComponentBuilder.build(), resolvedTagName?.trim() ? resolvedTagName : null) } - catch (IOException ex) { - final String uploadFailed = 'Upload of maven component with GAV ' + - "[${groupId}:${artifactId}:${version}] failed" - - logger.println(uploadFailed) - logger.println('Failing build due to failure to upload file to Nexus Repository Manager Publisher') - run.setResult(Result.FAILURE) - throw new IOException(uploadFailed, ex) + catch (RepositoryManagerException | IOException ex) { + throw new IOException( + "Upload of maven component with GAV [${groupId}:${artifactId}:${version}] failed due to ${ex.getMessage()}", + ex) } } diff --git a/src/test/java/org/sonatype/nexus/ci/nxrm/ComponentUploaderNxrm2Test.groovy b/src/test/java/org/sonatype/nexus/ci/nxrm/ComponentUploaderNxrm2Test.groovy index 902cfda3..9b23f4b1 100644 --- a/src/test/java/org/sonatype/nexus/ci/nxrm/ComponentUploaderNxrm2Test.groovy +++ b/src/test/java/org/sonatype/nexus/ci/nxrm/ComponentUploaderNxrm2Test.groovy @@ -12,6 +12,7 @@ */ package org.sonatype.nexus.ci.nxrm +import com.sonatype.nexus.api.exception.RepositoryManagerException import com.sonatype.nexus.api.repository.v2.RepositoryManagerV2Client import org.sonatype.nexus.ci.config.Nxrm2Configuration @@ -157,6 +158,39 @@ class ComponentUploaderNxrm2Test [new MavenAsset(null, 'classifier', 'extension'), new MavenAsset(null, 'env-classifier', 'env-extension')] } + @WithoutJenkins + def 'it logs upload failure message'() { + setup: + def client = Mock(RepositoryManagerV2Client) + client.uploadComponent(_, _, _) >> { + throw new RepositoryManagerException("UPLOAD ERROR", new IOException("BANG!")) + } + def nxrmConfiguration = new Nxrm2Configuration('id', 'internalId', 'displayName', 'foo', 'credId') + run.getEnvironment(_ as TaskListener) >> new EnvVars(['BUILD_ID': '1']) + + def mockComponentUploader = + Spy(ComponentUploaderNxrm2, constructorArgs: [nxrmConfiguration, run, taskListener]) { + it.getRepositoryManagerClient(nxrmConfiguration) >> client + } + def publisher = Mock(NexusPublisher) + def tempFile = File.createTempFile("temp", ".tmp") + + def filePath = new FilePath(tempFile.getParentFile()) + publisher.packages >> [ new MavenPackage( + new MavenCoordinate('some-group', 'some-artifact', '1.0.0-SNAPSHOT', 'jar'), + [new MavenAsset(tempFile.name, 'classifier', 'extension')]) + ] + + when: + mockComponentUploader.uploadComponents(publisher, filePath) + tempFile.delete() + + then: + def thrown = thrown(IOException) + thrown.message.endsWith('failed due to UPLOAD ERROR') + 1 * run.setResult(Result.FAILURE) + } + @WithoutJenkins def 'it copies assets locally'() { setup: diff --git a/src/test/java/org/sonatype/nexus/ci/nxrm/ComponentUploaderNxrm3Test.groovy b/src/test/java/org/sonatype/nexus/ci/nxrm/ComponentUploaderNxrm3Test.groovy index 10cf4e91..7bd3b8a9 100644 --- a/src/test/java/org/sonatype/nexus/ci/nxrm/ComponentUploaderNxrm3Test.groovy +++ b/src/test/java/org/sonatype/nexus/ci/nxrm/ComponentUploaderNxrm3Test.groovy @@ -12,6 +12,8 @@ */ package org.sonatype.nexus.ci.nxrm +import com.sonatype.nexus.api.exception.IqClientException +import com.sonatype.nexus.api.exception.RepositoryManagerException import com.sonatype.nexus.api.repository.v3.Component import com.sonatype.nexus.api.repository.v3.RepositoryManagerV3Client @@ -240,6 +242,41 @@ class ComponentUploaderNxrm3Test 1 * remotePath.read() >> payload } + @WithoutJenkins + def 'it logs upload failure message'() { + setup: + def client = Mock(RepositoryManagerV3Client) + client.upload(_, _, _) >> { + throw new RepositoryManagerException("UPLOAD ERROR", new IOException("BANG!")) + } + def nxrmConfiguration = new Nxrm3Configuration('id', 'internalId', 'displayName', 'foo', 'credId') + run.getEnvironment(_ as TaskListener) >> new EnvVars(['BUILD_ID': '1']) + client.getTag(_ as String) >> Optional.empty() + + ComponentUploaderNxrm3 mockComponentUploader = + Spy([constructorArgs: [nxrmConfiguration, run, taskListener]] as Map, + ComponentUploaderNxrm3) { + it.getRepositoryManagerClient(nxrmConfiguration) >> client + } + def publisher = Mock(NexusPublisher) + def tempFile = File.createTempFile("temp", ".tmp") + + def coordinate = new MavenCoordinate('some-group', 'some-artifact', '1.0', 'jar') + def filePath = new FilePath(tempFile.getParentFile()) + publisher.packages >> [ + new MavenPackage(coordinate, [new MavenAsset(tempFile.name, null, 'jar')]) + ] + + when: + mockComponentUploader.uploadComponents(publisher, filePath, 'foobar') + tempFile.delete() + + then: + def thrown = thrown(IOException) + thrown.message == 'Upload of maven component with GAV [some-group:some-artifact:1.0] failed due to UPLOAD ERROR' + 1 * run.setResult(Result.FAILURE) + } + @WithoutJenkins def 'it requires a nxrm3 server'() { setup: