diff --git a/dist/index.js b/dist/index.js index 95a8d8600..3752f388d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -405,7 +405,6 @@ module.exports.findCommitsWithAssociatedPullRequests = async ({ const core = __nccwpck_require__(42186) const { validateSchema } = __nccwpck_require__(5171) -const { DEFAULT_CONFIG } = __nccwpck_require__(25586) const log = __nccwpck_require__(13817) const { runnerIsActions } = __nccwpck_require__(50918) const Table = __nccwpck_require__(2101) @@ -416,8 +415,16 @@ module.exports.getConfig = async function getConfig({ context, configName }) { try { const repoConfig = await context.config( configName || DEFAULT_CONFIG_NAME, - DEFAULT_CONFIG + null ) + if (repoConfig == null) { + // noinspection ExceptionCaughtLocallyJS + throw new Error( + 'Configuration file .github/' + + (configName || DEFAULT_CONFIG_NAME) + + ' is not found. The configuration file must reside in your default branch.' + ) + } const config = validateSchema(context, repoConfig) @@ -429,7 +436,9 @@ module.exports.getConfig = async function getConfig({ context, configName }) { log({ context, message: - 'Config validation errors, please fix the following issues in release-drafter.yml:\n' + + 'Config validation errors, please fix the following issues in ' + + (configName || DEFAULT_CONFIG_NAME) + + ':\n' + joiValidationErrorsAsTable(error), }) } diff --git a/lib/config.js b/lib/config.js index 8e67e6790..bc48f28a4 100644 --- a/lib/config.js +++ b/lib/config.js @@ -1,6 +1,5 @@ const core = require('@actions/core') const { validateSchema } = require('./schema') -const { DEFAULT_CONFIG } = require('./default-config') const log = require('./log') const { runnerIsActions } = require('./utils') const Table = require('cli-table3') @@ -11,8 +10,16 @@ module.exports.getConfig = async function getConfig({ context, configName }) { try { const repoConfig = await context.config( configName || DEFAULT_CONFIG_NAME, - DEFAULT_CONFIG + null ) + if (repoConfig == null) { + // noinspection ExceptionCaughtLocallyJS + throw new Error( + 'Configuration file .github/' + + (configName || DEFAULT_CONFIG_NAME) + + ' is not found. The configuration file must reside in your default branch.' + ) + } const config = validateSchema(context, repoConfig) @@ -24,7 +31,9 @@ module.exports.getConfig = async function getConfig({ context, configName }) { log({ context, message: - 'Config validation errors, please fix the following issues in release-drafter.yml:\n' + + 'Config validation errors, please fix the following issues in ' + + (configName || DEFAULT_CONFIG_NAME) + + ':\n' + joiValidationErrorsAsTable(error), }) } diff --git a/test/config.test.js b/test/config.test.js index f455fc69a..f357a2eee 100644 --- a/test/config.test.js +++ b/test/config.test.js @@ -28,6 +28,29 @@ describe('getConfig', () => { }) }) + it('config file does not exist', async () => { + const context = { + payload: { repository: { default_branch: 'master' } }, + config: null, + log: { info: jest.fn(), warn: jest.fn() }, + } + const warnSpy = jest.spyOn(context.log, 'warn') + + const config = await getConfig({ + context, + }) + + expect(config).toBeNull() + expect(warnSpy).toHaveBeenLastCalledWith( + expect.objectContaining({ + stack: expect.stringMatching(/context.config is not a function/), + // In the test, this message is set by Probot. Actually the message below: + // 'Configuration file .github/release-drafter.yml is not found. The configuration file must reside in your default branch.' + }), + expect.stringMatching(/Invalid config file/) + ) + }) + describe('`replacers` option', () => { it('validates `replacers` option', async () => { const context = { @@ -35,13 +58,20 @@ describe('getConfig', () => { config: createGetConfigMock({ replacers: 'bogus' }), log: { info: jest.fn(), warn: jest.fn() }, } + const warnSpy = jest.spyOn(context.log, 'warn') + const infoSpy = jest.spyOn(context.log, 'info') + + const config = await getConfig({ + context, + }) - expect( - getConfig({ - context, - }) - ).rejects.toThrow( - 'child "replacers" fails because ["replacers" must be an array]' + expect(config).toBeNull() + expect(warnSpy).toHaveBeenLastCalledWith( + expect.objectContaining({}), + expect.stringMatching(/Invalid config file/) + ) + expect(infoSpy).toHaveBeenLastCalledWith( + expect.stringMatching(/"replacers" must be an array/) ) }) @@ -72,13 +102,22 @@ describe('getConfig', () => { config: createGetConfigMock({ 'sort-direction': 'bogus' }), log: { info: jest.fn(), warn: jest.fn() }, } + const warnSpy = jest.spyOn(context.log, 'warn') + const infoSpy = jest.spyOn(context.log, 'info') - expect( - getConfig({ - context, - }) - ).rejects.toThrow( - '"sort-direction" must be one of [ascending, descending]' + const config = await getConfig({ + context, + }) + + expect(config).toBeNull() + expect(warnSpy).toHaveBeenLastCalledWith( + expect.objectContaining({}), + expect.stringMatching(/Invalid config file/) + ) + expect(infoSpy).toHaveBeenLastCalledWith( + expect.stringMatching( + /"sort-direction" must be one of \[ascending, descending\]/ + ) ) })