Skip to content

Commit

Permalink
fix jest exit code issue
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-evans committed Jan 25, 2024
1 parent 780bc06 commit d12bac0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
16 changes: 11 additions & 5 deletions __test__/command-helper.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe('command-helper tests', () => {
dispatch_type: 'repository'
}
]
expect(configIsValid(config)).toBeTruthy()
expect(configIsValid(config)).toEqual(null)
})

test('invalid permission level in config', async () => {
Expand All @@ -169,7 +169,9 @@ describe('command-helper tests', () => {
dispatch_type: 'repository'
}
]
expect(configIsValid(config)).toBeFalsy()
expect(configIsValid(config)).toEqual(
`'test-case-invalid-permission' is not a valid 'permission'.`
)
})

test('invalid issue type in config', async () => {
Expand All @@ -185,23 +187,27 @@ describe('command-helper tests', () => {
dispatch_type: 'repository'
}
]
expect(configIsValid(config)).toBeFalsy()
expect(configIsValid(config)).toEqual(
`'test-case-invalid-issue-type' is not a valid 'issue-type'.`
)
})

test('invalid dispatch type in config', async () => {
const config: Command[] = [
{
command: 'test',
permission: 'write',
issue_type: 'test-case-invalid-issue-type',
issue_type: 'both',
allow_edits: false,
repository: 'peter-evans/slash-command-dispatch',
event_type_suffix: '-command',
static_args: [],
dispatch_type: 'test-case-invalid-dispatch-type'
}
]
expect(configIsValid(config)).toBeFalsy()
expect(configIsValid(config)).toEqual(
`'test-case-invalid-dispatch-type' is not a valid 'dispatch-type'.`
)
})

test('actor does not have permission', async () => {
Expand Down
17 changes: 8 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,16 @@ exports.getCommandsConfigFromJson = getCommandsConfigFromJson;
function configIsValid(config) {
for (const command of config) {
if (!['none', 'read', 'triage', 'write', 'maintain', 'admin'].includes(command.permission)) {
core.setFailed(`'${command.permission}' is not a valid 'permission'.`);
return false;
return `'${command.permission}' is not a valid 'permission'.`;
}
if (!['issue', 'pull-request', 'both'].includes(command.issue_type)) {
core.setFailed(`'${command.issue_type}' is not a valid 'issue-type'.`);
return false;
return `'${command.issue_type}' is not a valid 'issue-type'.`;
}
if (!['repository', 'workflow'].includes(command.dispatch_type)) {
core.setFailed(`'${command.dispatch_type}' is not a valid 'dispatch-type'.`);
return false;
return `'${command.dispatch_type}' is not a valid 'dispatch-type'.`;
}
}
return true;
return null;
}
exports.configIsValid = configIsValid;
function actorHasPermission(actorPermission, commandPermission) {
Expand Down Expand Up @@ -454,8 +451,10 @@ function run() {
const config = (0, command_helper_1.getCommandsConfig)(inputs);
core.debug(`Commands config: ${(0, util_1.inspect)(config)}`);
// Check the config is valid
if (!(0, command_helper_1.configIsValid)(config))
return;
const configError = (0, command_helper_1.configIsValid)(config);
if (configError) {
throw new Error(configError);
}
// Get the comment body and id
const commentBody = github.context.payload.comment.body;
const commentId = github.context.payload.comment.id;
Expand Down
15 changes: 5 additions & 10 deletions src/command-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,28 +155,23 @@ export function getCommandsConfigFromJson(json: string): Command[] {
return config
}

export function configIsValid(config: Command[]): boolean {
export function configIsValid(config: Command[]): string | null {
for (const command of config) {
if (
!['none', 'read', 'triage', 'write', 'maintain', 'admin'].includes(
command.permission
)
) {
core.setFailed(`'${command.permission}' is not a valid 'permission'.`)
return false
return `'${command.permission}' is not a valid 'permission'.`
}
if (!['issue', 'pull-request', 'both'].includes(command.issue_type)) {
core.setFailed(`'${command.issue_type}' is not a valid 'issue-type'.`)
return false
return `'${command.issue_type}' is not a valid 'issue-type'.`
}
if (!['repository', 'workflow'].includes(command.dispatch_type)) {
core.setFailed(
`'${command.dispatch_type}' is not a valid 'dispatch-type'.`
)
return false
return `'${command.dispatch_type}' is not a valid 'dispatch-type'.`
}
}
return true
return null
}

export function actorHasPermission(
Expand Down
5 changes: 4 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ async function run(): Promise<void> {
core.debug(`Commands config: ${inspect(config)}`)

// Check the config is valid
if (!configIsValid(config)) return
const configError = configIsValid(config)
if (configError) {
throw new Error(configError)
}

// Get the comment body and id
const commentBody: string = github.context.payload.comment.body
Expand Down

0 comments on commit d12bac0

Please sign in to comment.