pierrejeambrun commented on code in PR #26457:
URL: https://github.com/apache/airflow/pull/26457#discussion_r983133792


##########
airflow/api_connexion/endpoints/task_instance_endpoint.py:
##########
@@ -545,3 +546,54 @@ 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_READ, permissions.RESOURCE_DAG),
+        (permissions.ACTION_CAN_EDIT, permissions.RESOURCE_DAG_RUN),
+    ],
+)
+@provide_session
+def set_task_instance_note(
+    *, dag_id: str, dag_run_id: str, task_id: str, session: Session = 
NEW_SESSION
+) -> APIResponse:
+    """Set the note for a dag run."""
+    try:
+        post_body = 
set_task_instance_note_form_schema.load(get_json_request_dict())
+        new_value_for_notes = post_body["notes"]
+        # Note: We can't add map_index to the url as subpaths can't start with 
dashes.
+        map_index = post_body["map_index"]
+    except ValidationError as err:
+        raise BadRequest(detail=str(err))
+
+    query = (
+        session.query(TI)
+        .filter(TI.dag_id == dag_id)
+        .filter(TI.run_id == dag_run_id)
+        .filter(TI.task_id == task_id)
+    )
+    if map_index == -1:
+        query = query.filter(or_(TI.map_index == -1, TI.map_index is None))
+    else:
+        query = query.filter(TI.map_index == map_index)
+
+    try:
+        ti: TI | None = query.one_or_none()
+    except MultipleResultsFound:
+        raise NotFound(
+            "Task instance not found", detail="Task instance is mapped, add 
the map_index value to the URL"
+        )
+    if ti is None:
+        error_message = f'Task Instance not found for dag_id={dag_id}, 
run_id={dag_run_id}, task_id={task_id}'
+        raise NotFound(error_message)
+
+    ti.notes = new_value_for_notes
+    session.commit()
+
+    if map_index == -1:
+        return get_task_instance(dag_id=dag_id, dag_run_id=dag_run_id, 
task_id=task_id)

Review Comment:
   I tested the line I a have put above and it worked. The difference is to 
pass a tuple to the dump call. (The task instance schema is different from what 
we may expect, in a sense that it does need more than just the TI for 
serialising).
   
   For your specific error, (the add_entity) change your result to a tuple, so 
you need to take that into account if you update your query like that, you 
don’t necessarily need that. For your particular error, you cannot do 
query.all().notes, because the return of query.all(), is not a TI anymore. 
(add_entity added the SlaMiss in the result set).
   
   
   ```python
       query = (
           session.query(TI)
           .filter(TI.dag_id == dag_id)
           .filter(TI.run_id == dag_run_id)
           .filter(TI.task_id == task_id)
       )
       if map_index == -1:
           query = query.filter(or_(TI.map_index == -1, TI.map_index is None))
       else:
           query = query.filter(TI.map_index == map_index)
   
       try:
           ti: TI | None = query.one_or_none()
       except MultipleResultsFound:
           raise NotFound(
               "Task instance not found", detail="Task instance is mapped, add 
the map_index value to the URL"
           )
       if ti is None:
           error_message = f'Task Instance not found for dag_id={dag_id}, 
run_id={dag_run_id}, task_id={task_id}'
           raise NotFound(error_message)
   
       ti.notes = new_value_for_notes
       session.commit()
   
       return task_instance_schema.dump((ti, None))
   ```
   



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