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

Initial support for search arguments #241

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/commands/SearchCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,35 @@ 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,
fields: [ 'key', 'summary' ],
} );

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( { embeds: [embed] } );
return false;
}
Expand All @@ -41,7 +57,7 @@ export default class SearchCommand extends PrefixCommand {
await message.channel.send( { embeds: [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( { embeds: [embed] } );
return false;
}
Expand All @@ -52,4 +68,4 @@ export default class SearchCommand extends PrefixCommand {
public asString( args: string ): string {
return `!jira search ${ args }`;
}
}
}