-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Karl-Aksel Puulmann
committed
Feb 10, 2017
1 parent
3add0f6
commit 1395915
Showing
3 changed files
with
52 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ def initialize(config) | |
def self.manual | ||
{ | ||
description: "Add alias for user or schedule", | ||
syntax: ["alias FIELDVALUE as NEW_ALIAS"], | ||
syntax: ["alias USER_OR_SCHEDULE as NEW_ALIAS"], | ||
examples: [ | ||
"alias [email protected] as thebestkarl", | ||
"alias PIBRSYV as triage" | ||
|
@@ -21,49 +21,50 @@ def self.manual | |
def parse(query) | ||
return unless query[:command] == "alias" | ||
|
||
parse_stage = :field_value | ||
result = { | ||
field_value: [], | ||
new_alias: [], | ||
parse_stage = :searched_alias | ||
names = { | ||
searched_alias: [], | ||
new_alias: [] | ||
} | ||
|
||
query[:words].each do |word| | ||
case word | ||
when 'as' | ||
parse_stage = :new_alias | ||
else | ||
result[parse_stage] << word | ||
names[parse_stage] << word | ||
end | ||
end | ||
|
||
join_all(result) | ||
names.each do |stage, nameparts| | ||
names[stage] = nameparts.join(' ') | ||
end | ||
end | ||
|
||
def matches_in(collection, expected) | ||
def matches_in(collection, nameCandidate) | ||
fields_to_scan = [:email, :name, :id] | ||
|
||
normalized = PagerBot::Utilities.normalize(expected) | ||
normalizedCandidate = PagerBot::Utilities.normalize(nameCandidate) | ||
|
||
collection.list.select do |member| | ||
collection.select do |member| | ||
fields_to_scan.any? do |field| | ||
member[field] && member.normalized(field) == normalized | ||
member[field] && PagerBot::Utilities.normalize(member[field]) == normalizedCandidate | ||
end | ||
end | ||
end | ||
|
||
+PagerBot::Utilities::DispatchMethod | ||
def dispatch(query, event_data) | ||
users_matches = matches_in(pagerduty.users, query[:field_value]) | ||
schedule_matches = matches_in(pagerduty.schedules, query[:field_value]) | ||
users_matches = matches_in(pagerduty.users.list, query[:searched_alias]) | ||
schedule_matches = matches_in(pagerduty.schedules.list, query[:searched_alias]) | ||
new_alias = PagerBot::Utilities.normalize(query[:new_alias]) | ||
|
||
# if no matches | ||
if users_matches.empty? && schedule_matches.empty? | ||
return "Could not find #{query[:field_value]}." | ||
return "Could not find user or schedule #{query[:searched_alias]}." | ||
end | ||
# if too many matches | ||
# Ambigious matches. | ||
if users_matches.length + schedule_matches.length > 1 | ||
return "Field #{query[:field_value]} is ambiguous." | ||
return "Field #{query[:searched_alias]} is ambiguous." | ||
end | ||
# base case, only one find! | ||
store = PagerBot::DataStore.new | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require_relative('../../../_lib') | ||
|
||
class AddAlias < Critic::MockedPagerDutyTest | ||
before do | ||
@plugin = PagerBot::PluginManager.load_plugin "add_alias", {} | ||
end | ||
|
||
describe 'Adding an alias to user or schedule' do | ||
describe 'parsing' do | ||
it 'should parse a simple example' do | ||
got = @plugin.parse({ | ||
command: "alias", | ||
words: "[email protected] as the best-karl".split | ||
}) | ||
assert_equal got, { | ||
searched_alias: '[email protected]', | ||
new_alias: 'the best-karl' | ||
} | ||
end | ||
end | ||
|
||
describe 'matching' do | ||
it 'should match on id, name, email by default if they are present' do | ||
assert_equal @plugin.matches_in([name: 'foo'], 'foo'), [name: 'foo'] | ||
assert_equal @plugin.matches_in([name: 'foobar'], 'foo'), [] | ||
assert_equal @plugin.matches_in([], 'aaa'), [] | ||
assert_equal @plugin.matches_in([{}], 'aaa'), [] | ||
assert_equal @plugin.matches_in([name: nil], 'aaa'), [] | ||
assert_equal @plugin.matches_in([email: 'foo'], 'foo'), [email: 'foo'] | ||
assert_equal @plugin.matches_in([id: 'foo'], 'foo'), [id: 'foo'] | ||
end | ||
end | ||
end | ||
end |