Skip to content

Commit

Permalink
Merge pull request #9 from AntonNguyen/emotional-emojis
Browse files Browse the repository at this point in the history
Introduced the concept of 'happy' emojis
  • Loading branch information
AntonNguyen authored Apr 17, 2018
2 parents 28d3eac + 427675a commit 14d602d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pullsbury/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def load_config():
os.environ['REQUESTS_CA_BUNDLE'] = config.get('SSL_CA_BUNDLE')

json_values = [
'TEAMS', 'SLACK_EMOJIS', 'REPO_BLACKLIST', 'SLACK_CUSTOM_EMOJI_MAPPING'
'TEAMS', 'HAPPY_SLACK_EMOJIS', 'REPO_BLACKLIST', 'SLACK_CUSTOM_EMOJI_MAPPING'
]

for value in json_values:
Expand Down
8 changes: 4 additions & 4 deletions pullsbury/handlers/slack_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def __init__(self, event, config):
self.event = event
self.slack = SlackClient(config.get('SLACK_AUTH_TOKEN'))
self.channels_to_notify = self.parse_teams(config.get('TEAMS'), event.author)
self.emojis = config.get('SLACK_EMOJIS')
self.happy_emojis = config.get('HAPPY_SLACK_EMOJIS')
self.slack_icon = config.get('SLACK_ICON')
self.repo_blacklist = config.get('REPO_BLACKLIST')
self.slack_custom_emoji_mapping = config.get('SLACK_CUSTOM_EMOJI_MAPPING', {})
Expand Down Expand Up @@ -72,7 +72,7 @@ def should_handle(self):
return True

def get_emoji(self, author):
if not len(self.emojis):
if not len(self.happy_emojis):
return ":heart:"

if self.slack_custom_emoji_mapping.get(author):
Expand All @@ -81,6 +81,6 @@ def get_emoji(self, author):
name_index = len(author)
for character in author:
name_index = name_index + ord(character)
emoji_index = name_index % len(self.emojis)
emoji_index = name_index % len(self.happy_emojis)

return ":{}:".format(self.emojis[emoji_index])
return ":{}:".format(self.happy_emojis[emoji_index])
2 changes: 1 addition & 1 deletion settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def env(key, default, cast=str):
# Slack settings
SLACK_AUTH_TOKEN = env('SLACK_AUTH_TOKEN', '')
SLACK_ICON = env('SLACK_ICON', 'https://i.imgur.com/oEL0h26.jpg')
SLACK_EMOJIS = env('SLACK_EMOJIS', json.dumps([
HAPPY_SLACK_EMOJIS = env('HAPPY_SLACK_EMOJIS', json.dumps([
"exclamation",
"heart",
"icecream",
Expand Down
16 changes: 15 additions & 1 deletion tests/handlers/test_slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_get_emoji_returns_same_emoji_for_author(self):
def test_get_emoji_returns_emoji_even_if_emoji_list_is_empty(self):
event = TestableEvent()
self.config.update({
'SLACK_EMOJIS': []
'HAPPY_SLACK_EMOJIS': []
})
handler = SlackHandler(event, self.config)
eq_(handler.get_emoji('anton'), ':heart:')
Expand All @@ -70,6 +70,20 @@ def test_get_emoji_returns_correct_custom_emoji(self):
eq_(handler.get_emoji('anton'), ':sparkles:')
eq_(handler.get_emoji('nguyen'), ':snowman:')

def test_get_emoji_returns_correct_custom_emoji(self):
event = TestableEvent()
self.config.update({
'SLACK_CUSTOM_EMOJI_MAPPING': {
'batman': 'joker'
}
})

handler = SlackHandler(event, self.config)
eq_(handler.get_emoji('batman'), ':joker:')
# non custom emojis return the default response
eq_(handler.get_emoji('anton'), ':sparkles:')
eq_(handler.get_emoji('nguyen'), ':snowman:')

def test_parse_teams_returns_empty_list_if_no_teams_provided(self):
event = TestableEvent()
handler = SlackHandler(event, self.config)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_load_config():
res = load_config()
assert res['GITHUB_USER'].endswith, 'Exists and is stringy'

def test_load_config_handles_slack_emojis():
def test_load_config_handles_happy_slack_emojis():
expected_emojis = [
"exclamation",
"heart",
Expand All @@ -24,7 +24,7 @@ def test_load_config_handles_slack_emojis():
]

res = load_config()
actual_emojis = res.get('SLACK_EMOJIS')
actual_emojis = res.get('HAPPY_SLACK_EMOJIS')

ok_(len(actual_emojis) > 0)
eq_(actual_emojis, expected_emojis)
Expand Down

0 comments on commit 14d602d

Please sign in to comment.