Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(maven): ignore artifacts which contain no POM/BOM #538

Merged
merged 1 commit into from
May 22, 2024
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
28 changes: 28 additions & 0 deletions src/targets/__tests__/maven.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

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

Expand All @@ -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<typeof withTempDir>).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', () => {
Expand Down
26 changes: 24 additions & 2 deletions src/targets/maven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,16 @@ export class MavenTarget extends BaseTarget {
*/
private async uploadDistribution(distDir: string): Promise<void> {
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`);
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<string | undefined> {
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<void> {
if (this.mavenConfig.kmp !== false) {
await this.uploadKmpPomDistribution(distDir);
Expand Down
Loading