Skip to content

Commit

Permalink
Merge pull request #127 from m3dev/add-config-for-slack-notification
Browse files Browse the repository at this point in the history
Add config of setting make tree info for slack notification
  • Loading branch information
Ryusuketa authored Feb 21, 2020
2 parents a90c9d4 + cf9b4a6 commit 96c078d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
5 changes: 3 additions & 2 deletions gokart/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ def _try_to_send_event_summary_to_slack(slack_api: Optional[gokart.slack.SlackAP
if slack_api is None:
# do nothing
return
options = gokart.slack.SlackConfig()
with CmdlineParser.global_instance(cmdline_args) as cp:
task = cp.get_task_obj()
tree_info = gokart.make_tree_info(task, details=True)
tree_info = gokart.make_tree_info(task, details=True) if options.send_tree_info else 'Please add SlackConfig.send_tree_info to include tree-info'
task_name = type(task).__name__

comment = f'Report of {task_name}' + os.linesep + event_aggregator.get_summary()
Expand All @@ -97,7 +98,7 @@ def _try_to_send_event_summary_to_slack(slack_api: Optional[gokart.slack.SlackAP
event_aggregator.get_event_list(),
os.linesep,
'==== Tree Info ====',
tree_info,
tree_info
])
slack_api.send_snippet(comment=comment, title='event.txt', content=content)

Expand Down
7 changes: 5 additions & 2 deletions gokart/slack/slack_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@

class SlackConfig(luigi.Config):
token_name = luigi.Parameter(default='SLACK_TOKEN', description='slack token environment variable.')
channel = luigi.Parameter(default='', description='channel name for notification.')
to_user = luigi.Parameter(default='', description='Optional; user name who is supposed to be mentioned.')
channel = luigi.Parameter(default='', significant=False, description='channel name for notification.')
to_user = luigi.Parameter(default='', significant=False, description='Optional; user name who is supposed to be mentioned.')
send_tree_info = luigi.BoolParameter(default=False, significant=False,
description='When this option is true, the dependency tree of tasks is included in send messeage.'
'It is recommended to set false to this option when notification takes long time.')
38 changes: 37 additions & 1 deletion test/test_run.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import os
import unittest
from unittest.mock import patch
from unittest.mock import patch, MagicMock

import luigi
import luigi.mock


import gokart
from gokart.slack import SlackConfig
from gokart.run import _try_to_send_event_summary_to_slack


class _DummyTask(gokart.TaskOnKart):
Expand Down Expand Up @@ -46,6 +49,39 @@ def test_run_tree_info(self):
gokart.run()
self.assertTrue(gokart.make_tree_info(_DummyTask(param='test')), tree_info.output().load())

@patch('gokart.make_tree_info')
def test_try_to_send_event_summary_to_slack(self, make_tree_info_mock: MagicMock):
event_aggregator_mock = MagicMock()
event_aggregator_mock.get_summury.return_value = f'{__name__}._DummyTask'
event_aggregator_mock.get_event_list.return_value = f'{__name__}._DummyTask:[]'
make_tree_info_mock.return_value = 'tree'

def get_content(content: str, **kwargs):
self.output = content
slack_api_mock = MagicMock()
slack_api_mock.send_snippet.side_effect = get_content

cmdline_args = [f'{__name__}._DummyTask', '--param', 'test']
with patch('gokart.slack.SlackConfig.send_tree_info', True):
_try_to_send_event_summary_to_slack(slack_api_mock, event_aggregator_mock, cmdline_args)
expects = os.linesep.join(['===== Event List ====', event_aggregator_mock.get_event_list(), os.linesep, '==== Tree Info ====', 'tree'])

results = self.output
self.assertEqual(expects, results)

cmdline_args = [f'{__name__}._DummyTask', '--param', 'test']
with patch('gokart.slack.SlackConfig.send_tree_info', False):
_try_to_send_event_summary_to_slack(slack_api_mock, event_aggregator_mock, cmdline_args)
expects = os.linesep.join([
'===== Event List ====',
event_aggregator_mock.get_event_list(),
os.linesep,
'==== Tree Info ====',
'Please add SlackConfig.send_tree_info to include tree-info'])

results = self.output
self.assertEqual(expects, results)


if __name__ == '__main__':
unittest.main()

0 comments on commit 96c078d

Please sign in to comment.