From 2187204674118a5a51de8077c0b433b8980ec422 Mon Sep 17 00:00:00 2001 From: Markus Hintersteiner Date: Wed, 22 May 2024 12:20:21 +0200 Subject: [PATCH] Fix ignore artifacts which contain no POM/BOM --- src/targets/__tests__/maven.test.ts | 28 ++++++++++++++++++++++++++++ src/targets/maven.ts | 26 ++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/targets/__tests__/maven.test.ts b/src/targets/__tests__/maven.test.ts index 57617057..540e9dd7 100644 --- a/src/targets/__tests__/maven.test.ts +++ b/src/targets/__tests__/maven.test.ts @@ -371,6 +371,7 @@ describe('upload', () => { .fn() .mockResolvedValueOnce('artifact/download/path'); mvnTarget.isBomFile = jest.fn().mockResolvedValueOnce(false); + mvnTarget.getPomFileInDist = jest.fn().mockResolvedValueOnce('pom-default.xml'); await mvnTarget.upload('r3v1s10n'); @@ -420,6 +421,7 @@ describe('upload', () => { .fn() .mockResolvedValueOnce('artifact/download/path'); mvnTarget.isBomFile = jest.fn().mockResolvedValueOnce('path/to/bomfile'); + mvnTarget.getPomFileInDist = jest.fn().mockResolvedValueOnce(undefined); await mvnTarget.upload('r3v1s10n'); @@ -446,6 +448,32 @@ describe('upload', () => { expect(cmdArgs[6]).toBe('--settings'); expect(cmdArgs[7]).toBe(DEFAULT_OPTION_VALUE); }); + + test('should skip upload for artifacts without any POM/BOM', async () => { + // simple mock to always use the same temporary directory, + // instead of creating a new one + (withTempDir as jest.MockedFunction).mockImplementation( + async cb => { + return await cb(tmpDirName); + } + ); + + const mvnTarget = createMavenTarget(); + + mvnTarget.getArtifactsForRevision = jest + .fn() + .mockResolvedValueOnce([{ filename: 'mockArtifact.zip' }]); + mvnTarget.artifactProvider.downloadArtifact = jest + .fn() + .mockResolvedValueOnce('artifact/download/path'); + + mvnTarget.isBomFile = jest.fn().mockResolvedValueOnce(false); + mvnTarget.getPomFileInDist = jest.fn().mockResolvedValueOnce(undefined); + + await mvnTarget.upload('r3v1s10n'); + + expect(retrySpawnProcess).toHaveBeenCalledTimes(0); + }); }); describe('closeAndReleaseRepository', () => { diff --git a/src/targets/maven.ts b/src/targets/maven.ts index 1fe5df3e..0c565343 100644 --- a/src/targets/maven.ts +++ b/src/targets/maven.ts @@ -288,11 +288,16 @@ export class MavenTarget extends BaseTarget { */ private async uploadDistribution(distDir: string): Promise { const bomFile = await this.getBomFileInDist(distDir); + const pomFile = await this.getPomFileInDist(distDir); + if (bomFile) { this.logger.debug('Found BOM: ', bomFile); await this.uploadBomDistribution(bomFile); - } else { + } else if (pomFile) { + this.logger.debug('Found POM: ', pomFile); await this.uploadPomDistribution(distDir); + } else { + this.logger.warn(`No BOM/POM file found in: ${distDir}, skipping directory`); } } @@ -341,7 +346,7 @@ export class MavenTarget extends BaseTarget { } catch (error) { this.logger.warn( `Could not determine if path corresponds to a BOM file: ${pomFilepath}\n` + - 'Error:\n', + 'Error:\n', error ); return false; @@ -475,6 +480,23 @@ export class MavenTarget extends BaseTarget { }; } + /** + * Returns a file path to pom-default.xml if the file exists + * within the distribution directory. + */ + public async getPomFileInDist(distDir: string): Promise { + const pomFilepath = join(distDir, 'pom-default.xml'); + try { + const stat = await fsPromises.stat(pomFilepath); + if (stat.isFile()) { + return pomFilepath; + } + } catch (e) { + // ignored + } + return undefined; + } + private async uploadPomDistribution(distDir: string): Promise { if (this.mavenConfig.kmp !== false) { await this.uploadKmpPomDistribution(distDir);