Skip to content

Conversation

MarkRx
Copy link
Contributor

@MarkRx MarkRx commented May 30, 2024

User description

Seems this code hasn't been maintained. Contains the following fixes to minimally get it up and running:

  • Fix missing import
  • Fix missing attribute error when PR description is empty
  • Fix diff using incorrect to/from
  • Fix PR id retrieval
  • Fix some errors not being propagated
  • Fix description publishing not using correct url

Tested with BB Datacenter ("server" rebranded) v8.9.9


PR Type

Bug fix


Description

This PR includes several bug fixes and improvements for the Bitbucket Server provider:

  • Added missing import for quote_plus.
  • Introduced get_pr_id method to retrieve PR ID.
  • Corrected base and head SHA retrieval for diffs.
  • Ensured errors are raised for failed inline comment publishing.
  • Fixed URL generation for relevant line links.
  • Improved handling of inline comments, including multi-line and single-line comments.
  • Added check for PR description attribute existence.
  • Updated publish_description to use Bitbucket client and include reviewers.

Changes walkthrough 📝

Relevant files
Bug fix
bitbucket_server_provider.py
Multiple bug fixes and improvements for Bitbucket Server provider.

pr_agent/git_providers/bitbucket_server_provider.py

  • Added missing import for quote_plus.
  • Introduced get_pr_id method to retrieve PR ID.
  • Corrected base and head SHA retrieval for diffs.
  • Ensured errors are raised for failed inline comment publishing.
  • Fixed URL generation for relevant line links.
  • Improved handling of inline comments, including multi-line and
    single-line comments.
  • Added check for PR description attribute existence.
  • Updated publish_description to use Bitbucket client and include
    reviewers.
  • +29/-20 

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Copy link
    Contributor

    PR Review 🔍

    ⏱️ Estimated effort to review [1-5]

    3, because the PR involves multiple bug fixes and improvements across various methods in a single file. The changes are moderate in number and complexity, requiring a thorough review to ensure that each fix is correctly implemented and does not introduce new issues.

    🏅 Score

    85

    🧪 Relevant tests

    No

    ⚡ Possible issues

    Possible Bug: The method get_pr_description_full uses hasattr(object, "description") which should likely be hasattr(self.pr, "description"). Using object is incorrect and will not check the attribute on the pr instance.

    Error Handling: The method publish_inline_comments logs an error but does not halt execution or handle the error beyond logging. This might lead to silent failures in inline comment publishing.

    🔒 Security concerns

    No

    @@ -242,7 +239,7 @@ def publish_inline_comment(self, comment: str, from_line: int, file: str):
    }

    response = requests.post(url=self._get_pr_comments_url(""), json=payload, headers=self.headers)
    return response
    response.raise_for_status()
    Copy link
    Contributor Author

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    return isn't used and is inconsistent with other providers so I removed it

    "reviewers": self.pr.reviewers # needs to be sent otherwise gets wiped
    }

    self.bitbucket_client.update_pull_request(self.workspace_slug, self.repo_slug, str(self.pr_num), payload)
    Copy link
    Contributor Author

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    return isn't used and is inconsistent with other providers so I removed it

    Copy link
    Contributor

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Correct the attribute check to use the self.pr object instead of the object class

    The get_pr_description_full method should check if the self.pr object has the description
    attribute, not the object class.

    pr_agent/git_providers/bitbucket_server_provider.py [286-289]

    -if hasattr(object, "description"):
    +if hasattr(self.pr, "description"):
         return self.pr.description
     else:
         return None
     
    Suggestion importance[1-10]: 10

    Why: The suggestion correctly identifies a bug where the wrong object was checked for the attribute, which could lead to incorrect behavior. Fixing this is crucial for the method's functionality.

    10
    Best practice
    Add exception handling to the publish_description method to ensure robustness

    The publish_description method should handle potential exceptions when updating the pull
    request to ensure robustness.

    pr_agent/git_providers/bitbucket_server_provider.py [352]

    -self.bitbucket_client.update_pull_request(self.workspace_slug, self.repo_slug, str(self.pr_num), payload)
    +try:
    +    self.bitbucket_client.update_pull_request(self.workspace_slug, self.repo_slug, str(self.pr_num), payload)
    +except Exception as e:
    +    get_logger().error(f"Failed to update pull request: {e}")
     
    Suggestion importance[1-10]: 8

    Why: Adding exception handling is important for robustness, especially in network operations which are prone to failures. This suggestion enhances the reliability of the method.

    8
    Possible issue
    Ensure self.pr_url is not None before generating the link to avoid potential errors

    In the generate_link_to_relevant_line_number method, handle the case where self.pr_url
    might be None to avoid potential errors.

    pr_agent/git_providers/bitbucket_server_provider.py [255-257]

    -if absolute_position != -1 and self.pr_url:
    -    link = f"{self.pr_url}/diff#{quote_plus(relevant_file)}?t={absolute_position}"
    -    return link
    +if absolute_position != -1:
    +    if self.pr_url:
    +        link = f"{self.pr_url}/diff#{quote_plus(relevant_file)}?t={absolute_position}"
    +        return link
    +    else:
    +        get_logger().error("PR URL is not set.")
     
    Suggestion importance[1-10]: 7

    Why: This suggestion addresses a potential issue where self.pr_url could be None, leading to an error when constructing the link. It's a good preventive measure.

    7
    Enhancement
    Log the comment details when an inline comment cannot be published to aid in debugging

    In the publish_inline_comments method, consider logging the comment details when an inline
    comment cannot be published to aid in debugging.

    pr_agent/git_providers/bitbucket_server_provider.py [274]

    -get_logger().error(f"Could not publish inline comment {comment}")
    +get_logger().error(f"Could not publish inline comment: {comment}")
     
    Suggestion importance[1-10]: 6

    Why: Enhancing the error message provides better context for debugging, which is helpful but not critical. The improvement is minor but useful for maintenance.

    6

    @mrT23
    Copy link
    Collaborator

    mrT23 commented May 30, 2024

    thanks @MarkRx
    Bitbucket server support is a contribution from the community, and we rely on the community to maintain it.

    have you reviewed the code suggestions ?
    This seems like a bug, for example:

    Correct the attribute check to use the self.pr object instead of the object class

    The get_pr_description_full method should check if the self.pr object has the description
    attribute, not the object class.

    pr_agent/git_providers/bitbucket_server_provider.py [286-289]

    -if hasattr(object, "description"):
    +if hasattr(self.pr, "description"):
         return self.pr.description
     else:
         return None
    Suggestion importance[1-10]: 10

    Why: The suggestion correctly identifies a bug where the wrong object was checked for the attribute, which could lead to incorrect behavior. Fixing this is crucial for the method's functionality.

    @MarkRx MarkRx force-pushed the bugfix/bb-server-fixes branch from 8b8db5b to 0f6d505 Compare May 30, 2024 14:00
    @MarkRx MarkRx force-pushed the bugfix/bb-server-fixes branch from 0f6d505 to ee90f38 Compare May 30, 2024 14:06
    @mrT23
    Copy link
    Collaborator

    mrT23 commented May 30, 2024

    @MarkRx
    once you will fix the code suggestion, we can merge

    @mrT23 mrT23 merged commit ea9d410 into qodo-ai:main May 31, 2024
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants