From 16f23703890d2af73bc00027a65de86cbdff898a Mon Sep 17 00:00:00 2001 From: Till Kolter Date: Thu, 21 Oct 2021 17:20:23 +0200 Subject: [PATCH] fix: Make GEM_HOST_API_KEY optional for gemPublish: false --- README.md | 2 +- src/__tests__/verifyConditions.test.js | 22 ++++++++++++++++++---- src/common.js | 1 + src/publish.js | 7 ++++++- src/verifyConditions.js | 12 +++++++++--- 5 files changed, 35 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 0275848..c91a635 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Add the plugin to the [**semantic-release** configuration file](https://github.c ### Gem server authentication -The gem server authentication configuration is **required**. +The gem server authentication configuration is **required** unless `gemPublish` is explicitly set to `false`. The API key must be set using the `GEM_HOST_API_KEY` environment variable. To retrieve the key, you can: 1. Login to [RubyGems.org](https://rubygems.org) and click on ['Edit Profile'](https://rubygems.org/profile/edit). You'll find the key in the 'API Access' section of the page. diff --git a/src/__tests__/verifyConditions.test.js b/src/__tests__/verifyConditions.test.js index 9e7ed00..af007af 100644 --- a/src/__tests__/verifyConditions.test.js +++ b/src/__tests__/verifyConditions.test.js @@ -96,9 +96,23 @@ it('creates the credentials file', async () => { }); describe('when the API key env var is not defined', () => { - it('throws an error', async () => { - await expect( - verifyConditions({}, { cwd: validCwd, env: {} }, { credentialsFile }), - ).rejects.toThrow(/^No gem API key specified.$/); + describe('with config defaults', () => { + it('throws an error', async () => { + await expect( + verifyConditions({}, { cwd: validCwd, env: {} }, { credentialsFile }), + ).rejects.toThrow(/^No gem API key specified.$/); + }); + }); + + describe('with gemPublish false', () => { + it('does not throw an error', async () => { + await expect( + verifyConditions({ gemPublish: false }, { cwd: validCwd, env: {} }, { credentialsFile }), + ).resolves.toEqual({ + gemName: 'a-test-gem', + gemspec: 'test-gem.gemspec', + versionFile: 'lib/test-gem/version.rb', + }); + }); }); }); diff --git a/src/common.js b/src/common.js index 8b3219b..782b41a 100644 --- a/src/common.js +++ b/src/common.js @@ -1,2 +1,3 @@ // taken from https://github.com/svenfuchs/gem-release/blob/0f5f2382ef960d40169400a8aa25085cd37d26b0/lib/gem/release/files/version.rb#L7 module.exports.VERSION_REGEX = /(VERSION\s*=\s*['"])(?:(?!"|').)*(['"])/; +module.exports.CONFIG_DEFAULTS = { gemPublish: true, gemFileDir: false }; diff --git a/src/publish.js b/src/publish.js index 2caebf8..29e373c 100644 --- a/src/publish.js +++ b/src/publish.js @@ -1,11 +1,16 @@ const { unlink } = require('fs').promises; const execa = require('execa'); +const { CONFIG_DEFAULTS } = require('./common'); module.exports = async function publish( - { gemHost, gemPublish = true, gemFileDir = false }, + pluginConfig, { cwd, env, logger, nextRelease: { version }, stdout, stderr }, { gemFile, gemName, credentialsFile }, ) { + const { gemHost, gemPublish, gemFileDir } = { + ...CONFIG_DEFAULTS, + ...pluginConfig, + }; if (gemPublish !== false) { logger.log(`Publishing version ${version} to gem server`); const args = ['push', gemFile, '--config-file', credentialsFile]; diff --git a/src/verifyConditions.js b/src/verifyConditions.js index d001e01..4f75ac6 100644 --- a/src/verifyConditions.js +++ b/src/verifyConditions.js @@ -5,7 +5,7 @@ const path = require('path'); const { writeFile, readFile } = require('fs').promises; const SemanticReleaseError = require('@semantic-release/error'); const glob = util.promisify(require('glob')); -const { VERSION_REGEX } = require('./common'); +const { VERSION_REGEX, CONFIG_DEFAULTS } = require('./common'); const loadGemspec = async cwd => { const gemspecs = await glob('*.gemspec', { cwd }); @@ -113,6 +113,10 @@ You can retrieve an API key either from your \`~/.gem/credentials\` file or in y */ module.exports = async function verify(pluginConfig, { env, cwd }, { credentialsFile }) { // - Verify ruby installed? + const { gemPublish } = { + ...CONFIG_DEFAULTS, + ...pluginConfig, + }; // - Locate gemspec and determine name const { name, gemspec } = await loadGemspec(cwd); @@ -120,8 +124,10 @@ module.exports = async function verify(pluginConfig, { env, cwd }, { credentials // - Locate version file const versionFile = await verifyVersionFile(cwd); - // - Verify env var - await verifyApiKey({ env, cwd, credentialsFile }); + if (gemPublish) { + // - Verify env var + await verifyApiKey({ env, cwd, credentialsFile }); + } return { gemName: name, gemspec, versionFile }; };