Skip to content
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

[AIRFLOW-5715] Make email, owner context available #6385

Merged
merged 1 commit into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion airflow/utils/operator_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
'AIRFLOW_CONTEXT_EXECUTION_DATE': {'default': 'airflow.ctx.execution_date',
'env_var_format': 'AIRFLOW_CTX_EXECUTION_DATE'},
'AIRFLOW_CONTEXT_DAG_RUN_ID': {'default': 'airflow.ctx.dag_run_id',
'env_var_format': 'AIRFLOW_CTX_DAG_RUN_ID'}
'env_var_format': 'AIRFLOW_CTX_DAG_RUN_ID'},
'AIRFLOW_CONTEXT_DAG_OWNER': {'default': 'airflow.ctx.dag_owner',
'env_var_format': 'AIRFLOW_CTX_DAG_OWNER'},
'AIRFLOW_CONTEXT_DAG_EMAIL': {'default': 'airflow.ctx.dag_email',
'env_var_format': 'AIRFLOW_CTX_DAG_EMAIL'},
}


Expand All @@ -48,6 +52,23 @@ def context_to_airflow_vars(context, in_env_var_format=False):
name_format = 'env_var_format'
else:
name_format = 'default'
task = context.get('task')
if task and task.email:
if isinstance(task.email, str):
params[AIRFLOW_VAR_NAME_FORMAT_MAPPING['AIRFLOW_CONTEXT_DAG_EMAIL'][
name_format]] = task.email
elif isinstance(task.email, list):
# os env variable value needs to be string
params[AIRFLOW_VAR_NAME_FORMAT_MAPPING['AIRFLOW_CONTEXT_DAG_EMAIL'][
name_format]] = ','.join(task.email)
if task and task.owner:
if isinstance(task.owner, str):
params[AIRFLOW_VAR_NAME_FORMAT_MAPPING['AIRFLOW_CONTEXT_DAG_OWNER'][
name_format]] = task.owner
elif isinstance(task.owner, list):
# os env variable value needs to be string
params[AIRFLOW_VAR_NAME_FORMAT_MAPPING['AIRFLOW_CONTEXT_DAG_OWNER'][
name_format]] = ','.join(task.owner)
task_instance = context.get('task_instance')
if task_instance and task_instance.dag_id:
params[AIRFLOW_VAR_NAME_FORMAT_MAPPING['AIRFLOW_CONTEXT_DAG_ID'][
Expand Down
11 changes: 11 additions & 0 deletions tests/utils/test_operator_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def setUp(self):
self.task_id = 'task_id'
self.execution_date = '2017-05-21T00:00:00'
self.dag_run_id = 'dag_run_id'
self.owner = ['owner1', 'owner2']
self.email = ['[email protected]']
self.context = {
'dag_run': mock.MagicMock(
name='dag_run',
Expand All @@ -46,6 +48,11 @@ def setUp(self):
execution_date=datetime.strptime(self.execution_date,
'%Y-%m-%dT%H:%M:%S'),
),
'task': mock.MagicMock(
name='task',
owner=self.owner,
email=self.email
)
}

def test_context_to_airflow_vars_empty_context(self):
Expand All @@ -59,6 +66,8 @@ def test_context_to_airflow_vars_all_context(self):
'airflow.ctx.execution_date': self.execution_date,
'airflow.ctx.task_id': self.task_id,
'airflow.ctx.dag_run_id': self.dag_run_id,
'airflow.ctx.dag_owner': 'owner1,owner2',
'airflow.ctx.dag_email': '[email protected]'
}
)

Expand All @@ -70,6 +79,8 @@ def test_context_to_airflow_vars_all_context(self):
'AIRFLOW_CTX_EXECUTION_DATE': self.execution_date,
'AIRFLOW_CTX_TASK_ID': self.task_id,
'AIRFLOW_CTX_DAG_RUN_ID': self.dag_run_id,
'AIRFLOW_CTX_DAG_OWNER': 'owner1,owner2',
'AIRFLOW_CTX_DAG_EMAIL': '[email protected]'
}
)

Expand Down