def match(self, action_alias_match_api, **kwargs): """ Run a chatops command Handles requests: POST /actionalias/match """ command = action_alias_match_api.command try: # 1. Get aliases aliases = super(ActionAliasController, self)._get_all(**kwargs) # 2. Match alias(es) to command matches = match_command_to_alias(command, aliases) if len(matches) > 1: raise ActionAliasAmbiguityException( "Command '%s' matched more than 1 pattern" % command, matches=matches, command=command ) elif len(matches) == 0: raise ActionAliasAmbiguityException( "Command '%s' matched no patterns" % command, matches=[], command=command ) return [self._match_tuple_to_dict(match) for match in matches] except (ActionAliasAmbiguityException) as e: LOG.exception('Command "%s" matched (%s) patterns.', e.command, len(e.matches)) pecan.abort(http_client.BAD_REQUEST, str(e)) return [self._match_tuple_to_dict(match) for match in e.matches]
def match(self, action_alias_match_api, **kwargs): """ Run a chatops command Handles requests: POST /actionalias/match """ command = action_alias_match_api.command try: # 1. Get aliases aliases_resp = super(ActionAliasController, self)._get_all(**kwargs) aliases = [ActionAliasAPI(**alias) for alias in aliases_resp.json] # 2. Match alias(es) to command matches = match_command_to_alias(command, aliases) if len(matches) > 1: raise ActionAliasAmbiguityException( "Command '%s' matched more than 1 pattern" % command, matches=matches, command=command) elif len(matches) == 0: raise ActionAliasAmbiguityException( "Command '%s' matched no patterns" % command, matches=[], command=command) return [self._match_tuple_to_dict(match) for match in matches] except ActionAliasAmbiguityException as e: LOG.exception('Command "%s" matched (%s) patterns.', e.command, len(e.matches)) return abort(http_client.BAD_REQUEST, str(e))
def test_matching(self, mock): ALIASES = [ MemoryActionAliasDB(name="spengler", ref="ghostbusters.1", formats=["{{choice}} cross the {{target}}"]), ] COMMAND = "Don't cross the streams" match = matching.match_command_to_alias(COMMAND, ALIASES) self.assertEqual(len(match), 1) self.assertEqual(match[0]['alias'].ref, "ghostbusters.1") self.assertEqual(match[0]['representation'], "{{choice}} cross the {{target}}")