Skip to content

Commit

Permalink
fix(maven): move importGPGKey function call from constructor to `pu…
Browse files Browse the repository at this point in the history
…blish` (#553)

* fix: add missing await to `importGPGKey` function call

## Context
We have recently set up a multiple target setup in one of our repos: https://github.com/getsentry/sentry-kotlin-multiplatform/blob/main/.craft.yml

and noticed an error during publish that indicates an error within the `importGPGKey` function: https://github.com/getsentry/publish/actions/runs/10250810494/job/28357374501#step:11:166

## Fix

The `importGPGKey` function returns a promise but is not being awaited

Side note: shouldn't this be caught by some static code analyzer or linter?

* move  to publish

* fix test

* revert some formatting

* add missing newline
  • Loading branch information
buenaflor committed Aug 8, 2024
1 parent 62d65fe commit 78790e9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/targets/__tests__/maven.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,16 @@ describe('Maven target configuration', () => {
expect(typeof mvnTarget.config.kmp.appleDistDirRegex).toBe('string');
});

test('import GPG private key if one is present in the environment', () => {
test('import GPG private key if one is present in the environment', async () => {
setTargetSecretsInEnv();
process.env.GPG_PRIVATE_KEY = DEFAULT_OPTION_VALUE;
createMavenTarget(getFullTargetConfig());
const callOrder: string[] = [];
const mvnTarget = createMavenTarget(getFullTargetConfig());
mvnTarget.upload = jest.fn(async () => void callOrder.push('upload'));
mvnTarget.closeAndReleaseRepository = jest.fn(
async () => void callOrder.push('closeAndReleaseRepository')
);
await mvnTarget.publish('1.0.0', 'r3v1s10n');
expect(importGPGKey).toHaveBeenCalledWith(DEFAULT_OPTION_VALUE);
});
});
Expand Down
7 changes: 3 additions & 4 deletions src/targets/maven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ export class MavenTarget extends BaseTarget {
) {
super(config, artifactProvider);
this.mavenConfig = this.getMavenConfig();

if (process.env.GPG_PRIVATE_KEY) {
importGPGKey(process.env.GPG_PRIVATE_KEY);
}
}

/**
Expand Down Expand Up @@ -236,6 +232,9 @@ export class MavenTarget extends BaseTarget {
* @param revision Git commit SHA to be published.
*/
public async publish(_version: string, revison: string): Promise<void> {
if (process.env.GPG_PRIVATE_KEY) {
await importGPGKey(process.env.GPG_PRIVATE_KEY);
}
await this.upload(revison);
await this.closeAndReleaseRepository();
}
Expand Down

0 comments on commit 78790e9

Please sign in to comment.