Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow command handlers to match regexes #846

Merged
merged 3 commits into from Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
107 changes: 107 additions & 0 deletions src/App.spec.ts
Expand Up @@ -1110,6 +1110,9 @@ describe('App', () => {
app.command('/echo', async ({}) => {
/* noop */
});
app.command(/\/e.*/, async ({}) => {
/* noop */
});

// invalid view constraints
const invalidViewConstraints1 = ({
Expand Down Expand Up @@ -1256,6 +1259,110 @@ describe('App', () => {
});
});

describe('command()', () => {
it('should respond to exact name matches', async () => {
// Arrange
const overrides = buildOverrides([withNoopWebClient()]);
const App = await importApp(overrides); // eslint-disable-line @typescript-eslint/naming-convention, no-underscore-dangle, id-blacklist, id-match
let matchCount = 0;

// Act
const app = new App({ receiver: fakeReceiver, authorize: sinon.fake.resolves(dummyAuthorizationResult) });
app.command('/hello', async () => {
++matchCount;
});
await fakeReceiver.sendEvent({
body: {
type: 'slash_command',
command: '/hello',
},
ack: noop,
});

// Assert
assert.equal(matchCount, 1);
});

it('should respond to pattern matches', async () => {
// Arrange
const overrides = buildOverrides([withNoopWebClient()]);
const App = await importApp(overrides); // eslint-disable-line @typescript-eslint/naming-convention, no-underscore-dangle, id-blacklist, id-match
let matchCount = 0;

// Act
const app = new App({ receiver: fakeReceiver, authorize: sinon.fake.resolves(dummyAuthorizationResult) });
app.command(/h.*/, async () => {
++matchCount;
});
await fakeReceiver.sendEvent({
body: {
type: 'slash_command',
command: '/hello',
},
ack: noop,
});

// Assert
assert.equal(matchCount, 1);
});

it('should run all matching listeners', async () => {
// Arrange
const overrides = buildOverrides([withNoopWebClient()]);
const App = await importApp(overrides); // eslint-disable-line @typescript-eslint/naming-convention, no-underscore-dangle, id-blacklist, id-match
let firstCount = 0;
let secondCount = 0;

// Act
const app = new App({ receiver: fakeReceiver, authorize: sinon.fake.resolves(dummyAuthorizationResult) });
app.command(/h.*/, async () => {
++firstCount;
});
app.command(/he.*/, async () => {
++secondCount;
});
await fakeReceiver.sendEvent({
body: {
type: 'slash_command',
command: '/hello',
},
ack: noop,
});

// Assert
assert.equal(firstCount, 1);
assert.equal(secondCount, 1);
});

it('should not stop at an unsuccessful match', async () => {
// Arrange
const overrides = buildOverrides([withNoopWebClient()]);
const App = await importApp(overrides); // eslint-disable-line @typescript-eslint/naming-convention, no-underscore-dangle, id-blacklist, id-match
let firstCount = 0;
let secondCount = 0;

// Act
const app = new App({ receiver: fakeReceiver, authorize: sinon.fake.resolves(dummyAuthorizationResult) });
app.command(/x.*/, async () => {
++firstCount;
});
app.command(/h.*/, async () => {
++secondCount;
});
await fakeReceiver.sendEvent({
body: {
type: 'slash_command',
command: '/hello',
},
ack: noop,
});

// Assert
assert.equal(firstCount, 0);
assert.equal(secondCount, 1);
});
});

describe('respond()', () => {
it('should respond to events with a response_url', async () => {
// Arrange
Expand Down
3 changes: 1 addition & 2 deletions src/App.ts
Expand Up @@ -528,8 +528,7 @@ export default class App {
this.listeners.push([onlyActions, matchConstraints(constraints), ...listeners] as Middleware<AnyMiddlewareArgs>[]);
}

// TODO: should command names also be regex?
public command(commandName: string, ...listeners: Middleware<SlackCommandMiddlewareArgs>[]): void {
public command(commandName: string | RegExp, ...listeners: Middleware<SlackCommandMiddlewareArgs>[]): void {
this.listeners.push([onlyCommands, matchCommandName(commandName), ...listeners] as Middleware<AnyMiddlewareArgs>[]);
}

Expand Down
8 changes: 7 additions & 1 deletion src/middleware/builtin.spec.ts
Expand Up @@ -510,12 +510,18 @@ describe('matchCommandName', () => {
};
}

it('should detect valid requests', async () => {
it('should detect requests that match exactly', async () => {
const fakeNext = sinon.fake();
await matchCommandName('/hi')(buildArgs(fakeNext));
assert.isTrue(fakeNext.called);
});

it('should detect requests that match a pattern', async () => {
const fakeNext = sinon.fake();
await matchCommandName(/h/)(buildArgs(fakeNext));
assert.isTrue(fakeNext.called);
});

it('should skip other requests', async () => {
const fakeNext = sinon.fake();
await matchCommandName('/hello')(buildArgs(fakeNext));
Expand Down
15 changes: 11 additions & 4 deletions src/middleware/builtin.ts
Expand Up @@ -241,12 +241,12 @@ export function matchMessage(
}

/**
* Middleware that filters out any command that doesn't match name
* Middleware that filters out any command that doesn't match the pattern
*/
export function matchCommandName(name: string): Middleware<SlackCommandMiddlewareArgs> {
export function matchCommandName(pattern: string | RegExp): Middleware<SlackCommandMiddlewareArgs> {
return async ({ command, next }) => {
// Filter out any commands that are not the correct command name
if (name !== command.command) {
// Filter out any commands that do not match the correct command name or pattern
if (!matchesPattern(pattern, command.command)) {
return;
}

Expand All @@ -255,6 +255,13 @@ export function matchCommandName(name: string): Middleware<SlackCommandMiddlewar
};
}

function matchesPattern(pattern: string | RegExp, candidate: string): boolean {
if (typeof pattern === 'string') {
return pattern === candidate;
}
return pattern.test(candidate);
}

/*
* Middleware that filters out events that don't match pattern
*/
Expand Down