dabla commented on code in PR #55068:
URL: https://github.com/apache/airflow/pull/55068#discussion_r2962022290


##########
airflow-core/src/airflow/triggers/base.py:
##########
@@ -66,14 +79,68 @@ class BaseTrigger(abc.ABC, LoggingMixin):
     supports_triggerer_queue: bool = True
 
     def __init__(self, **kwargs):
+        super().__init__()
         # these values are set by triggerer when preparing to run the instance
         # when run, they are injected into logger record.
-        self.task_instance = None
+        self._task_instance = None
         self.trigger_id = None
+        self.template_fields = ()
+        self.template_ext = ()
 
     def _set_context(self, context):
         """Part of LoggingMixin and used mainly for configuration of task 
logging; not used for triggers."""
-        raise NotImplementedError
+        pass
+
+    @property
+    def task(self) -> Operator | None:
+        # We must check if the TaskInstance is the generated Pydantic one or 
the RuntimeTaskInstance
+        if self.task_instance and hasattr(self.task_instance, "task"):
+            return self.task_instance.task
+        return None
+
+    @property
+    def task_instance(self) -> TaskInstance:
+        return self._task_instance
+
+    @task_instance.setter
+    def task_instance(self, value: TaskInstance | None) -> None:
+        self._task_instance = value
+        if self.task:
+            self.template_fields = self.task.template_fields
+            self.template_ext = self.task.template_ext
+
+    def render_template_fields(
+        self,
+        context: Context,
+        jinja_env: jinja2.Environment | None = None,
+    ) -> None:
+        """
+        Template all attributes listed in *self.template_fields*.
+
+        This mutates the attributes in-place and is irreversible.
+
+        :param context: Context dict with values to apply on content.
+        :param jinja_env: Jinja's environment to use for rendering.
+        """
+        # We only need to render templated fields if templated fields are part 
of the start_trigger_args
+        for attr_name in self.template_fields:
+            value = getattr(self, attr_name, None)
+
+            if value:
+                try:
+                    rendered_content = self.render_template(value, context, 
jinja_env)
+                except Exception:
+                    # TODO: Mask the value. Depends on 
https://github.com/apache/airflow/issues/45438

Review Comment:
   I've refactored the code so _do_render_template_fields method is now part of 
Templater and can be re-used across AbstractOperator and Templater which is 
more DRY.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to