From 4688b20284f13afb7645c79859d04e19faab30f5 Mon Sep 17 00:00:00 2001 From: Vishwanath Martur <64204611+vishwamartur@users.noreply.github.com> Date: Sun, 22 Dec 2024 00:42:11 +0530 Subject: [PATCH] Support pull requests in personal spaces in Bitbucket Server Related to #1148 Update `_parse_pr_url` method in `pr_agent/git_providers/bitbucket_server_provider.py` to handle URLs with `/users/`. * Add logic to check for both `/projects/` and `/users/` in the URL path and process them accordingly. * Modify the method to raise a `ValueError` if neither `/projects/` nor `/users/` is found in the URL. * Update the `workspace_slug` to include a `~` prefix if the URL contains `/users/`. Add test case for URL with `/users/` in `tests/unittest/test_bitbucket_provider.py`. * Ensure the new test case verifies the correct parsing of URLs with `/users/`. --- .../git_providers/bitbucket_server_provider.py | 17 +++++++++++++++-- tests/unittest/test_bitbucket_provider.py | 7 +++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/pr_agent/git_providers/bitbucket_server_provider.py b/pr_agent/git_providers/bitbucket_server_provider.py index 4dfa8226a..cbbb4a212 100644 --- a/pr_agent/git_providers/bitbucket_server_provider.py +++ b/pr_agent/git_providers/bitbucket_server_provider.py @@ -402,10 +402,21 @@ def _parse_pr_url(pr_url: str) -> Tuple[str, str, int]: try: projects_index = path_parts.index("projects") - except ValueError as e: + except ValueError: + projects_index = -1 + + try: + users_index = path_parts.index("users") + except ValueError: + users_index = -1 + + if projects_index == -1 and users_index == -1: raise ValueError(f"The provided URL '{pr_url}' does not appear to be a Bitbucket PR URL") - path_parts = path_parts[projects_index:] + if projects_index != -1: + path_parts = path_parts[projects_index:] + else: + path_parts = path_parts[users_index:] if len(path_parts) < 6 or path_parts[2] != "repos" or path_parts[4] != "pull-requests": raise ValueError( @@ -413,6 +424,8 @@ def _parse_pr_url(pr_url: str) -> Tuple[str, str, int]: ) workspace_slug = path_parts[1] + if users_index != -1: + workspace_slug = f"~{workspace_slug}" repo_slug = path_parts[3] try: pr_number = int(path_parts[5]) diff --git a/tests/unittest/test_bitbucket_provider.py b/tests/unittest/test_bitbucket_provider.py index 5c6729289..d883d55b0 100644 --- a/tests/unittest/test_bitbucket_provider.py +++ b/tests/unittest/test_bitbucket_provider.py @@ -24,6 +24,13 @@ def test_parse_pr_url(self): assert repo_slug == "my-repo" assert pr_number == 1 + def test_parse_pr_url_with_users(self): + url = "https://bitbucket.company-server.url/users/username/repos/my-repo/pull-requests/1" + workspace_slug, repo_slug, pr_number = BitbucketServerProvider._parse_pr_url(url) + assert workspace_slug == "~username" + assert repo_slug == "my-repo" + assert pr_number == 1 + def mock_get_content_of_file(self, project_key, repository_slug, filename, at=None, markup=None): content_map = { '9c1cffdd9f276074bfb6fb3b70fbee62d298b058': 'file\nwith\nsome\nlines\nto\nemulate\na\nreal\nfile\n',