Skip to content

Commit

Permalink
fix: mention text parsing (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
radulucut authored Nov 26, 2024
1 parent 2597134 commit 0d1ad26
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 27 deletions.
17 changes: 12 additions & 5 deletions packages/bot-utils/src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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}`;
Expand Down
39 changes: 39 additions & 0 deletions packages/bot-utils/tests/args-to-flags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,29 @@ describe('Utils', () => {
expect(result).toEqual(flags);
});

it('host target with slack link', () => {
const args
= 'http <https://google.com|google.com> 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';
Expand Down Expand Up @@ -139,6 +162,22 @@ describe('Utils', () => {
expect(result).toEqual(flags);
});

it('convert ping args to flags with slack link', () => {
const args
= 'ping <http://google.com|google.com> 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';
Expand Down
15 changes: 5 additions & 10 deletions packages/slack/src/mention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
16 changes: 4 additions & 12 deletions packages/slack/src/tests/mention.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ describe('Mention', () => {
= '<@U052V9JLQ5C> http <http://yahoo.com|yahoo.com> --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 <http://yahoo.com|yahoo.com> --from uk --limit 5');
});

it('valid with url link and query', () => {
const text
= '<@U052V9JLQ5C> http <http://yahoo.com?abc=xyz1|yahoo.com> --from uk';
const botUserId = 'U052V9JLQ5C';
const cmd = parseCommandfromMention(text, botUserId);
expect(cmd).to.equal('http yahoo.com --from uk');
expect(cmd).to.equal('http <http://yahoo.com?abc=xyz1|yahoo.com> --from uk');
});

it('valid with just complex url link', () => {
const text
= '<@U052V9JLQ5C> http <https://www.example.com:8080/my/path?x=abc&yz=defg> --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 <https://www.example.com:8080/my/path?x=abc&yz=defg> --from france --limit 5');
});

it('valid with host', () => {
Expand All @@ -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 <http://yahoo.com|yahoo.com> --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 <http://yahoo.com|yahoo.com> --from france --limit 5';
const botUserId = 'U052V9JLQ5C';
const cmd = parseCommandfromMention(text, botUserId);
expect(cmd).to.equal('');
expect(cmd).to.equal('ping <http://yahoo.com|yahoo.com> --from france --limit 5');
});
});
});

0 comments on commit 0d1ad26

Please sign in to comment.