Skip to content

Commit c6561d1

Browse files
committed
Render view_url_template in view_url
1 parent b9153f5 commit c6561d1

File tree

2 files changed

+96
-26
lines changed
  • providers/git

2 files changed

+96
-26
lines changed

providers/git/src/airflow/providers/git/bundles/git.py

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -219,33 +219,10 @@ def _convert_git_ssh_url_to_https(url: str) -> str:
219219
def view_url(self, version: str | None = None) -> str | None:
220220
if not version:
221221
return None
222-
url = self.repo_url
223-
if not url:
222+
template = self.view_url_template()
223+
if not template:
224224
return None
225-
if url.startswith("git@"):
226-
url = self._convert_git_ssh_url_to_https(url)
227-
if url.endswith(".git"):
228-
url = url[:-4]
229-
parsed_url = urlparse(url)
230-
host = parsed_url.hostname
231-
if not host:
232-
return None
233-
if parsed_url.username or parsed_url.password:
234-
new_netloc = host
235-
if parsed_url.port:
236-
new_netloc += f":{parsed_url.port}"
237-
url = parsed_url._replace(netloc=new_netloc).geturl()
238-
host_patterns = {
239-
"github.com": f"{url}/tree/{version}",
240-
"gitlab.com": f"{url}/-/tree/{version}",
241-
"bitbucket.org": f"{url}/src/{version}",
242-
}
243-
if self.subdir:
244-
host_patterns = {k: f"{v}/{self.subdir}" for k, v in host_patterns.items()}
245-
for allowed_host, template in host_patterns.items():
246-
if host == allowed_host or host.endswith(f".{allowed_host}"):
247-
return template
248-
return None
225+
return template.format(version=version)
249226

250227
def view_url_template(self) -> str | None:
251228
if self._view_url_template:

providers/git/tests/unit/git/bundles/test_git.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,99 @@ def test_view_url_subdir(
521521
assert view_url == expected_url
522522
bundle.initialize.assert_not_called()
523523

524+
@pytest.mark.parametrize(
525+
"repo_url, extra_conn_kwargs, expected_url",
526+
[
527+
(
528+
"git@github.com:apache/airflow.git",
529+
None,
530+
"https://github.com/apache/airflow/tree/{version}/subdir",
531+
),
532+
(
533+
"git@github.com:apache/airflow",
534+
None,
535+
"https://github.com/apache/airflow/tree/{version}/subdir",
536+
),
537+
(
538+
"https://github.com/apache/airflow",
539+
None,
540+
"https://github.com/apache/airflow/tree/{version}/subdir",
541+
),
542+
(
543+
"https://github.com/apache/airflow.git",
544+
None,
545+
"https://github.com/apache/airflow/tree/{version}/subdir",
546+
),
547+
(
548+
"git@gitlab.com:apache/airflow.git",
549+
None,
550+
"https://gitlab.com/apache/airflow/-/tree/{version}/subdir",
551+
),
552+
(
553+
"git@bitbucket.org:apache/airflow.git",
554+
None,
555+
"https://bitbucket.org/apache/airflow/src/{version}/subdir",
556+
),
557+
(
558+
"git@myorg.github.com:apache/airflow.git",
559+
None,
560+
"https://myorg.github.com/apache/airflow/tree/{version}/subdir",
561+
),
562+
(
563+
"https://myorg.github.com/apache/airflow.git",
564+
None,
565+
"https://myorg.github.com/apache/airflow/tree/{version}/subdir",
566+
),
567+
(
568+
"https://github.com/apache/airflow",
569+
{"password": "abc123"},
570+
"https://github.com/apache/airflow/tree/{version}/subdir",
571+
),
572+
(
573+
"https://github.com/apache/airflow",
574+
{"login": "abc123"},
575+
"https://github.com/apache/airflow/tree/{version}/subdir",
576+
),
577+
(
578+
"https://github.com/apache/airflow",
579+
{"login": "abc123", "password": "def456"},
580+
"https://github.com/apache/airflow/tree/{version}/subdir",
581+
),
582+
(
583+
"https://github.com:443/apache/airflow",
584+
None,
585+
"https://github.com:443/apache/airflow/tree/{version}/subdir",
586+
),
587+
(
588+
"https://github.com:443/apache/airflow",
589+
{"password": "abc123"},
590+
"https://github.com:443/apache/airflow/tree/{version}/subdir",
591+
),
592+
],
593+
)
594+
@mock.patch("airflow.providers.git.bundles.git.Repo")
595+
def test_view_url_template_subdir(
596+
self, mock_gitrepo, repo_url, extra_conn_kwargs, expected_url, create_connection_without_db
597+
):
598+
create_connection_without_db(
599+
Connection(
600+
conn_id="git_default",
601+
host=repo_url,
602+
conn_type="git",
603+
**(extra_conn_kwargs or {}),
604+
)
605+
)
606+
bundle = GitDagBundle(
607+
name="test",
608+
tracking_ref="main",
609+
subdir="subdir",
610+
git_conn_id="git_default",
611+
)
612+
bundle.initialize = mock.MagicMock()
613+
view_url_template = bundle.view_url_template()
614+
assert view_url_template == expected_url
615+
bundle.initialize.assert_not_called()
616+
524617
@mock.patch("airflow.providers.git.bundles.git.Repo")
525618
def test_view_url_returns_none_when_no_version_in_view_url(self, mock_gitrepo):
526619
bundle = GitDagBundle(

0 commit comments

Comments
 (0)