From c68567eb7dff958f021e46b502d2659decaa2d9b Mon Sep 17 00:00:00 2001 From: subkanthi Date: Tue, 27 Jul 2021 00:26:28 -0400 Subject: [PATCH 1/6] [14168] Fixed SlackAPIFileOperator to upload file and file content. --- airflow/providers/slack/operators/slack.py | 41 ++++++++++++++----- tests/providers/slack/operators/test_slack.py | 33 +++++++++++++-- 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/airflow/providers/slack/operators/slack.py b/airflow/providers/slack/operators/slack.py index 96a8ea09582d0..3d16a288f1157 100644 --- a/airflow/providers/slack/operators/slack.py +++ b/airflow/providers/slack/operators/slack.py @@ -192,9 +192,9 @@ def __init__( self, channel: str = '#general', initial_comment: str = 'No message has been set!', - filename: str = 'default_name.csv', - filetype: str = 'csv', - content: str = 'default,content,csv,file', + filename: str = None, + filetype: str = None, + content: str = None, **kwargs, ) -> None: self.method = 'files.upload' @@ -203,13 +203,34 @@ def __init__( self.filename = filename self.filetype = filetype self.content = content + self.file_params = {} super().__init__(method=self.method, **kwargs) def construct_api_call_params(self) -> Any: - self.api_params = { - 'channels': self.channel, - 'content': self.content, - 'filename': self.filename, - 'filetype': self.filetype, - 'initial_comment': self.initial_comment, - } + if self.content is not None: + self.api_params = { + 'channels': self.channel, + 'content': self.content, + 'initial_comment': self.initial_comment + } + elif self.filename is not None: + self.api_params = { + 'channels': self.channel, + 'filename': self.filename, + 'filetype': self.filetype, + 'initial_comment': self.initial_comment, + } + self.file_params = { + 'file': self.filename + } + + def execute(self, **kwargs): + """ + The SlackAPIOperator calls will not fail even if the call is not unsuccessful. + It should not prevent a DAG from completing in success + """ + if not self.api_params: + self.construct_api_call_params() + slack = SlackHook(token=self.token, slack_conn_id=self.slack_conn_id) + slack.call(self.method, data=self.api_params, files=self.file_params) + diff --git a/tests/providers/slack/operators/test_slack.py b/tests/providers/slack/operators/test_slack.py index a60c193341081..e94b693f0509a 100644 --- a/tests/providers/slack/operators/test_slack.py +++ b/tests/providers/slack/operators/test_slack.py @@ -188,12 +188,13 @@ def test_init_with_valid_params(self): assert slack_api_post_operator.slack_conn_id == test_slack_conn_id @mock.patch('airflow.providers.slack.operators.slack.SlackHook') - def test_api_call_params_with_default_args(self, mock_hook): + def test_api_call_params_with_content_args(self, mock_hook): test_slack_conn_id = 'test_slack_conn_id' slack_api_post_operator = SlackAPIFileOperator( task_id='slack', slack_conn_id=test_slack_conn_id, + content='test-content' ) slack_api_post_operator.execute() @@ -201,8 +202,32 @@ def test_api_call_params_with_default_args(self, mock_hook): expected_api_params = { 'channels': '#general', 'initial_comment': 'No message has been set!', - 'filename': 'default_name.csv', - 'filetype': 'csv', - 'content': 'default,content,csv,file', + 'content': 'test-content', + } + assert expected_api_params == slack_api_post_operator.api_params + + @mock.patch('airflow.providers.slack.operators.slack.SlackHook') + def test_api_call_params_with_file_args(self, mock_hook): + test_slack_conn_id = 'test_slack_conn_id' + + slack_api_post_operator = SlackAPIFileOperator( + task_id='slack', + slack_conn_id=test_slack_conn_id, + filename='test.csv', + filetype='csv' + ) + + slack_api_post_operator.execute() + + expected_api_params = { + 'channels': '#general', + 'initial_comment': 'No message has been set!', + 'filename': 'test.csv', + 'filetype': 'csv' + } + + expected_file_params = { + 'file': 'test.csv' } assert expected_api_params == slack_api_post_operator.api_params + assert expected_file_params == slack_api_post_operator.file_params From efe7047229a8fc0f85d9a704fe3b3a3ba97f6539 Mon Sep 17 00:00:00 2001 From: subkanthi Date: Tue, 27 Jul 2021 09:02:31 -0400 Subject: [PATCH 2/6] [14168] Fixed Lint errors --- airflow/providers/slack/operators/slack.py | 7 ++----- tests/providers/slack/operators/test_slack.py | 15 ++++----------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/airflow/providers/slack/operators/slack.py b/airflow/providers/slack/operators/slack.py index 3d16a288f1157..3bb73e44f7f71 100644 --- a/airflow/providers/slack/operators/slack.py +++ b/airflow/providers/slack/operators/slack.py @@ -211,7 +211,7 @@ def construct_api_call_params(self) -> Any: self.api_params = { 'channels': self.channel, 'content': self.content, - 'initial_comment': self.initial_comment + 'initial_comment': self.initial_comment, } elif self.filename is not None: self.api_params = { @@ -220,9 +220,7 @@ def construct_api_call_params(self) -> Any: 'filetype': self.filetype, 'initial_comment': self.initial_comment, } - self.file_params = { - 'file': self.filename - } + self.file_params = {'file': self.filename} def execute(self, **kwargs): """ @@ -233,4 +231,3 @@ def execute(self, **kwargs): self.construct_api_call_params() slack = SlackHook(token=self.token, slack_conn_id=self.slack_conn_id) slack.call(self.method, data=self.api_params, files=self.file_params) - diff --git a/tests/providers/slack/operators/test_slack.py b/tests/providers/slack/operators/test_slack.py index e94b693f0509a..964bb9cbef7d5 100644 --- a/tests/providers/slack/operators/test_slack.py +++ b/tests/providers/slack/operators/test_slack.py @@ -192,9 +192,7 @@ def test_api_call_params_with_content_args(self, mock_hook): test_slack_conn_id = 'test_slack_conn_id' slack_api_post_operator = SlackAPIFileOperator( - task_id='slack', - slack_conn_id=test_slack_conn_id, - content='test-content' + task_id='slack', slack_conn_id=test_slack_conn_id, content='test-content' ) slack_api_post_operator.execute() @@ -211,10 +209,7 @@ def test_api_call_params_with_file_args(self, mock_hook): test_slack_conn_id = 'test_slack_conn_id' slack_api_post_operator = SlackAPIFileOperator( - task_id='slack', - slack_conn_id=test_slack_conn_id, - filename='test.csv', - filetype='csv' + task_id='slack', slack_conn_id=test_slack_conn_id, filename='test.csv', filetype='csv' ) slack_api_post_operator.execute() @@ -223,11 +218,9 @@ def test_api_call_params_with_file_args(self, mock_hook): 'channels': '#general', 'initial_comment': 'No message has been set!', 'filename': 'test.csv', - 'filetype': 'csv' + 'filetype': 'csv', } - expected_file_params = { - 'file': 'test.csv' - } + expected_file_params = {'file': 'test.csv'} assert expected_api_params == slack_api_post_operator.api_params assert expected_file_params == slack_api_post_operator.file_params From da9ed8f90277eb3419eb443bdec1c6eb79e4e825 Mon Sep 17 00:00:00 2001 From: subkanthi Date: Tue, 27 Jul 2021 09:35:27 -0400 Subject: [PATCH 3/6] [14168] Updated documentation for Slack API File Operator --- airflow/providers/slack/operators/slack.py | 26 +++++++++++++++------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/airflow/providers/slack/operators/slack.py b/airflow/providers/slack/operators/slack.py index 3bb73e44f7f71..20d2102c33c65 100644 --- a/airflow/providers/slack/operators/slack.py +++ b/airflow/providers/slack/operators/slack.py @@ -161,15 +161,25 @@ class SlackAPIFileOperator(SlackAPIOperator): .. code-block:: python + # Send file with filename and filetype slack = SlackAPIFileOperator( - task_id="slack_file_upload", - dag=dag, - slack_conn_id="slack", - channel="#general", - initial_comment="Hello World!", - filename="hello_world.csv", - filetype="csv", - content="hello,world,csv,file", + task_id="slack_file_upload", + dag=dag, + slack_conn_id="slack", + channel="#general", + initial_comment="Hello World!", + filename="hello_world.csv", + filetype="csv" + ) + + # Send file content + slack = SlackAPIFileOperator( + task_id="slack_file_upload", + dag=dag, + slack_conn_id="slack", + channel="#general", + initial_comment="Hello World!", + content="file content in txt", ) :param channel: channel in which to sent file on slack name (templated) From fa542e0ab45837b278235af919cb64e80e823228 Mon Sep 17 00:00:00 2001 From: subkanthi Date: Wed, 28 Jul 2021 10:22:26 -0400 Subject: [PATCH 4/6] [14168] Fixed indendation in SlackAPIFileOperator docs --- airflow/providers/slack/operators/slack.py | 40 +++++++++++----------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/airflow/providers/slack/operators/slack.py b/airflow/providers/slack/operators/slack.py index 20d2102c33c65..95475c110aee5 100644 --- a/airflow/providers/slack/operators/slack.py +++ b/airflow/providers/slack/operators/slack.py @@ -161,26 +161,26 @@ class SlackAPIFileOperator(SlackAPIOperator): .. code-block:: python - # Send file with filename and filetype - slack = SlackAPIFileOperator( - task_id="slack_file_upload", - dag=dag, - slack_conn_id="slack", - channel="#general", - initial_comment="Hello World!", - filename="hello_world.csv", - filetype="csv" - ) - - # Send file content - slack = SlackAPIFileOperator( - task_id="slack_file_upload", - dag=dag, - slack_conn_id="slack", - channel="#general", - initial_comment="Hello World!", - content="file content in txt", - ) + # Send file with filename and filetype + slack = SlackAPIFileOperator( + task_id="slack_file_upload", + dag=dag, + slack_conn_id="slack", + channel="#general", + initial_comment="Hello World!", + filename="hello_world.csv", + filetype="csv" + ) + + # Send file content + slack = SlackAPIFileOperator( + task_id="slack_file_upload", + dag=dag, + slack_conn_id="slack", + channel="#general", + initial_comment="Hello World!", + content="file content in txt", + ) :param channel: channel in which to sent file on slack name (templated) :type channel: str From f1c163ad73ec9ab8b2f601e15415c6b88b3586f8 Mon Sep 17 00:00:00 2001 From: Kanthi Date: Fri, 30 Jul 2021 02:33:45 -0400 Subject: [PATCH 5/6] Update airflow/providers/slack/operators/slack.py Fixed indentation. Co-authored-by: Tzu-ping Chung --- airflow/providers/slack/operators/slack.py | 40 +++++++++++----------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/airflow/providers/slack/operators/slack.py b/airflow/providers/slack/operators/slack.py index 95475c110aee5..70b8896ab4101 100644 --- a/airflow/providers/slack/operators/slack.py +++ b/airflow/providers/slack/operators/slack.py @@ -161,26 +161,26 @@ class SlackAPIFileOperator(SlackAPIOperator): .. code-block:: python - # Send file with filename and filetype - slack = SlackAPIFileOperator( - task_id="slack_file_upload", - dag=dag, - slack_conn_id="slack", - channel="#general", - initial_comment="Hello World!", - filename="hello_world.csv", - filetype="csv" - ) - - # Send file content - slack = SlackAPIFileOperator( - task_id="slack_file_upload", - dag=dag, - slack_conn_id="slack", - channel="#general", - initial_comment="Hello World!", - content="file content in txt", - ) + # Send file with filename and filetype + slack = SlackAPIFileOperator( + task_id="slack_file_upload", + dag=dag, + slack_conn_id="slack", + channel="#general", + initial_comment="Hello World!", + filename="hello_world.csv", + filetype="csv" + ) + + # Send file content + slack = SlackAPIFileOperator( + task_id="slack_file_upload", + dag=dag, + slack_conn_id="slack", + channel="#general", + initial_comment="Hello World!", + content="file content in txt", + ) :param channel: channel in which to sent file on slack name (templated) :type channel: str From df0aa9d5362873d3af1dc950deab8fc0364c9717 Mon Sep 17 00:00:00 2001 From: subkanthi Date: Fri, 30 Jul 2021 10:53:19 -0400 Subject: [PATCH 6/6] Fixed docs comment in Slack.py --- airflow/providers/slack/operators/slack.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/airflow/providers/slack/operators/slack.py b/airflow/providers/slack/operators/slack.py index 70b8896ab4101..053ce3b38e8b2 100644 --- a/airflow/providers/slack/operators/slack.py +++ b/airflow/providers/slack/operators/slack.py @@ -169,7 +169,7 @@ class SlackAPIFileOperator(SlackAPIOperator): channel="#general", initial_comment="Hello World!", filename="hello_world.csv", - filetype="csv" + filetype="csv", ) # Send file content