diff --git a/packages/bot-utils/src/flags.ts b/packages/bot-utils/src/flags.ts index 181fbfb..145ed62 100644 --- a/packages/bot-utils/src/flags.ts +++ b/packages/bot-utils/src/flags.ts @@ -289,14 +289,11 @@ export const argsToFlags = (argv: string | string[]): Flags => { checkFlags(cmd, parsed); } - let urlData: UrlData | undefined; + const urlData = parseUrlData(target); let httpHeaders: HttpHeaders | undefined; + target = urlData.target; if (cmd === 'http' && target) { - urlData = parseUrlData(target); - - target = urlData.target; - if (!host) { host = urlData.host; } @@ -361,6 +358,16 @@ function parseUrlData (input: string): UrlData { let u = input; + if (u.startsWith('<') && u.endsWith('>')) { + const linkEnd = u.indexOf('|'); + + if (linkEnd !== -1) { + u = u.slice(1, linkEnd); + } else { + u = u.slice(1, -1); + } + } + // add url scheme if missing if (!u.startsWith('http://') && !u.startsWith('https://')) { u = `http://${u}`; diff --git a/packages/bot-utils/tests/args-to-flags.test.ts b/packages/bot-utils/tests/args-to-flags.test.ts index 693c074..5bb7a35 100644 --- a/packages/bot-utils/tests/args-to-flags.test.ts +++ b/packages/bot-utils/tests/args-to-flags.test.ts @@ -48,6 +48,29 @@ describe('Utils', () => { expect(result).toEqual(flags); }); + it('host target with slack link', () => { + const args + = 'http from New York --limit 2 --path / --query ?a=abc --host google.fr --method get --port 80 --protocol http2 --latency'; + const result = argsToFlags(args); + + const flags: Flags = { + cmd: 'http', + from: 'New York', + host: 'google.fr', + limit: 2, + method: 'GET', + path: '/', + port: 80, + protocol: 'HTTP2', + query: '?a=abc', + target: 'google.com', + latency: true, + headers: {}, + }; + + expect(result).toEqual(flags); + }); + it('url target, resolver and spaces', () => { const args = 'http https://www.example.com:1234/my/path/?abc=123&xyz=test @8.8.8.8 --from usa --limit 2 --method get'; @@ -139,6 +162,22 @@ describe('Utils', () => { expect(result).toEqual(flags); }); + it('convert ping args to flags with slack link', () => { + const args + = 'ping from New York --limit 2 --packets 3 --latency'; + const result = argsToFlags(args); + const flags: Flags = { + cmd: 'ping', + from: 'New York', + limit: 2, + packets: 3, + target: 'google.com', + latency: true, + }; + + expect(result).toEqual(flags); + }); + it('convert mtr args to flags with share', () => { const args = 'mtr aws.com from Canada --packets 2 --protocol icmp -L 5 --share'; diff --git a/packages/slack/src/mention.ts b/packages/slack/src/mention.ts index 8fbc2bb..2ced88a 100644 --- a/packages/slack/src/mention.ts +++ b/packages/slack/src/mention.ts @@ -8,19 +8,14 @@ export function parseCommandfromMention ( text: string, botUserId: string, ): string { - const trimmedText = text.trim(); - const expectedMention = `<@${botUserId}>`; + const botMention = '<@' + botUserId + '>'; + const mentionIndex = text.indexOf(botMention); - if (trimmedText.startsWith(expectedMention)) { - const urlRegex = /<([^>|]+)(?:\|([^>]+))?>/g; - - return trimmedText - .slice(expectedMention.length) - .replace(urlRegex, (_, link, urlText) => urlText || link) - .trim(); + if (mentionIndex === -1) { + return text; } - return ''; + return text.slice(mentionIndex + botMention.length).trim(); } export async function handleMention ( diff --git a/packages/slack/src/tests/mention.test.ts b/packages/slack/src/tests/mention.test.ts index 4544571..7aec721 100644 --- a/packages/slack/src/tests/mention.test.ts +++ b/packages/slack/src/tests/mention.test.ts @@ -9,7 +9,7 @@ describe('Mention', () => { = '<@U052V9JLQ5C> http --from uk --limit 5'; const botUserId = 'U052V9JLQ5C'; const cmd = parseCommandfromMention(text, botUserId); - expect(cmd).to.equal('http yahoo.com --from uk --limit 5'); + expect(cmd).to.equal('http --from uk --limit 5'); }); it('valid with url link and query', () => { @@ -17,7 +17,7 @@ describe('Mention', () => { = '<@U052V9JLQ5C> http --from uk'; const botUserId = 'U052V9JLQ5C'; const cmd = parseCommandfromMention(text, botUserId); - expect(cmd).to.equal('http yahoo.com --from uk'); + expect(cmd).to.equal('http --from uk'); }); it('valid with just complex url link', () => { @@ -25,7 +25,7 @@ describe('Mention', () => { = '<@U052V9JLQ5C> http --from france --limit 5'; const botUserId = 'U052V9JLQ5C'; const cmd = parseCommandfromMention(text, botUserId); - expect(cmd).to.equal('http https://www.example.com:8080/my/path?x=abc&yz=defg --from france --limit 5'); + expect(cmd).to.equal('http --from france --limit 5'); }); it('valid with host', () => { @@ -42,20 +42,12 @@ describe('Mention', () => { expect(cmd).to.equal('ping 1.2.3.4 --from france --limit 5'); }); - it('wrong user id', () => { - const text - = '<@U061X8OMS7D> ping --from france --limit 5'; - const botUserId = 'U052V9JLQ5C'; - const cmd = parseCommandfromMention(text, botUserId); - expect(cmd).to.equal(''); - }); - it('text before mention', () => { const text = 'some other text <@U052V9JLQ5C> ping --from france --limit 5'; const botUserId = 'U052V9JLQ5C'; const cmd = parseCommandfromMention(text, botUserId); - expect(cmd).to.equal(''); + expect(cmd).to.equal('ping --from france --limit 5'); }); }); });