Skip to content
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

Support pull requests in personal spaces in Bitbucket Server #1406

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

vishwamartur
Copy link

@vishwamartur vishwamartur commented Dec 21, 2024

User description

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/.

PR Type

Enhancement


Description

  • Added support for Bitbucket Server pull request URLs in personal spaces (URLs containing /users/)
  • Enhanced URL parsing logic to handle both project-based (/projects/) and user-based (/users/) paths
  • Automatically adds ~ prefix to workspace slug for personal space URLs
  • Added test coverage to verify correct parsing of personal space URLs
  • Eliminates the need for manual URL modification workarounds

Changes walkthrough 📝

Relevant files
Enhancement
bitbucket_server_provider.py
Enhanced PR URL parsing for personal spaces                           

pr_agent/git_providers/bitbucket_server_provider.py

  • Added support for parsing Bitbucket Server PR URLs with /users/ path
  • Modified URL parsing logic to handle both /projects/ and /users/ paths
  • Added prefix ~ to workspace_slug when URL contains /users/
  • +15/-2   
    Tests
    test_bitbucket_provider.py
    Added tests for personal spaces URL parsing                           

    tests/unittest/test_bitbucket_provider.py

  • Added test case for parsing PR URLs with /users/ path
  • Verified correct handling of personal space URLs
  • +7/-0     

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Related to Codium-ai#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/`.
    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis ✅

    1148 - Fully compliant

    Compliant requirements:

    • Support for /users/ path in PR URLs
    • Automatic handling of personal space URLs without manual modification
    • Correct parsing of the specified URL format
    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🏅 Score: 95
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Error Handling

    Verify that the error messages are clear and helpful when invalid URLs are provided, especially for edge cases with malformed paths

            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")

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add validation to prevent processing of malformed URLs that contain conflicting path segments

    Add error handling for the case when both /projects and /users paths are present in
    the URL, as this would be an invalid URL format. Check that only one of them exists.

    pr_agent/git_providers/bitbucket_server_provider.py [403-414]

     try:
         projects_index = path_parts.index("projects")
     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")
    +if projects_index != -1 and users_index != -1:
    +    raise ValueError(f"Invalid URL format: URL cannot contain both 'projects' and 'users' paths")
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: This is a valuable security enhancement that prevents processing of malformed URLs. The current code doesn't handle the edge case where both paths exist, which could lead to unexpected behavior.

    8
    Improve array bounds checking to prevent potential index out of range errors

    After modifying path_parts using slice operations, verify that there are still
    enough elements left to safely access the required indices to prevent potential
    IndexError exceptions.

    pr_agent/git_providers/bitbucket_server_provider.py [416-421]

     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":
    +if len(path_parts) < 6:
    +    raise ValueError(f"The provided URL '{pr_url}' has an invalid format - missing required path segments")
    +if path_parts[2] != "repos" or path_parts[4] != "pull-requests":
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion improves error handling by separating length validation from path segment validation, making the code more robust against malformed URLs and providing clearer error messages.

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

    @mrT23
    Copy link
    Collaborator

    mrT23 commented Dec 22, 2024

    @vishwamartur i think that's a reasonable suggestion. To make sure that somehow there is no conflict

    other than that, looks good

    Add validation to prevent processing of malformed URLs that contain conflicting path segments

    Add error handling for the case when both /projects and /users paths are present in
    the URL, as this would be an invalid URL format. Check that only one of them exists.

    pr_agent/git_providers/bitbucket_server_provider.py [403-414]

     try:
         projects_index = path_parts.index("projects")
     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")
    +if projects_index != -1 and users_index != -1:
    +    raise ValueError(f"Invalid URL format: URL cannot contain both 'projects' and 'users' paths")```
    
        
      
    <ul class="contains-task-list">
    <li class="task-list-item"><span class="handle"></span><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> <strong>Apply this suggestion</strong> </li>
    </ul>
    <details><summary>Suggestion importance[1-10]: 8</summary>
    <p dir="auto">Why: This is a valuable security enhancement that prevents processing of malformed URLs. The current code doesn't handle the edge case where both paths exist, which could lead to unexpected behavior.</p>
    </details></details>

    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