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

feat: enhance PR processing logic across GitLab, GitHub, and Bitbucket #1267

Merged
merged 3 commits into from
Oct 2, 2024

Conversation

mrT23
Copy link
Collaborator

@mrT23 mrT23 commented Oct 2, 2024

User description

  • Refactor should_process_pr_logic to improve PR filtering based on data attributes.
  • Update _perform_commands_* functions to incorporate new PR processing checks.
  • Ensure consistent handling of PRs by checking configurations before executing commands.

PR Type

Enhancement


Description

  • Implemented consistent PR processing logic across Bitbucket, GitHub, and GitLab integrations
  • Enhanced should_process_pr_logic function to improve PR filtering based on various attributes (title, labels, branches)
  • Updated command execution functions to include PR processing checks before running commands
  • Improved bot user detection and handling in GitHub integration
  • Refactored GitLab webhook handler to use unified PR processing logic
  • Ensured consistent application of repository settings before PR processing

Changes walkthrough 📝

Relevant files
Enhancement
bitbucket_app.py
Enhance Bitbucket PR processing logic                                       

pr_agent/servers/bitbucket_app.py

  • Added should_process_pr_logic check in _perform_commands_bitbucket
    function
  • Updated function signature to include data parameter
  • Implemented PR processing check for 'pullrequest:created' event
  • +5/-2     
    github_app.py
    Improve GitHub PR filtering and processing                             

    pr_agent/servers/github_app.py

  • Updated should_process_pr_logic function signature and usage
  • Modified bot user check in handle_request function
  • Added PR processing check in _perform_auto_commands_github function
  • +5/-3     
    gitlab_webhook.py
    Refactor GitLab PR processing and filtering                           

    pr_agent/servers/gitlab_webhook.py

  • Updated should_process_pr_logic function to extract title from data
  • Modified PR processing checks in webhook handler
  • Added PR processing check in _perform_commands_gitlab function
  • +11/-5   

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

    - Refactor `should_process_pr_logic` to improve PR filtering based on data attributes.
    - Update `_perform_commands_*` functions to incorporate new PR processing checks.
    - Ensure consistent handling of PRs by checking configurations before executing commands.
    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🏅 Score: 85
    🧪 No relevant tests
    🔒 No security concerns identified
    🔀 Multiple PR themes

    Sub-PR theme: Refactor PR processing logic for Bitbucket and GitHub

    Relevant files:

    • pr_agent/servers/bitbucket_app.py
    • pr_agent/servers/github_app.py

    Sub-PR theme: Update GitLab webhook handler with new PR processing logic

    Relevant files:

    • pr_agent/servers/gitlab_webhook.py

    ⚡ Recommended focus areas for review

    Possible Bug
    The is_bot_user check is now only performed when 'check_run' is not in the body. This might lead to unintended processing of bot-created PRs in certain scenarios.

    Code Duplication
    The should_process_pr_logic function is implemented separately in different files. Consider centralizing this logic to avoid duplication and ensure consistency.

    @mrT23
    Copy link
    Collaborator Author

    mrT23 commented Oct 2, 2024

    /improve

    Copy link
    Contributor

    codiumai-pr-agent-pro bot commented Oct 2, 2024

    PR Code Suggestions ✨

    Latest suggestions up to e21d9dc

    CategorySuggestion                                                                                                                                    Score
    Enhancement
    Generalize the PR processing logic to handle multiple PR-related events

    The should_process_pr_logic function is called only for the 'pullrequest:created'
    event. Consider generalizing this check to handle other PR-related events as well,
    such as 'pullrequest:updated' or 'pullrequest:reopened', to ensure consistent
    processing across different PR actions.

    pr_agent/servers/bitbucket_app.py [80-82]

    -if data.get("event", "") == "pullrequest:created":
    +if data.get("event", "").startswith("pullrequest:"):
         if not should_process_pr_logic(data):
             return
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: This suggestion significantly improves the flexibility and consistency of the PR processing logic. By generalizing the check to handle multiple PR-related events, it ensures that the same processing rules are applied across different PR actions, reducing the risk of inconsistent behavior.

    9
    Consolidate PR processing logic by moving the bot user check into the main processing function

    Consider moving the is_bot_user check inside the should_process_pr_logic function.
    This would centralize the PR processing logic and make it easier to maintain. You
    can pass the sender and sender_type as parameters to should_process_pr_logic.

    pr_agent/servers/github_app.py [309-313]

    -if is_bot_user(sender, sender_type) and 'check_run' not in body:
    -    return {}
     if action != 'created' and 'check_run' not in body:
    -    if not should_process_pr_logic(body):
    +    if not should_process_pr_logic(body, sender, sender_type):
             return {}
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: This suggestion improves code organization and maintainability by centralizing the PR processing logic. It reduces duplication and makes the code easier to understand and modify in the future.

    7
    Best practice
    Improve error handling in the PR processing logic function

    The should_process_pr_logic function is checking for the existence of
    object_attributes in the data dictionary, but it's not using this information
    effectively. Consider refactoring this function to handle cases where
    object_attributes is missing more gracefully, possibly by returning early if it's
    not present.

    pr_agent/servers/gitlab_webhook.py [95-100]

     def should_process_pr_logic(data) -> bool:
         try:
    -        if not data.get('object_attributes', {}):
    +        object_attributes = data.get('object_attributes', {})
    +        if not object_attributes:
                 return False
    -        title = data['object_attributes'].get('title')
    +        title = object_attributes.get('title')
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: This suggestion enhances the robustness of the code by improving error handling. It provides a more graceful way to handle missing data, which can prevent potential runtime errors and improve the overall reliability of the function.

    8

    Previous suggestions

    ✅ Suggestions up to commit c2ae429
    CategorySuggestion                                                                                                                                    Score
    Bug fix
    Update function call to match the new signature of should_process_pr_logic

    The should_process_pr_logic function has been updated to accept only one parameter
    (body), but it's still being called with three parameters in the handle_request
    function. Update the function call to match the new signature.

    pr_agent/servers/github_app.py [311-313]

    +if action != 'created' and 'check_run' not in body:
    +    if not should_process_pr_logic(body):
    +        return {}
     
    -
    Suggestion importance[1-10]: 9

    Why: This suggestion fixes a critical bug where the function is called with incorrect parameters, which would likely cause runtime errors. It's essential for the correct functioning of the code.

    9
    Possible issue
    ✅ Ensure that the data parameter is provided and validated before processing
    Suggestion Impact:The commit changed the function signature to specify that 'data' should be a dict, which aligns with the suggestion's intent to ensure data is always provided.

    code diff:

    -                                   log_context: dict, data=None):
    +                                   log_context: dict, data: dict):

    The should_process_pr_logic function now expects a data parameter, but it's being
    called with None as the default value in the _perform_commands_gitlab function.
    Consider updating the function call to ensure data is always provided.

    pr_agent/servers/gitlab_webhook.py [61-65]

     async def _perform_commands_gitlab(commands_conf: str, agent: PRAgent, api_url: str,
    -                               log_context: dict, data=None):
    +                               log_context: dict, data: dict):
     apply_repo_settings(api_url)
    -if not should_process_pr_logic(data): # Here we already updated the configurations
    +if not data or not should_process_pr_logic(data): # Here we already updated the configurations
         return
    Suggestion importance[1-10]: 8

    Why: This suggestion addresses a potential issue where the function might be called with None as the data parameter. It improves the robustness of the code by adding a null check.

    8
    Error handling
    Add error handling for the PR processing logic check

    Consider adding error handling for the case where should_process_pr_logic raises an
    exception. This will prevent the function from crashing if there's an unexpected
    issue with the data structure.

    pr_agent/servers/bitbucket_app.py [80-82]

     if data.get("event", "") == "pullrequest:created":
    -    if not should_process_pr_logic(data):
    +    try:
    +        if not should_process_pr_logic(data):
    +            return
    +    except Exception as e:
    +        get_logger().error(f"Error in should_process_pr_logic: {e}")
             return
    Suggestion importance[1-10]: 7

    Why: This suggestion improves error handling, which can prevent unexpected crashes and provide better logging. It's a good practice for robust code, especially when dealing with external data.

    7
    Error prevention
    Add a default value when extracting the title from the data

    The should_process_pr_logic function now extracts the title from the data parameter,
    but it doesn't handle the case where the title might be missing. Consider adding a
    fallback or default value for the title to prevent potential errors.

    pr_agent/servers/gitlab_webhook.py [95-99]

     def should_process_pr_logic(data) -> bool:
         try:
             if not data.get('object_attributes', {}):
                 return False
    -        title = data['object_attributes'].get('title')
    +        title = data['object_attributes'].get('title', '')
    Suggestion importance[1-10]: 6

    Why: This suggestion adds a fallback value when extracting the title, which can prevent potential KeyError exceptions. While useful, it's less critical than some of the other suggestions.

    6

    @mrT23 mrT23 merged commit 481c2a5 into main Oct 2, 2024
    2 checks passed
    @mrT23 mrT23 deleted the tr/should_process branch October 2, 2024 14:10
    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