Skip to content

Azure devops: parse PR url starting from the end #1742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2025

Conversation

twdkeule
Copy link
Contributor

@twdkeule twdkeule commented May 6, 2025

User description

My company uses a self-hosted AzureDevops server.
The path has more parts than the public Azure PR urls: http://server:1234/tfs/$department/$project/_git/$repo/pullrequest/68407
I modified the url parser to count the parts from the end of the url, this way it works for any prefix.


PR Type

Bug fix, Enhancement


Description

  • Improved Azure DevOps PR URL parsing for flexible path structures

  • Replaced fixed-length parsing with end-based indexing for robustness

  • Enhanced error handling for malformed or unexpected URLs


Changes walkthrough 📝

Relevant files
Enhancement
azuredevops_provider.py
Flexible and robust Azure DevOps PR URL parsing                   

pr_agent/git_providers/azuredevops_provider.py

  • Replaced fixed-length path parsing with end-based indexing for PR URLs
  • Improved compatibility with self-hosted and custom Azure DevOps server
    URLs
  • Enhanced error handling for invalid or unexpected URL formats
  • +7/-10   

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Copy link
    Contributor

    qodo-merge-pro-for-open-source bot commented May 6, 2025

    PR Reviewer Guide 🔍

    (Review updated until commit df1d859)

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Potential Bug

    The workspace_slug extraction may be incorrect. The code assumes workspace_slug is at index num_parts-5, but based on the test cases and PR description, it should be extracting the project name, not the department/organization.

    workspace_slug = path_parts[num_parts - 5]
    repo_slug = path_parts[num_parts - 3]
    Error Handling

    The code should check if num_parts is at least 5 before trying to access path_parts[num_parts-5], otherwise it might cause an index error for shorter URLs.

    workspace_slug = path_parts[num_parts - 5]
    repo_slug = path_parts[num_parts - 3]

    Copy link
    Contributor

    qodo-merge-pro-for-open-source bot commented May 6, 2025

    PR Code Suggestions ✨

    Latest suggestions up to ed77b6a

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Improve URL validation

    The code should verify that the path part before the PR number is actually
    "pullrequest" to ensure proper URL validation, rather than just checking if the
    string exists anywhere in the path.

    pr_agent/git_providers/azuredevops_provider.py [548-553]

     if "pullrequest" not in path_parts:
         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")
    +
    • Apply / Chat
    Suggestion importance[1-10]: 8

    __

    Why: The suggestion significantly improves the robustness of URL parsing by adding a specific check that path_parts[num_parts - 2] is indeed "pullrequest". The PR's new parsing logic relies on fixed offsets from the end of the URL path, making this explicit validation of a key structural element (pullrequest's position) crucial for correctness and preventing misinterpretation of malformed URLs. This is a strong enhancement to input validation.

    Medium
    Fix URL parsing logic

    The current approach assumes a fixed position for workspace_slug and repo_slug
    relative to the end of the URL, which may not be correct for all Azure DevOps
    URL formats. This could lead to incorrect parsing of repository and organization
    information.

    pr_agent/git_providers/azuredevops_provider.py [555-556]

    -workspace_slug = path_parts[num_parts - 5]
    -repo_slug = path_parts[num_parts - 3]
    +# For URLs like "https://dev.azure.com/organization/project/_git/repo/pullrequest/1"
    +if "dev.azure.com" in parsed_url.netloc:
    +    workspace_slug = path_parts[1]
    +    repo_slug = path_parts[3]
    +# For URLs like 'https://organization.visualstudio.com/project/_git/repo/pullrequest/1'
    +else:
    +    workspace_slug = parsed_url.netloc.split('.')[0]
    +    repo_slug = path_parts[2]
    • Apply / Chat
    Suggestion importance[1-10]: 2

    __

    Why: The PR correctly refactors the existing logic for extracting workspace_slug (as ProjectName) and repo_slug. The suggestion proposes changing the source of workspace_slug for visualstudio.com URLs to be OrganizationName from the hostname, while keeping it as ProjectName for dev.azure.com URLs. This introduces an inconsistency and alters the original behavior, rather than fixing a flaw in the PR's refactoring itself. The improved_code is not demonstrably superior or more correct in general.

    Low
    • Update
    • Author self-review: I have reviewed the PR code suggestions, and addressed the relevant ones.

    Previous suggestions

    Suggestions up to commit be68370
    CategorySuggestion                                                                                                                                    Impact
    Learned
    best practice
    Add validation for array length

    Add a check to verify that path_parts has at least 5 elements before attempting
    to access indices. This prevents IndexError when the URL format is unexpected
    and provides a more specific error message.

    pr_agent/git_providers/azuredevops_provider.py [552-558]

     try:
         numParts = len(path_parts)
    +    if numParts < 5:
    +        raise ValueError("URL has insufficient path components for an Azure DevOps PR URL")
         workspace_slug = path_parts[numParts - 5]
         repo_slug = path_parts[numParts - 3]
         pr_number = int(path_parts[numParts - 1])
     except (ValueError, IndexError) as e:
         raise ValueError("The provided URL does not appear to be a Azure DevOps PR URL") from e
    Suggestion importance[1-10]: 6

    __

    Why:
    Relevant best practice - Add null safety checks before performing operations on potentially null or undefined values to prevent runtime errors

    Low

    @twdkeule twdkeule force-pushed the feature/parse-azure-url branch from be68370 to ed77b6a Compare May 6, 2025 12:48
    @twdkeule
    Copy link
    Contributor Author

    twdkeule commented May 6, 2025

    /improve

    @twdkeule twdkeule force-pushed the feature/parse-azure-url branch from ed77b6a to df1d859 Compare May 6, 2025 13:02
    @twdkeule
    Copy link
    Contributor Author

    twdkeule commented May 6, 2025

    /review

    Copy link
    Contributor

    Persistent review updated to latest commit df1d859

    @mrT23
    Copy link
    Collaborator

    mrT23 commented May 8, 2025

    cool, thanks

    @mrT23 mrT23 merged commit 0c21d4a into qodo-ai:main May 8, 2025
    @twdkeule twdkeule deleted the feature/parse-azure-url branch May 9, 2025 09:39
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants