uranusjr commented on code in PR #26165:
URL: https://github.com/apache/airflow/pull/26165#discussion_r996552621


##########
airflow/api_connexion/endpoints/task_instance_endpoint.py:
##########
@@ -545,3 +547,102 @@ def post_set_task_instances_state(*, dag_id: str, 
session: Session = NEW_SESSION
         session=session,
     )
     return 
task_instance_reference_collection_schema.dump(TaskInstanceReferenceCollection(task_instances=tis))
+
+
[email protected]_access(
+    [
+        (permissions.ACTION_CAN_EDIT, permissions.RESOURCE_DAG),
+        (permissions.ACTION_CAN_READ, permissions.RESOURCE_DAG_RUN),
+        (permissions.ACTION_CAN_EDIT, permissions.RESOURCE_TASK_INSTANCE),
+    ],
+)
+@provide_session
+def patch_mapped_task_instance(
+    *, dag_id: str, dag_run_id: str, task_id: str, map_index: int, session: 
Session = NEW_SESSION
+) -> APIResponse:
+    """Update the state of a mapped task instance."""
+    body = get_json_request_dict()
+    try:
+        data = set_single_task_instance_state_form.load(body)
+    except ValidationError as err:
+        raise BadRequest(detail=str(err.messages))
+
+    error_message = f"Dag ID {dag_id} not found"
+    dag = get_airflow_app().dag_bag.get_dag(dag_id)
+    if not dag:
+        raise NotFound(error_message)
+
+    if not dag.has_task(task_id):
+        error_message = f"Task ID {task_id} not found"
+        raise NotFound(error_message)
+
+    ti: TI = session.query(TI).get(
+        {'task_id': task_id, 'dag_id': dag_id, 'run_id': dag_run_id, 
'map_index': map_index}
+    )
+
+    if not ti:
+        error_message = (
+            f"Mapped task instance not found for task {task_id!r} on DAG run 
with ID {dag_run_id!r}"
+        )
+        raise NotFound(detail=error_message)
+
+    if not data["dry_run"]:
+        ti = dag.set_task_instance_state(
+            task_id=task_id,
+            run_id=dag_run_id,
+            map_indexes=[map_index],
+            state=data["new_state"],
+            commit=not data["dry_run"],
+            session=session,
+        )

Review Comment:
   ```suggestion
           ti = dag.set_task_instance_state(
               task_id=task_id,
               run_id=dag_run_id,
               map_indexes=[map_index],
               state=data["new_state"],
               commit=True,
               session=session,
           )
   ```



##########
airflow/api_connexion/endpoints/task_instance_endpoint.py:
##########
@@ -545,3 +547,102 @@ def post_set_task_instances_state(*, dag_id: str, 
session: Session = NEW_SESSION
         session=session,
     )
     return 
task_instance_reference_collection_schema.dump(TaskInstanceReferenceCollection(task_instances=tis))
+
+
[email protected]_access(
+    [
+        (permissions.ACTION_CAN_EDIT, permissions.RESOURCE_DAG),
+        (permissions.ACTION_CAN_READ, permissions.RESOURCE_DAG_RUN),
+        (permissions.ACTION_CAN_EDIT, permissions.RESOURCE_TASK_INSTANCE),
+    ],
+)
+@provide_session
+def patch_mapped_task_instance(
+    *, dag_id: str, dag_run_id: str, task_id: str, map_index: int, session: 
Session = NEW_SESSION
+) -> APIResponse:
+    """Update the state of a mapped task instance."""
+    body = get_json_request_dict()
+    try:
+        data = set_single_task_instance_state_form.load(body)
+    except ValidationError as err:
+        raise BadRequest(detail=str(err.messages))
+
+    error_message = f"Dag ID {dag_id} not found"
+    dag = get_airflow_app().dag_bag.get_dag(dag_id)
+    if not dag:
+        raise NotFound(error_message)

Review Comment:
   ```suggestion
       dag = get_airflow_app().dag_bag.get_dag(dag_id)
       if not dag:
           raise NotFound("DAG not found", detail=f"DAG {dag_id!r} not found")
   ```



##########
airflow/api_connexion/endpoints/task_instance_endpoint.py:
##########
@@ -545,3 +547,102 @@ def post_set_task_instances_state(*, dag_id: str, 
session: Session = NEW_SESSION
         session=session,
     )
     return 
task_instance_reference_collection_schema.dump(TaskInstanceReferenceCollection(task_instances=tis))
+
+
[email protected]_access(
+    [
+        (permissions.ACTION_CAN_EDIT, permissions.RESOURCE_DAG),
+        (permissions.ACTION_CAN_READ, permissions.RESOURCE_DAG_RUN),
+        (permissions.ACTION_CAN_EDIT, permissions.RESOURCE_TASK_INSTANCE),
+    ],
+)
+@provide_session
+def patch_mapped_task_instance(
+    *, dag_id: str, dag_run_id: str, task_id: str, map_index: int, session: 
Session = NEW_SESSION
+) -> APIResponse:
+    """Update the state of a mapped task instance."""
+    body = get_json_request_dict()
+    try:
+        data = set_single_task_instance_state_form.load(body)
+    except ValidationError as err:
+        raise BadRequest(detail=str(err.messages))
+
+    error_message = f"Dag ID {dag_id} not found"
+    dag = get_airflow_app().dag_bag.get_dag(dag_id)
+    if not dag:
+        raise NotFound(error_message)
+
+    if not dag.has_task(task_id):
+        error_message = f"Task ID {task_id} not found"
+        raise NotFound(error_message)
+
+    ti: TI = session.query(TI).get(
+        {'task_id': task_id, 'dag_id': dag_id, 'run_id': dag_run_id, 
'map_index': map_index}
+    )

Review Comment:
   ```suggestion
       ti: TI | None = session.query(TI).get(
           {'task_id': task_id, 'dag_id': dag_id, 'run_id': dag_run_id, 
'map_index': map_index}
       )
   ```



##########
airflow/api_connexion/endpoints/task_instance_endpoint.py:
##########
@@ -545,3 +547,102 @@ def post_set_task_instances_state(*, dag_id: str, 
session: Session = NEW_SESSION
         session=session,
     )
     return 
task_instance_reference_collection_schema.dump(TaskInstanceReferenceCollection(task_instances=tis))
+
+
[email protected]_access(
+    [
+        (permissions.ACTION_CAN_EDIT, permissions.RESOURCE_DAG),
+        (permissions.ACTION_CAN_READ, permissions.RESOURCE_DAG_RUN),
+        (permissions.ACTION_CAN_EDIT, permissions.RESOURCE_TASK_INSTANCE),
+    ],
+)
+@provide_session
+def patch_mapped_task_instance(
+    *, dag_id: str, dag_run_id: str, task_id: str, map_index: int, session: 
Session = NEW_SESSION
+) -> APIResponse:
+    """Update the state of a mapped task instance."""
+    body = get_json_request_dict()
+    try:
+        data = set_single_task_instance_state_form.load(body)
+    except ValidationError as err:
+        raise BadRequest(detail=str(err.messages))
+
+    error_message = f"Dag ID {dag_id} not found"
+    dag = get_airflow_app().dag_bag.get_dag(dag_id)
+    if not dag:
+        raise NotFound(error_message)
+
+    if not dag.has_task(task_id):
+        error_message = f"Task ID {task_id} not found"
+        raise NotFound(error_message)

Review Comment:
   ```suggestion
       if not dag.has_task(task_id):
           raise NotFound("Task not found", detail=f"Task {task_id!r} not found 
in DAG {dag_id!r}")
   ```



##########
airflow/api_connexion/endpoints/task_instance_endpoint.py:
##########
@@ -545,3 +547,102 @@ def post_set_task_instances_state(*, dag_id: str, 
session: Session = NEW_SESSION
         session=session,
     )
     return 
task_instance_reference_collection_schema.dump(TaskInstanceReferenceCollection(task_instances=tis))
+
+
[email protected]_access(
+    [
+        (permissions.ACTION_CAN_EDIT, permissions.RESOURCE_DAG),
+        (permissions.ACTION_CAN_READ, permissions.RESOURCE_DAG_RUN),
+        (permissions.ACTION_CAN_EDIT, permissions.RESOURCE_TASK_INSTANCE),
+    ],
+)
+@provide_session
+def patch_mapped_task_instance(
+    *, dag_id: str, dag_run_id: str, task_id: str, map_index: int, session: 
Session = NEW_SESSION
+) -> APIResponse:
+    """Update the state of a mapped task instance."""
+    body = get_json_request_dict()
+    try:
+        data = set_single_task_instance_state_form.load(body)
+    except ValidationError as err:
+        raise BadRequest(detail=str(err.messages))
+
+    error_message = f"Dag ID {dag_id} not found"
+    dag = get_airflow_app().dag_bag.get_dag(dag_id)
+    if not dag:
+        raise NotFound(error_message)
+
+    if not dag.has_task(task_id):
+        error_message = f"Task ID {task_id} not found"
+        raise NotFound(error_message)
+
+    ti: TI = session.query(TI).get(
+        {'task_id': task_id, 'dag_id': dag_id, 'run_id': dag_run_id, 
'map_index': map_index}
+    )
+
+    if not ti:
+        error_message = (
+            f"Mapped task instance not found for task {task_id!r} on DAG run 
with ID {dag_run_id!r}"
+        )
+        raise NotFound(detail=error_message)
+
+    if not data["dry_run"]:
+        ti = dag.set_task_instance_state(
+            task_id=task_id,
+            run_id=dag_run_id,
+            map_indexes=[map_index],
+            state=data["new_state"],
+            commit=not data["dry_run"],
+            session=session,
+        )
+
+    return task_instance_reference_schema.dump(ti)
+
+
[email protected]_access(
+    [
+        (permissions.ACTION_CAN_EDIT, permissions.RESOURCE_DAG),
+        (permissions.ACTION_CAN_READ, permissions.RESOURCE_DAG_RUN),
+        (permissions.ACTION_CAN_EDIT, permissions.RESOURCE_TASK_INSTANCE),
+    ],
+)
+@provide_session
+def patch_task_instance(
+    *, dag_id: str, dag_run_id: str, task_id: str, session: Session = 
NEW_SESSION
+) -> APIResponse:

Review Comment:
   This and `patch_mapped_task_instance` are too similar that they can simply 
be one single endpoint. I also wonder if we should have an endpoint (either 
this one directly or a separate one) to clear _all task instances of one mapped 
task_.
   
   (Also the two endpoint declarations in OpenAPI can also be de-duplicated a 
bit with components.)



-- 
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