SlackWebhookHook use password instead of extra (#12674)

closes: #12214
This commit is contained in:
Darwin Yip 2020-12-02 09:09:28 -05:00 коммит произвёл GitHub
Родитель 03fa6edc7a
Коммит 2947e09999
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 35 добавлений и 2 удалений

Просмотреть файл

@ -17,6 +17,7 @@
# under the License.
#
import json
import warnings
from typing import Optional
from airflow.exceptions import AirflowException
@ -104,8 +105,21 @@ class SlackWebhookHook(HttpHook):
return token
elif http_conn_id:
conn = self.get_connection(http_conn_id)
extra = conn.extra_dejson
return extra.get('webhook_token', '')
if getattr(conn, 'password', None):
return conn.password
else:
extra = conn.extra_dejson
web_token = extra.get('webhook_token', '')
if web_token:
warnings.warn(
"'webhook_token' in 'extra' is deprecated. Please use 'password' field",
DeprecationWarning,
stacklevel=2,
)
return web_token
else:
raise AirflowException('Cannot get token: No valid Slack webhook token nor conn_id supplied')

Просмотреть файл

@ -76,6 +76,13 @@ class TestSlackWebhookHook(unittest.TestCase):
conn_id='slack-webhook-host', conn_type='http', host='https://hooks.slack.com/services/T000/'
)
)
db.merge_conn(
Connection(
conn_id='slack-webhook-with-password',
conn_type='http',
password='your_token_here',
)
)
def test_get_token_manual_token(self):
# Given
@ -100,6 +107,18 @@ class TestSlackWebhookHook(unittest.TestCase):
# Then
self.assertEqual(webhook_token, expected_webhook_token)
def test_get_token_conn_id_password(self):
# Given
conn_id = 'slack-webhook-with-password'
hook = SlackWebhookHook(http_conn_id=conn_id)
expected_webhook_token = 'your_token_here'
# When
webhook_token = hook._get_token(None, conn_id)
# Then
self.assertEqual(webhook_token, expected_webhook_token)
def test_build_slack_message(self):
# Given
hook = SlackWebhookHook(**self._config)