Skip to content

Commit

Permalink
fix(verifyConditions): skip google service account key verification w…
Browse files Browse the repository at this point in the history
…hen `publishPub` is false (#56)
  • Loading branch information
zeshuaro committed Oct 11, 2023
1 parent bb9f001 commit 21196e3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
23 changes: 17 additions & 6 deletions src/verifyConditions.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import SemanticReleaseError from '@semantic-release/error';
import { execa } from 'execa';
import { VerifyConditionsContext } from 'semantic-release';
import { PluginConfig } from './types.js';
import { getConfig, getGoogleIdentityToken } from './utils.js';

export const verifyConditions = async (pluginConfig: PluginConfig) => {
const { cli } = getConfig(pluginConfig);
export const verifyConditions = async (
pluginConfig: PluginConfig,
{ logger }: VerifyConditionsContext
) => {
const { cli, publishPub } = getConfig(pluginConfig);
const { GOOGLE_SERVICE_ACCOUNT_KEY } = process.env;

if (!GOOGLE_SERVICE_ACCOUNT_KEY) {
throw new SemanticReleaseError(
'Environment variable not found: GOOGLE_SERVICE_ACCOUNT_KEY'
if (publishPub) {
if (!GOOGLE_SERVICE_ACCOUNT_KEY) {
throw new SemanticReleaseError(
'Environment variable not found: GOOGLE_SERVICE_ACCOUNT_KEY'
);
}

await getGoogleIdentityToken(GOOGLE_SERVICE_ACCOUNT_KEY);
} else {
logger.log(
`Skipping Google service account key verification as publishPub is ${publishPub}`
);
}

await verifyCommand(cli);
await getGoogleIdentityToken(GOOGLE_SERVICE_ACCOUNT_KEY);
};

const verifyCommand = async (command: string) => {
Expand Down
31 changes: 26 additions & 5 deletions tests/verifyConditions.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import SemanticReleaseError from '@semantic-release/error';
import { execa } from 'execa';
import { VerifyConditionsContext } from 'semantic-release';
import { Signale } from 'signale';
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import { mock } from 'vitest-mock-extended';
import { PluginConfig, verifyConditions } from '../src/index.js';
import { getConfig, getGoogleIdentityToken } from '../src/utils.js';

Expand All @@ -13,9 +16,14 @@ describe('verifyConditions', () => {
const serviceAccount = 'serviceAccount';
const idToken = 'idToken';

const config: PluginConfig = { cli };
const config: PluginConfig = { cli, publishPub: true };

const logger = mock<Signale>();
const context = mock<VerifyConditionsContext>();

beforeEach(() => {
context.logger = logger;

vi.mocked(getConfig).mockReturnValue(config);
vi.mocked(getGoogleIdentityToken).mockResolvedValue(idToken);
});
Expand All @@ -28,10 +36,20 @@ describe('verifyConditions', () => {
test('success', async () => {
stubEnv();

await verifyConditions(config);
await verifyConditions(config, context);

expect(execa).toBeCalledWith(cli);
expect(getGoogleIdentityToken).toHaveBeenNthCalledWith(1, serviceAccount);
expectGetGoogleIdentityTokenCalled();
});

test('success with publishPub=false', async () => {
const config: PluginConfig = { cli, publishPub: false };
vi.mocked(getConfig).mockReturnValue(config);

await verifyConditions(config, context);

expect(execa).toBeCalledWith(cli);
expect(getGoogleIdentityToken).toBeCalledTimes(0);
});

test('error due to missing environment variable', async () => {
Expand All @@ -50,14 +68,17 @@ describe('verifyConditions', () => {
await expectSemanticReleaseError();

expect(execa).toBeCalledWith(cli);
expect(getGoogleIdentityToken).toBeCalledTimes(0);
expectGetGoogleIdentityTokenCalled();
});

const stubEnv = () =>
vi.stubEnv('GOOGLE_SERVICE_ACCOUNT_KEY', serviceAccount);

const expectGetGoogleIdentityTokenCalled = () =>
expect(getGoogleIdentityToken).toHaveBeenNthCalledWith(1, serviceAccount);

const expectSemanticReleaseError = async () => {
await expect(() => verifyConditions(config)).rejects.toThrowError(
await expect(() => verifyConditions(config, context)).rejects.toThrowError(
SemanticReleaseError
);
};
Expand Down

0 comments on commit 21196e3

Please sign in to comment.