Merge pull request #1742 from twdkeule/feature/parse-azure-url

Azure devops: parse PR url starting from the end
This commit is contained in:
Tal
2025-05-08 19:00:32 +03:00
committed by GitHub
2 changed files with 21 additions and 15 deletions

View File

@ -543,22 +543,21 @@ class AzureDevopsProvider(GitProvider):
@staticmethod
def _parse_pr_url(pr_url: str) -> Tuple[str, str, int]:
parsed_url = urlparse(pr_url)
path_parts = parsed_url.path.strip("/").split("/")
if "pullrequest" not in path_parts:
raise ValueError(
"The provided URL does not appear to be a Azure DevOps PR URL"
)
if len(path_parts) == 6: # "https://dev.azure.com/organization/project/_git/repo/pullrequest/1"
workspace_slug = path_parts[1]
repo_slug = path_parts[3]
pr_number = int(path_parts[5])
elif len(path_parts) == 5: # 'https://organization.visualstudio.com/project/_git/repo/pullrequest/1'
workspace_slug = path_parts[0]
repo_slug = path_parts[2]
pr_number = int(path_parts[4])
else:
raise ValueError("The provided URL does not appear to be a Azure DevOps PR URL")
num_parts = len(path_parts)
if num_parts < 5:
raise ValueError("The provided URL has insufficient path components for an Azure DevOps PR URL")
# Verify that the second-to-last path component is "pullrequest"
if path_parts[num_parts - 2] != "pullrequest":
raise ValueError("The provided URL does not follow the expected Azure DevOps PR URL format")
workspace_slug = path_parts[num_parts - 5]
repo_slug = path_parts[num_parts - 3]
try:
pr_number = int(path_parts[num_parts - 1])
except ValueError as e:
raise ValueError("Cannot parse PR number in the provided URL") from e
return workspace_slug, repo_slug, pr_number

View File

@ -13,3 +13,10 @@ class TestAzureDevOpsParsing():
# workspace_slug, repo_slug, pr_number
assert AzureDevopsProvider._parse_pr_url(pr_url) == ("project", "repo", 1)
def test_self_hosted_address(self):
pr_url = "http://server.be:8080/tfs/department/project/_git/repo/pullrequest/1"
# workspace_slug, repo_slug, pr_number
assert AzureDevopsProvider._parse_pr_url(pr_url) == ("project", "repo", 1)