Repository: apache/airflow
File: providers/microsoft/azure/src/airflow/providers/microsoft/azure/triggers/data_factory.py (Python)

Code before:
        return (
            "airflow.providers.microsoft.azure.triggers.data_factory.ADFPipelineRunStatusSensorTrigger",
            {
                "run_id": self.run_id,
                "azure_data_factory_conn_id": self.azure_data_factory_conn_id,
                "resource_group_name": self.resource_group_name,
                "factory_name": self.factory_name,
                "poke_interval": self.poke_interval,
            },
        )

    async def run(self) -> AsyncIterator[TriggerEvent]:
        """Make async connection to Azure Data Factory, polls for the pipeline run status."""
        hook = AzureDataFactoryAsyncHook(azure_data_factory_conn_id=self.azure_data_factory_conn_id)
        executed_after_token_refresh = False
        try:
            async with hook:
                while True:
                    try:
                        pipeline_status = await hook.get_adf_pipeline_run_status(
                            run_id=self.run_id,
                            resource_group_name=self.resource_group_name,
                            factory_name=self.factory_name,
                        )
                        executed_after_token_refresh = False
                        if pipeline_status == AzureDataFactoryPipelineRunStatus.FAILED:
                            yield TriggerEvent(
                                {"status": "error", "message": f"Pipeline run {self.run_id} has Failed."}
                            )
                            return
                        elif pipeline_status == AzureDataFactoryPipelineRunStatus.CANCELLED:
                            msg = f"Pipeline run {self.run_id} has been Cancelled."
                            yield TriggerEvent({"status": "error", "message": msg})
                            return
                        elif pipeline_status == AzureDataFactoryPipelineRunStatus.SUCCEEDED:
                            msg = f"Pipeline run {self.run_id} has been Succeeded."
                            yield TriggerEvent({"status": "success", "message": msg})
                            return
                        await asyncio.sleep(self.poke_interval)
                    except ServiceRequestError:
                        # conn might expire during long running pipeline.
                        # If exception is caught, it tries to refresh connection once.
                        # If it still doesn't fix the issue,
                        # than the execute_after_token_refresh would still be False
                        # and an exception will be raised
                        if executed_after_token_refresh:
                            await hook.refresh_conn()
                            executed_after_token_refresh = False
                        else:
                            raise
        except Exception as e:

Review comment (bug):
Maybe write a helper method which constructs the TriggerEvent base on the pipeline_status so then you could yield the resulting event from it and encapsulating the if/else in a dedicated method.

```
def trigger_event_from_pipline_status(self, pipeline_status: AzureDataFactoryPipelineRunStatus) -> TriggerEvent:
    if pipeline_status == AzureDataFactoryPipelineRunStatus.FAILED:
        return TriggerEvent(
            {"status": "error", "message": f"Pipeline run {self.run_id} has Failed."}
        )
    if pipeline_status == AzureDataFactoryPipelineRunStatus.CANCELLED:
        msg = f"Pipeline run {self.run_id} has been Cancelled."
        return TriggerEvent({"status": "error", "message": msg})
    if pipeline_status == AzureDataFactoryPipelineRunStatus.SUCCEEDED:
        msg = f"Pipeline run {self.run_id} has been Succeeded."
        return TriggerEvent({"status": "success", "message": msg})
        
...

        event = self.trigger_event_from_pipline_status(pipeline_status)
        yield event
```

Code after:
        return (
            "airflow.providers.microsoft.azure.triggers.data_factory.ADFPipelineRunStatusSensorTrigger",
            {
                "run_id": self.run_id,
                "azure_data_factory_conn_id": self.azure_data_factory_conn_id,
                "resource_group_name": self.resource_group_name,
                "factory_name": self.factory_name,
                "poke_interval": self.poke_interval,
            },
        )

    def _build_trigger_event(self, pipeline_status: str) -> TriggerEvent | None:
        """Build TriggerEvent based on pipeline status. Returns None if status is not terminal."""
        if pipeline_status == AzureDataFactoryPipelineRunStatus.FAILED:
            return TriggerEvent({"status": "error", "message": f"Pipeline run {self.run_id} has Failed."})
        if pipeline_status == AzureDataFactoryPipelineRunStatus.CANCELLED:
            return TriggerEvent(
                {"status": "error", "message": f"Pipeline run {self.run_id} has been Cancelled."}
            )
        if pipeline_status == AzureDataFactoryPipelineRunStatus.SUCCEEDED:
            return TriggerEvent(
                {"status": "success", "message": f"Pipeline run {self.run_id} has been Succeeded."}
            )
        return None

    async def run(self) -> AsyncIterator[TriggerEvent]:
        """Make async connection to Azure Data Factory, polls for the pipeline run status."""
        executed_after_token_refresh = False
        try:
            async with AzureDataFactoryAsyncHook(
                azure_data_factory_conn_id=self.azure_data_factory_conn_id
            ) as hook:
                while True:
                    try:
                        pipeline_status = await hook.get_adf_pipeline_run_status(
                            run_id=self.run_id,
                            resource_group_name=self.resource_group_name,
                            factory_name=self.factory_name,
                        )
                        executed_after_token_refresh = False
                        event = self._build_trigger_event(pipeline_status)
                        if event:
                            yield event
                            return
                        await asyncio.sleep(self.poke_interval)
                    except ServiceRequestError:
                        # conn might expire during long running pipeline.
                        # If exception is caught, it tries to refresh connection once.
                        # If it still doesn't fix the issue,
                        # than the execute_after_token_refresh would still be False
                        # and an exception will be raised