Skip to content

Commit 82a2c91

Browse files
authored
Validate Databricks Repos operators' template fields after rendering (#70341)
Validate Databricks Repos operators' template fields after rendering branch, tag and repo_path are template fields, rendered after __init__ runs. The Create, Update and Delete Repos operators enforced their mutual-exclusivity and presence combinations in the constructor, acting on the un-rendered Jinja expressions. Move those checks into execute(), preserving order; git_url/git_provider detection reads no template field and stays in __init__. related: #70296
1 parent d369e6f commit 82a2c91

3 files changed

Lines changed: 30 additions & 30 deletions

File tree

‎providers/databricks/src/airflow/providers/databricks/operators/databricks_repos.py‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ def __init__(
9797
else:
9898
self.git_provider = git_provider
9999
self.repo_path = repo_path
100-
if branch is not None and tag is not None:
101-
raise AirflowException("Only one of branch or tag should be provided, but not both")
102100
self.branch = branch
103101
self.tag = tag
104102

@@ -131,6 +129,8 @@ def execute(self, context: Context):
131129
:param context: context
132130
:return: Repo ID
133131
"""
132+
if self.branch is not None and self.tag is not None:
133+
raise AirflowException("Only one of branch or tag should be provided, but not both")
134134
payload = {
135135
"url": self.git_url,
136136
"provider": self.git_provider,
@@ -225,14 +225,6 @@ def __init__(
225225
self.databricks_conn_id = databricks_conn_id
226226
self.databricks_retry_limit = databricks_retry_limit
227227
self.databricks_retry_delay = databricks_retry_delay
228-
if branch is not None and tag is not None:
229-
raise AirflowException("Only one of branch or tag should be provided, but not both")
230-
if branch is None and tag is None:
231-
raise AirflowException("One of branch or tag should be provided")
232-
if repo_id is not None and repo_path is not None:
233-
raise AirflowException("Only one of repo_id or repo_path should be provided, but not both")
234-
if repo_id is None and repo_path is None:
235-
raise AirflowException("One of repo_id or repo_path should be provided")
236228
self.repo_path = repo_path
237229
self.repo_id = repo_id
238230
self.branch = branch
@@ -248,6 +240,14 @@ def _hook(self) -> DatabricksHook:
248240
)
249241

250242
def execute(self, context: Context):
243+
if self.branch is not None and self.tag is not None:
244+
raise AirflowException("Only one of branch or tag should be provided, but not both")
245+
if self.branch is None and self.tag is None:
246+
raise AirflowException("One of branch or tag should be provided")
247+
if self.repo_id is not None and self.repo_path is not None:
248+
raise AirflowException("Only one of repo_id or repo_path should be provided, but not both")
249+
if self.repo_id is None and self.repo_path is None:
250+
raise AirflowException("One of repo_id or repo_path should be provided")
251251
if self.repo_path is not None:
252252
self.repo_id = self._hook.get_repo_by_path(self.repo_path)
253253
if self.repo_id is None:
@@ -297,10 +297,6 @@ def __init__(
297297
self.databricks_conn_id = databricks_conn_id
298298
self.databricks_retry_limit = databricks_retry_limit
299299
self.databricks_retry_delay = databricks_retry_delay
300-
if repo_id is not None and repo_path is not None:
301-
raise AirflowException("Only one of repo_id or repo_path should be provided, but not both")
302-
if repo_id is None and repo_path is None:
303-
raise AirflowException("One of repo_id repo_path tag should be provided")
304300
self.repo_path = repo_path
305301
self.repo_id = repo_id
306302

@@ -314,6 +310,10 @@ def _hook(self) -> DatabricksHook:
314310
)
315311

316312
def execute(self, context: Context):
313+
if self.repo_id is not None and self.repo_path is not None:
314+
raise AirflowException("Only one of repo_id or repo_path should be provided, but not both")
315+
if self.repo_id is None and self.repo_path is None:
316+
raise AirflowException("One of repo_id repo_path tag should be provided")
317317
if self.repo_path is not None:
318318
self.repo_id = self._hook.get_repo_by_path(self.repo_path)
319319
if self.repo_id is None:

‎providers/databricks/tests/unit/databricks/operators/test_databricks_repos.py‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,26 @@ def test_update_with_path(self, db_mock_class):
7777
db_mock.update_repo.assert_called_once_with("123", {"tag": "v1.0.0"})
7878

7979
def test_init_exception(self):
80-
"""
81-
Tests handling of incorrect parameters passed to ``__init__``
82-
"""
80+
"""Incorrect parameter combinations are rejected at execute, after rendering."""
81+
op = DatabricksReposUpdateOperator(task_id=TASK_ID, repo_id="abc", repo_path="path", branch="abc")
8382
with pytest.raises(
8483
AirflowException, match="Only one of repo_id or repo_path should be provided, but not both"
8584
):
86-
DatabricksReposUpdateOperator(task_id=TASK_ID, repo_id="abc", repo_path="path", branch="abc")
85+
op.execute(None)
8786

87+
op = DatabricksReposUpdateOperator(task_id=TASK_ID, branch="abc")
8888
with pytest.raises(AirflowException, match="One of repo_id or repo_path should be provided"):
89-
DatabricksReposUpdateOperator(task_id=TASK_ID, branch="abc")
89+
op.execute(None)
9090

91+
op = DatabricksReposUpdateOperator(task_id=TASK_ID, repo_id="123", branch="123", tag="123")
9192
with pytest.raises(
9293
AirflowException, match="Only one of branch or tag should be provided, but not both"
9394
):
94-
DatabricksReposUpdateOperator(task_id=TASK_ID, repo_id="123", branch="123", tag="123")
95+
op.execute(None)
9596

97+
op = DatabricksReposUpdateOperator(task_id=TASK_ID, repo_id="123")
9698
with pytest.raises(AirflowException, match="One of branch or tag should be provided"):
97-
DatabricksReposUpdateOperator(task_id=TASK_ID, repo_id="123")
99+
op.execute(None)
98100

99101

100102
class TestDatabricksReposDeleteOperator:
@@ -140,16 +142,16 @@ def test_delete_with_path(self, db_mock_class):
140142
db_mock.delete_repo.assert_called_once_with("123")
141143

142144
def test_init_exception(self):
143-
"""
144-
Tests handling of incorrect parameters passed to ``__init__``
145-
"""
145+
"""Incorrect parameter combinations are rejected at execute, after rendering."""
146+
op = DatabricksReposDeleteOperator(task_id=TASK_ID, repo_id="abc", repo_path="path")
146147
with pytest.raises(
147148
AirflowException, match="Only one of repo_id or repo_path should be provided, but not both"
148149
):
149-
DatabricksReposDeleteOperator(task_id=TASK_ID, repo_id="abc", repo_path="path")
150+
op.execute(None)
150151

152+
op = DatabricksReposDeleteOperator(task_id=TASK_ID)
151153
with pytest.raises(AirflowException, match="One of repo_id repo_path tag should be provided"):
152-
DatabricksReposDeleteOperator(task_id=TASK_ID)
154+
op.execute(None)
153155

154156

155157
class TestDatabricksReposCreateOperator:
@@ -286,7 +288,8 @@ def test_init_exception(self):
286288
with pytest.raises(AirflowException, match=exception_message):
287289
op.execute(None)
288290

291+
op = DatabricksReposCreateOperator(task_id=TASK_ID, git_url=git_url, branch="123", tag="123")
289292
with pytest.raises(
290293
AirflowException, match="Only one of branch or tag should be provided, but not both"
291294
):
292-
DatabricksReposCreateOperator(task_id=TASK_ID, git_url=git_url, branch="123", tag="123")
295+
op.execute(None)

‎scripts/ci/prek/validate_operators_init_exemptions.txt‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ providers/amazon/src/airflow/providers/amazon/aws/transfers/s3_to_redshift.py::S
2323
providers/anthropic/src/airflow/providers/anthropic/operators/agent.py::AnthropicAgentSessionOperator
2424
providers/apache/kafka/src/airflow/providers/apache/kafka/operators/produce.py::ProduceToTopicOperator
2525
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/operators/pod.py::KubernetesPodOperator
26-
providers/databricks/src/airflow/providers/databricks/operators/databricks_repos.py::DatabricksReposCreateOperator
27-
providers/databricks/src/airflow/providers/databricks/operators/databricks_repos.py::DatabricksReposDeleteOperator
28-
providers/databricks/src/airflow/providers/databricks/operators/databricks_repos.py::DatabricksReposUpdateOperator
2926
providers/docker/src/airflow/providers/docker/operators/docker.py::DockerOperator
3027
providers/google/src/airflow/providers/google/cloud/operators/bigquery.py::BigQueryInsertJobOperator
3128
providers/google/src/airflow/providers/google/cloud/operators/cloud_batch.py::CloudBatchSubmitJobOperator

0 commit comments

Comments
 (0)