From 132bdc41ec7d2eac9f1d640e63b0c69c13e60c7f Mon Sep 17 00:00:00 2001 From: dericksonmark Date: Tue, 21 Sep 2021 23:33:01 -0400 Subject: [PATCH 1/2] Initial support for search arguments --- src/commands/SearchCommand.ts | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/commands/SearchCommand.ts b/src/commands/SearchCommand.ts index 1e841d95..31b6413d 100644 --- a/src/commands/SearchCommand.ts +++ b/src/commands/SearchCommand.ts @@ -1,4 +1,4 @@ -import { Message, MessageEmbed, Util } from 'discord.js'; +import { Message, MessageEmbed, TextChannel, Util } from 'discord.js'; import PrefixCommand from './PrefixCommand'; import BotConfig from '../BotConfig'; import MojiraBot from '../MojiraBot'; @@ -11,11 +11,27 @@ export default class SearchCommand extends PrefixCommand { return false; } - const plainArgs = args.replace( /"|<|>/g, '' ); + const plainArgs = args.replace( /<|>/g, '' ); + const modifierRegex = new RegExp( /_[a-z]+\s(([a-zA-Z0-9]+)|(["'][^"']+["']))|:query/, 'g' ); + const modifiers = plainArgs.match( modifierRegex ); + const textArgs = plainArgs.replace( modifierRegex, '' ).trim(); try { const embed = new MessageEmbed(); - const searchFilter = `text ~ "${ plainArgs }" AND project in (${ BotConfig.projects.join( ', ' ) })`; + let searchFilter = `project in (${ BotConfig.projects.join( ', ' ) })`; + + if ( modifiers ) { + for ( const modifier of modifiers ) { + if ( modifier == ':query' ) { + searchFilter = textArgs; + break; + } + if ([ 'project', 'creator', 'reporter', 'assignee', 'version', 'resolution', 'status', 'fixversion', 'confirmation', 'gamemode', 'id', 'labels', 'key', 'priority', 'mp' ].includes( modifier.split( /_|\s/g )[1] ) ) searchFilter += ` AND ${ modifier.split( /_|\s/g )[1].toLowerCase().replace( 'confirmation', '"Confirmation Status"' ).replace( 'gamemode', '"Game Mode"' ).replace( 'mp', '"Mojang Priority"' ) } = ${ modifier.split( /\s/g ).slice( 1 ).join( ' ' ) }`; + if ([ 'comment', 'summary', 'description', 'environment' ].includes( modifier.split( /_|\s/g )[1] ) ) searchFilter += ` AND ${ modifier.split( /_|\s/g )[1].toLowerCase() } ~ ${ modifier.split( /\s/g ).slice( 1 ).join( ' ' ) }` + } + } + if ( textArgs && !modifiers.includes( ':query' ) ) searchFilter += ` AND text ~ "${ textArgs }"` + const searchResults = await MojiraBot.jira.issueSearch.searchForIssuesUsingJqlGet( { jql: searchFilter, maxResults: BotConfig.maxSearchResults, @@ -23,7 +39,7 @@ export default class SearchCommand extends PrefixCommand { } ); if ( !searchResults.issues ) { - embed.setTitle( `No results found for "${ Util.escapeMarkdown( plainArgs ) }"` ); + embed.setTitle( `No results found for "${ Util.escapeMarkdown( textArgs ) }"` ); await message.channel.send( embed ); return false; } @@ -41,7 +57,7 @@ export default class SearchCommand extends PrefixCommand { await message.channel.send( embed ); } catch { const embed = new MessageEmbed(); - embed.setTitle( `No results found for "${ Util.escapeMarkdown( plainArgs ) }"` ); + embed.setTitle( `No results found for "${ Util.escapeMarkdown( textArgs ) }"` ); await message.channel.send( embed ); return false; } @@ -52,4 +68,4 @@ export default class SearchCommand extends PrefixCommand { public asString( args: string ): string { return `!jira search ${ args }`; } -} +} \ No newline at end of file From 1bec8ac62bf664c000b2e7e40cb439b052b0af32 Mon Sep 17 00:00:00 2001 From: dericksonmark Date: Tue, 21 Sep 2021 23:51:50 -0400 Subject: [PATCH 2/2] Fix errors --- src/commands/SearchCommand.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/commands/SearchCommand.ts b/src/commands/SearchCommand.ts index 31b6413d..6658f05d 100644 --- a/src/commands/SearchCommand.ts +++ b/src/commands/SearchCommand.ts @@ -1,4 +1,4 @@ -import { Message, MessageEmbed, TextChannel, Util } from 'discord.js'; +import { Message, MessageEmbed, Util } from 'discord.js'; import PrefixCommand from './PrefixCommand'; import BotConfig from '../BotConfig'; import MojiraBot from '../MojiraBot'; @@ -19,18 +19,18 @@ export default class SearchCommand extends PrefixCommand { try { const embed = new MessageEmbed(); let searchFilter = `project in (${ BotConfig.projects.join( ', ' ) })`; - + if ( modifiers ) { for ( const modifier of modifiers ) { if ( modifier == ':query' ) { searchFilter = textArgs; break; } - if ([ 'project', 'creator', 'reporter', 'assignee', 'version', 'resolution', 'status', 'fixversion', 'confirmation', 'gamemode', 'id', 'labels', 'key', 'priority', 'mp' ].includes( modifier.split( /_|\s/g )[1] ) ) searchFilter += ` AND ${ modifier.split( /_|\s/g )[1].toLowerCase().replace( 'confirmation', '"Confirmation Status"' ).replace( 'gamemode', '"Game Mode"' ).replace( 'mp', '"Mojang Priority"' ) } = ${ modifier.split( /\s/g ).slice( 1 ).join( ' ' ) }`; - if ([ 'comment', 'summary', 'description', 'environment' ].includes( modifier.split( /_|\s/g )[1] ) ) searchFilter += ` AND ${ modifier.split( /_|\s/g )[1].toLowerCase() } ~ ${ modifier.split( /\s/g ).slice( 1 ).join( ' ' ) }` + if ( [ 'project', 'creator', 'reporter', 'assignee', 'version', 'resolution', 'status', 'fixversion', 'confirmation', 'gamemode', 'id', 'labels', 'key', 'priority', 'mp' ].includes( modifier.split( /_|\s/g )[1] ) ) searchFilter += ` AND ${ modifier.split( /_|\s/g )[1].toLowerCase().replace( 'confirmation', '"Confirmation Status"' ).replace( 'gamemode', '"Game Mode"' ).replace( 'mp', '"Mojang Priority"' ) } = ${ modifier.split( /\s/g ).slice( 1 ).join( ' ' ) }`; + if ( [ 'comment', 'summary', 'description', 'environment' ].includes( modifier.split( /_|\s/g )[1] ) ) searchFilter += ` AND ${ modifier.split( /_|\s/g )[1].toLowerCase() } ~ ${ modifier.split( /\s/g ).slice( 1 ).join( ' ' ) }`; } } - if ( textArgs && !modifiers.includes( ':query' ) ) searchFilter += ` AND text ~ "${ textArgs }"` + if ( textArgs && !modifiers.includes( ':query' ) ) searchFilter += ` AND text ~ "${ textArgs }"`; const searchResults = await MojiraBot.jira.issueSearch.searchForIssuesUsingJqlGet( { jql: searchFilter,