-
Notifications
You must be signed in to change notification settings - Fork 17.4k
Move send_file method into SlackHook #26118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -16,10 +16,13 @@ | |
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| import json | ||
| from typing import Any, Dict, List, Optional, Sequence | ||
| import warnings | ||
| from typing import Any, Dict, List, Optional, Sequence, Union | ||
|
|
||
| from airflow.compat.functools import cached_property | ||
| from airflow.models import BaseOperator | ||
| from airflow.providers.slack.hooks.slack import SlackHook | ||
| from airflow.utils.log.secrets_masker import mask_secret | ||
|
|
||
|
|
||
| class SlackAPIOperator(BaseOperator): | ||
|
|
@@ -47,13 +50,19 @@ def __init__( | |
| **kwargs, | ||
| ) -> None: | ||
| super().__init__(**kwargs) | ||
|
|
||
| self.token = token # type: Optional[str] | ||
| self.slack_conn_id = slack_conn_id # type: Optional[str] | ||
| if token: | ||
| mask_secret(token) | ||
| self.token = token | ||
| self.slack_conn_id = slack_conn_id | ||
|
|
||
| self.method = method | ||
| self.api_params = api_params | ||
|
|
||
| @cached_property | ||
| def hook(self) -> SlackHook: | ||
| """Slack Hook.""" | ||
| return SlackHook(token=self.token, slack_conn_id=self.slack_conn_id) | ||
|
|
||
| def construct_api_call_params(self) -> Any: | ||
| """ | ||
| Used by the execute function. Allows templating on the source fields | ||
|
|
@@ -70,14 +79,9 @@ def construct_api_call_params(self) -> Any: | |
| ) | ||
|
|
||
| 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, json=self.api_params) | ||
| self.hook.call(self.method, json=self.api_params) | ||
|
|
||
|
|
||
| class SlackAPIPostOperator(SlackAPIOperator): | ||
|
|
@@ -144,7 +148,7 @@ def construct_api_call_params(self) -> Any: | |
|
|
||
| class SlackAPIFileOperator(SlackAPIOperator): | ||
| """ | ||
| Send a file to a slack channel | ||
| Send a file to a slack channels | ||
| Examples: | ||
|
|
||
| .. code-block:: python | ||
|
|
@@ -154,7 +158,7 @@ class SlackAPIFileOperator(SlackAPIOperator): | |
| task_id="slack_file_upload_1", | ||
| dag=dag, | ||
| slack_conn_id="slack", | ||
| channel="#general", | ||
| channels="#general,#random", | ||
|
Comment on lines
157
to
161
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Slack API |
||
| initial_comment="Hello World!", | ||
| filename="/files/dags/test.txt", | ||
| filetype="txt", | ||
|
|
@@ -165,63 +169,67 @@ class SlackAPIFileOperator(SlackAPIOperator): | |
| task_id="slack_file_upload_2", | ||
| dag=dag, | ||
| slack_conn_id="slack", | ||
| channel="#general", | ||
| channels="#general", | ||
| initial_comment="Hello World!", | ||
| content="file content in txt", | ||
| ) | ||
|
|
||
| :param channel: channel in which to sent file on slack name (templated) | ||
| :param channels: Comma-separated list of channel names or IDs where the file will be shared. | ||
| If set this argument to None, then file will send to associated workspace. (templated) | ||
| :param initial_comment: message to send to slack. (templated) | ||
| :param filename: name of the file (templated) | ||
| :param filetype: slack filetype. (templated) | ||
| - see https://api.slack.com/types/file | ||
| :param filetype: slack filetype. (templated) See: https://api.slack.com/types/file#file_types | ||
| :param content: file content. (templated) | ||
| :param title: title of file. (templated) | ||
| :param channel: (deprecated) channel in which to sent file on slack name | ||
| """ | ||
|
|
||
| template_fields: Sequence[str] = ('channel', 'initial_comment', 'filename', 'filetype', 'content') | ||
| template_fields: Sequence[str] = ( | ||
| 'channels', | ||
| 'initial_comment', | ||
| 'filename', | ||
| 'filetype', | ||
| 'content', | ||
| 'title', | ||
| ) | ||
| ui_color = '#44BEDF' | ||
|
|
||
| def __init__( | ||
| self, | ||
| channel: str = '#general', | ||
| initial_comment: str = 'No message has been set!', | ||
| channels: Optional[Union[str, Sequence[str]]] = None, | ||
| initial_comment: Optional[str] = None, | ||
| filename: Optional[str] = None, | ||
| filetype: Optional[str] = None, | ||
| content: Optional[str] = None, | ||
| title: Optional[str] = None, | ||
| channel: Optional[str] = None, | ||
| **kwargs, | ||
| ) -> None: | ||
| self.method = 'files.upload' | ||
| self.channel = channel | ||
| if channel: | ||
| warnings.warn( | ||
| "Argument `channel` is deprecated and will removed in a future releases. " | ||
| "Please use `channels` instead.", | ||
| DeprecationWarning, | ||
| stacklevel=2, | ||
| ) | ||
| if channels: | ||
| raise ValueError(f"Cannot set both arguments: channel={channel!r} and channels={channels!r}.") | ||
| channels = channel | ||
|
|
||
| self.channels = channels | ||
| self.initial_comment = initial_comment | ||
| self.filename = filename | ||
| self.filetype = filetype | ||
| self.content = content | ||
| self.file_params: Dict = {} | ||
| super().__init__(method=self.method, **kwargs) | ||
| self.title = title | ||
| super().__init__(method="files.upload", **kwargs) | ||
|
|
||
| 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 | ||
| """ | ||
| slack = SlackHook(token=self.token, slack_conn_id=self.slack_conn_id) | ||
|
|
||
| # If file content is passed. | ||
| if self.content is not None: | ||
| self.api_params = { | ||
| 'channels': self.channel, | ||
| 'content': self.content, | ||
| 'initial_comment': self.initial_comment, | ||
| } | ||
| slack.call(self.method, data=self.api_params) | ||
| # If file name is passed. | ||
| elif self.filename is not None: | ||
| self.api_params = { | ||
| 'channels': self.channel, | ||
| 'filename': self.filename, | ||
| 'filetype': self.filetype, | ||
| 'initial_comment': self.initial_comment, | ||
| } | ||
| with open(self.filename, "rb") as file_handle: | ||
| slack.call(self.method, data=self.api_params, files={'file': file_handle}) | ||
| file_handle.close() | ||
| self.hook.send_file( | ||
| channels=self.channels, | ||
| # For historical reason SlackAPIFileOperator use filename as reference to file | ||
| file=self.filename, | ||
| content=self.content, | ||
| initial_comment=self.initial_comment, | ||
| title=self.title, | ||
| ) | ||
This file contains hidden or 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 was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually slack_sdk v3 always validate response and raise an error