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