vincbeck commented on code in PR #74570:
URL: https://github.com/apache/airflow/pull/74570#discussion_r4591329633
##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/import_error.py:
##########
@@ -297,18 +297,23 @@ def get_import_errors(
import_errors_result: Iterable[tuple[ParseImportError, Iterable]] =
groupby(
session.execute(import_errors_select), itemgetter(0)
)
+ errors_with_dag_ids = [
+ (import_error, [dag_id for _, dag_id in file_dag_ids_iter if dag_id is
not None])
+ for import_error, file_dag_ids_iter in import_errors_result
+ ]
+ page_dag_ids = list({dag_id for _, dag_ids in errors_with_dag_ids for
dag_id in dag_ids})
+ dag_id_to_team = (
+ DagModel.get_dag_id_to_team_name_mapping(page_dag_ids,
session=session) if page_dag_ids else {}
Review Comment:
Why not do the same as `get_import_error` and check
`set(dag_ids).issubset(readable_dag_ids)`? `readable_dag_ids` is already
computed at the top of the function, so we would not need the team lookup nor
the `batch_is_authorized_dag` call per file at all.
##########
airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_import_error.py:
##########
@@ -877,6 +877,41 @@ def test_user_can_not_read_all_dags_in_file(
user=mock.ANY,
)
+ @mock.patch.object(DagModel, "get_dag_id_to_team_name_mapping")
+
@mock.patch("airflow.api_fastapi.core_api.routes.public.import_error.get_auth_manager")
+ def test_team_names_are_resolved_once_per_page(
+ self,
+ mock_get_auth_manager,
+ mock_get_dag_id_to_team_name_mapping,
+ test_client,
+ permitted_dag_model_all,
+ ):
+ mock_get_dag_id_to_team_name_mapping.return_value = {
+ "dag_id1": "team_a",
+ "dag_id2": "team_b",
+ "dag_id3": None,
+ }
+ set_mock_auth_manager__get_authorized_dag_ids(mock_get_auth_manager,
permitted_dag_model_all)
+ mock_batch_is_authorized_dag =
set_mock_auth_manager__batch_is_authorized_dag(
+ mock_get_auth_manager, True
+ )
+
+ response = test_client.get("/importErrors")
+
+ assert response.status_code == 200
+ assert response.json()["total_entries"] == 3
+ mock_get_dag_id_to_team_name_mapping.assert_called_once()
+ assert sorted(mock_get_dag_id_to_team_name_mapping.call_args.args[0])
== [
+ "dag_id1",
+ "dag_id2",
+ "dag_id3",
+ ]
+ assert sorted(
Review Comment:
nit: this flattens every call, so it would still pass if all the files were
authorized in one single `batch_is_authorized_dag` call. Can you also check
`call_count == 3` so we know each file is still authorized on its own?
--
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]