From 64db9d85c3e8a05c74541a5a1b01a56b77ae49ca Mon Sep 17 00:00:00 2001
From: fritz-astronomer <fritz@astronomer.io>
Date: Wed, 8 Jun 2022 12:16:49 -0400
Subject: [PATCH 1/3] fix log_url to include map_index; happy path test fix

---
 airflow/models/taskinstance.py    |  5 ++++-
 tests/models/test_taskinstance.py | 15 ++-------------
 2 files changed, 6 insertions(+), 14 deletions(-)

diff --git a/airflow/models/taskinstance.py b/airflow/models/taskinstance.py
index 0f5d49b819762..83fb319953d4a 100644
--- a/airflow/models/taskinstance.py
+++ b/airflow/models/taskinstance.py
@@ -771,7 +771,10 @@ def log_url(self):
         """Log URL for TaskInstance"""
         iso = quote(self.execution_date.isoformat())
         base_url = conf.get('webserver', 'BASE_URL')
-        return base_url + f"/log?execution_date={iso}&task_id={self.task_id}&dag_id={self.dag_id}"
+        return base_url + f"/log?execution_date={iso}" \
+                          f"&task_id={self.task_id}" \
+                          f"&dag_id={self.dag_id}" \
+                          f"&map_index={self.map_index}"
 
     @property
     def mark_success_url(self):
diff --git a/tests/models/test_taskinstance.py b/tests/models/test_taskinstance.py
index a1d180fa1ee56..e4ecb2c2af9fd 100644
--- a/tests/models/test_taskinstance.py
+++ b/tests/models/test_taskinstance.py
@@ -1307,26 +1307,15 @@ def test_get_num_running_task_instances(self, create_task_instance):
         assert 1 == ti2.get_num_running_task_instances(session=session)
         assert 1 == ti3.get_num_running_task_instances(session=session)
 
-    # def test_log_url(self):
-    #     now = pendulum.now('Europe/Brussels')
-    #     dag = DAG('dag', start_date=DEFAULT_DATE)
-    #     task = EmptyOperator(task_id='op', dag=dag)
-    #     ti = TI(task=task, execution_date=now)
-    #     d = urllib.parse.parse_qs(
-    #         urllib.parse.urlparse(ti.log_url).query,
-    #         keep_blank_values=True, strict_parsing=True)
-    #     self.assertEqual(d['dag_id'][0], 'dag')
-    #     self.assertEqual(d['task_id'][0], 'op')
-    #     self.assertEqual(pendulum.parse(d['execution_date'][0]), now)
-
     def test_log_url(self, create_task_instance):
-        ti = create_task_instance(dag_id='dag', task_id='op', execution_date=timezone.datetime(2018, 1, 1))
+        ti = create_task_instance(dag_id='dag', task_id='op', execution_date=timezone.datetime(2018, 1, 1), )
 
         expected_url = (
             'http://localhost:8080/log?'
             'execution_date=2018-01-01T00%3A00%3A00%2B00%3A00'
             '&task_id=op'
             '&dag_id=dag'
+            '&map_index=-1'
         )
         assert ti.log_url == expected_url
 

From 04d98064846e6c21d229a4b33f33388057472a77 Mon Sep 17 00:00:00 2001
From: fritz-astronomer <fritz@astronomer.io>
Date: Thu, 9 Jun 2022 14:50:36 -0400
Subject: [PATCH 2/3] fix static check formatting

---
 airflow/models/taskinstance.py    | 11 +++++++----
 tests/models/test_taskinstance.py |  6 +++++-
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/airflow/models/taskinstance.py b/airflow/models/taskinstance.py
index 83fb319953d4a..7913cff7ff7c5 100644
--- a/airflow/models/taskinstance.py
+++ b/airflow/models/taskinstance.py
@@ -771,10 +771,13 @@ def log_url(self):
         """Log URL for TaskInstance"""
         iso = quote(self.execution_date.isoformat())
         base_url = conf.get('webserver', 'BASE_URL')
-        return base_url + f"/log?execution_date={iso}" \
-                          f"&task_id={self.task_id}" \
-                          f"&dag_id={self.dag_id}" \
-                          f"&map_index={self.map_index}"
+        return (
+            f"{base_url}/log"
+            f"?execution_date={iso}"
+            f"&task_id={self.task_id}"
+            f"&dag_id={self.dag_id}"
+            f"&map_index={self.map_index}"
+        )
 
     @property
     def mark_success_url(self):
diff --git a/tests/models/test_taskinstance.py b/tests/models/test_taskinstance.py
index e4ecb2c2af9fd..51d9f07100f99 100644
--- a/tests/models/test_taskinstance.py
+++ b/tests/models/test_taskinstance.py
@@ -1308,7 +1308,11 @@ def test_get_num_running_task_instances(self, create_task_instance):
         assert 1 == ti3.get_num_running_task_instances(session=session)
 
     def test_log_url(self, create_task_instance):
-        ti = create_task_instance(dag_id='dag', task_id='op', execution_date=timezone.datetime(2018, 1, 1), )
+        ti = create_task_instance(
+            dag_id='dag',
+            task_id='op',
+            execution_date=timezone.datetime(2018, 1, 1)
+        )
 
         expected_url = (
             'http://localhost:8080/log?'

From 09749088b66f9267a3e4b49034db87a6774cef44 Mon Sep 17 00:00:00 2001
From: fritz-astronomer <fritz@astronomer.io>
Date: Thu, 9 Jun 2022 18:20:51 -0400
Subject: [PATCH 3/3] fix static check formatting

---
 tests/models/test_taskinstance.py | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/tests/models/test_taskinstance.py b/tests/models/test_taskinstance.py
index 51d9f07100f99..da3d138306235 100644
--- a/tests/models/test_taskinstance.py
+++ b/tests/models/test_taskinstance.py
@@ -1308,11 +1308,7 @@ def test_get_num_running_task_instances(self, create_task_instance):
         assert 1 == ti3.get_num_running_task_instances(session=session)
 
     def test_log_url(self, create_task_instance):
-        ti = create_task_instance(
-            dag_id='dag',
-            task_id='op',
-            execution_date=timezone.datetime(2018, 1, 1)
-        )
+        ti = create_task_instance(dag_id='dag', task_id='op', execution_date=timezone.datetime(2018, 1, 1))
 
         expected_url = (
             'http://localhost:8080/log?'
