Skip to content
This repository has been archived by the owner on May 28, 2024. It is now read-only.

Commit

Permalink
Better logging for upload failures - CLM-25075 (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduard-tita authored Jul 19, 2023
1 parent 3e10c22 commit 1223ad4
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 29 deletions.
12 changes: 10 additions & 2 deletions src/main/java/org/sonatype/nexus/ci/nxrm/ComponentUploader.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ abstract class ComponentUploader

protected abstract void upload(final Map<MavenCoordinate, List<RemoteMavenAsset>> remoteMavenComponents,
final String nxrmRepositoryId,
final String tagName = null)
final String tagName = null) throws IOException

@SuppressWarnings(['UnusedMethodParameter', 'EmptyMethodInAbstractClass'])
void maybeCreateTag(final String tagName) {
Expand Down Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ComponentUploaderNxrm2
@Override
void upload(final Map<MavenCoordinate, List<RemoteMavenAsset>> remoteMavenComponents,
final String nxrmRepositoryId,
final String tag = null)
final String tag = null) throws IOException
{
def nxrmClient = getRepositoryManagerClient(nxrmConfiguration)

Expand All @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ComponentUploaderNxrm3
@Override
void upload(final Map<MavenCoordinate, List<RemoteMavenAsset>> remoteMavenComponents,
final String nxrmRepositoryId,
final String tagName = null)
final String tagName = null) throws IOException
{
def nxrmClient = getRepositoryManagerClient(nxrmConfiguration)

Expand Down Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<String, Object>,
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:
Expand Down

0 comments on commit 1223ad4

Please sign in to comment.