Skip to content

Commit

Permalink
Mod error message when invalid config (#973)
Browse files Browse the repository at this point in the history
  • Loading branch information
nowsprinting committed Dec 13, 2021
1 parent 2429215 commit ac01fc2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 18 deletions.
15 changes: 12 additions & 3 deletions dist/index.js
Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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),
})
}
Expand Down
15 changes: 12 additions & 3 deletions 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')
Expand All @@ -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)

Expand All @@ -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),
})
}
Expand Down
63 changes: 51 additions & 12 deletions test/config.test.js
Expand Up @@ -28,20 +28,50 @@ 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 = {
payload: { repository: { default_branch: 'master' } },
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/)
)
})

Expand Down Expand Up @@ -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\]/
)
)
})

Expand Down

0 comments on commit ac01fc2

Please sign in to comment.