Skip to content

for changed file look for owners in parent folder recursively till owner is found#637

Closed
dbasunag wants to merge 6 commits intomainfrom
sigowners
Closed

for changed file look for owners in parent folder recursively till owner is found#637
dbasunag wants to merge 6 commits intomainfrom
sigowners

Conversation

@dbasunag
Copy link
Copy Markdown
Contributor

@dbasunag dbasunag commented Dec 10, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced logic for retrieving unique approvers and reviewers from OWNERS files in pull requests.
    • Added support for the "code" directory in the approval and review process.
  • Bug Fixes

    • Improved accuracy in identifying owner data for changed folders.
  • Tests

    • Updated test cases to validate the new structure for approvers and reviewers associated with the "code" directory.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Dec 10, 2024

Caution

Review failed

The head commit changed during the review from e46d52d to 8406cc3.

Walkthrough

The pull request introduces significant changes to the ProcessGithubWehook class in github_api.py, focusing on the management of approvers and reviewers. Enhancements include duplicate entry prevention in the get_all_approvers and get_all_reviewers methods, along with the addition of a private method for owner directory retrieval. The owners_data_for_changed_files method is refactored to improve clarity and maintainability. Additionally, the test suite in test_github_api.py is updated to accommodate a new path for owner data, ensuring that the approval and review processes remain accurate.

Changes

File Change Summary
webhook_server_container/libs/github_api.py - Enhanced get_all_approvers and get_all_reviewers methods to prevent duplicate entries.
- Added _get_owner_dir_name_for_changed_folder method for owner directory retrieval.
- Refactored owners_data_for_changed_files to use the new method.
webhook_server_container/tests/test_github_api.py - Updated owners_data_for_changed_files method to include new path "code" with specified approvers and reviewers.
- Adjusted test cases to validate the new structure.

Possibly related PRs

Suggested labels

size/L, verified, can-be-merged, approved-rnetser

Suggested reviewers

  • dbasunag
  • rnetser

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@myakove-bot
Copy link
Copy Markdown
Collaborator

Report bugs in Issues

The following are automatically added:

  • Add reviewers from OWNER file (in the root of the repository) under reviewers section.
  • Set PR size label.
  • New issue is created for the PR. (Closed when PR is merged/closed)
  • Run pre-commit if .pre-commit-config.yaml exists in the repo.

Available user actions:

  • To mark PR as WIP comment /wip to the PR, To remove it from the PR comment /wip cancel to the PR.
  • To block merging of PR comment /hold, To un-block merging of PR comment /hold cancel.
  • To mark PR as verified comment /verified to the PR, to un-verify comment /verified cancel to the PR.
    verified label removed on each new commit push.
  • To cherry pick a merged PR comment /cherry-pick <target branch to cherry-pick to> in the PR.
    • Multiple target branches can be cherry-picked, separated by spaces. (/cherry-pick branch1 branch2)
    • Cherry-pick will be started when PR is merged
  • To build and push container image command /build-and-push-container in the PR (tag will be the PR number).
    • You can add extra args to the Podman build command
      • Example: /build-and-push-container --build-arg OPENSHIFT_PYTHON_WRAPPER_COMMIT=<commit_hash>
  • To add a label by comment use /<label name>, to remove, use /<label name> cancel
  • To assign reviewers based on OWNERS file use /assign-reviewers
  • To check if PR can be merged use /check-can-merge
Supported /retest check runs
  • /retest tox: Retest tox
  • /retest build-container: Retest build-container
  • /retest python-module-install: Retest python-module-install
  • /retest pre-commit: Retest pre-commit
  • /retest all: Retest all
Supported labels
  • hold
  • verified
  • wip
  • lgtm

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
webhook_server_container/libs/github_api.py (1)

2082-2089: Consider adding safeguards against infinite recursion.

While the recursive approach is clean, it could benefit from additional error handling to prevent infinite recursion in case of deeply nested directories with no OWNERS file.

 def _get_owner_dir_name_for_changed_folder(self, changed_folder: str) -> str:
+    max_depth = 100  # Configurable maximum recursion depth
+    def _recursive_find(folder: str, depth: int = 0) -> str:
+        if depth > max_depth:
+            self.logger.warning(f"{self.log_prefix} Max recursion depth reached for {folder}")
+            return "."
+        if folder in self.all_approvers_and_reviewers.keys():
+            return folder
+        parent = str(Path(folder).parent)
+        if parent == folder:  # We've reached the root
+            return "."
+        return _recursive_find(parent, depth + 1)
+    
+    return _recursive_find(changed_folder)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 1d70f8a and 3eb1b3c.

📒 Files selected for processing (2)
  • webhook_server_container/libs/github_api.py (3 hunks)
  • webhook_server_container/tests/test_github_api.py (1 hunks)
🧰 Additional context used
📓 Learnings (1)
webhook_server_container/libs/github_api.py (1)
Learnt from: myakove
PR: myk-org/github-webhook-server#624
File: webhook_server_container/libs/github_api.py:596-604
Timestamp: 2024-11-21T13:34:45.218Z
Learning: In the codebase, aggregation of reviewers and approvers from all OWNERS files is handled within the `assign_reviewers` method.
🔇 Additional comments (3)
webhook_server_container/tests/test_github_api.py (1)

225-225: LGTM! Test coverage added for the new path.

The test case properly validates that the "code" path inherits approvers and reviewers from the root OWNERS file.

webhook_server_container/libs/github_api.py (2)

2041-2042: LGTM! Duplicate prevention added.

Good addition of duplicate checks before appending approvers and reviewers. This prevents potential issues with duplicate entries in the lists.

Also applies to: 2050-2052


2061-2072: LGTM! Well-structured refactoring.

The refactoring improves code clarity by:

  1. Using the new helper method for owner directory lookup
  2. Properly handling the root-approvers flag
  3. Using clear variable names


changed_folders = {Path(cf).parent for cf in self.changed_files}

all_owner_data = self.all_approvers_and_reviewers
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

why is this assignment needed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I need to change these based on what you suggested. This PR is not ready for review. I will add a wip.


return data

def _get_owner_dir_name_for_changed_folder(self, changed_folder: str) -> str:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

please get here Path, not str and return Path as well

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have changed it totally. Now I am returning the combined approvers, reviewers dict. Do you still want me to pass the Path to the function? Let me know I will update accordingly.

@@ -2056,20 +2058,18 @@ def owners_data_for_changed_files(self) -> dict[str, dict[str, Any]]:
data: dict[str, dict[str, Any]] = {}

changed_folders = {Path(cf).parent for cf in self.changed_files}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

can you please add docstring that shows examples?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Still have not added docstring. Will do it next.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (5)
webhook_server_container/libs/github_api.py (5)

2041-2042: Optimize duplicate checking by using a set for _approvers

Currently, the code checks for duplicates manually when appending to _approvers. Using a set can improve efficiency and readability.

Apply this diff to refactor the code:

 def get_all_approvers(self) -> list[str]:
-    _approvers: list[str] = []
+    _approvers: set[str] = set()
     for list_of_approvers in self.owners_data_for_changed_files().values():
         for _approver in list_of_approvers.get("approvers", []):
-            if _approver not in _approvers:
-                _approvers.append(_approver)
+            _approvers.add(_approver)
-    _approvers.sort()
-    return _approvers
+    return sorted(_approvers)

2050-2052: Optimize duplicate checking by using a set for _reviewers

Similar to _approvers, using a set for _reviewers avoids manual duplication checks and enhances performance.

Apply this diff to refactor the code:

 def get_all_reviewers(self) -> list[str]:
-    _reviewers: list[str] = []
+    _reviewers: set[str] = set()
     for list_of_reviewers in self.owners_data_for_changed_files().values():
         for _reviewer in list_of_reviewers.get("reviewers", []):
-            if _reviewer not in _reviewers:
-                _reviewers.append(_reviewer)
+            _reviewers.add(_reviewer)
-    _reviewers.sort()
-    return _reviewers
+    return sorted(_reviewers)

2062-2068: Clarify the control flow and handle missing OWNERS files gracefully

Ensure that all changed folders are processed correctly, even if some do not have associated OWNERS files. Also, consider adding comments to improve readability.

No code changes are necessary, but adding comments can help future maintainers understand the logic.


2074-2105: Avoid leading underscores in parameter names

Leading underscores in parameter names can be confusing, as they typically indicate private variables. Renaming _changed_folder to changed_folder improves code clarity and adheres to Python conventions.

Apply this diff to rename the parameter and update its usage:

 def _get_owner_data_for_changed_folder(self, changed_folder: str) -> dict[str, Any]:
     # If we find an entry for the changed folder in self.all_approvers_and_reviewers, then we return it.
     # Else we start from the parent and go all the way to root and combine all approvers and owners found.
     # "root-approvers" would be copied from the owner file found below root.
     # Example: if a file is changed under test_folder0/test_folder1/test_folder2/test_folder3/,
     # reviewers and approvers from test_folder3, test_folder2, test_folder1, test_folder0,
     # and root would all be combined to get approvers and reviewers for that file.

     _aggregated_owners: dict[str, Any] = {"approvers": [], "reviewers": []}
     owners_data = self.all_approvers_and_reviewers
-    if owners_data.get(_changed_folder):
-        return owners_data[_changed_folder]
+    if owners_data.get(changed_folder):
+        return owners_data[changed_folder]
     else:
         # Find all OWNERS files up to the root and combine them
         while changed_folder != ".":
-            changed_folder = str(Path(_changed_folder).parent) or "."
-            if owners_data.get(_changed_folder):
+            changed_folder = str(Path(changed_folder).parent) or "."
+            if owners_data.get(changed_folder):
                 _aggregated_owners["approvers"].extend([
                     _approver
-                    for _approver in owners_data[_changed_folder].get("approvers", [])
+                    for _approver in owners_data[changed_folder].get("approvers", [])
                     if _approver not in _aggregated_owners["approvers"]
                 ])
                 _aggregated_owners["reviewers"].extend([
                     _reviewer
-                    for _reviewer in owners_data[_changed_folder].get("reviewers", [])
+                    for _reviewer in owners_data[changed_folder].get("reviewers", [])
                     if _reviewer not in _aggregated_owners["reviewers"]
                 ])
                 if changed_folder != ".":
                     _aggregated_owners["root-approvers"] = owners_data[changed_folder].get("root-approvers", True)
         return _aggregated_owners

2083-2105: Improve efficiency by using sets for _aggregated_owners

Using sets for approvers and reviewers improves performance and simplifies duplicate handling.

Apply this diff to implement sets:

-_aggregated_owners: dict[str, Any] = {"approvers": [], "reviewers": []}
+_aggregated_owners: dict[str, Any] = {"approvers": set(), "reviewers": set()}
 owners_data = self.all_approvers_and_reviewers
 if owners_data.get(changed_folder):
     return owners_data[changed_folder]
 else:
     # Find all OWNERS files up to the root and combine them
     while changed_folder != ".":
         changed_folder = str(Path(changed_folder).parent) or "."
         if owners_data.get(changed_folder):
-            _aggregated_owners["approvers"].extend([
+            _aggregated_owners["approvers"].update([
                 _approver
                 for _approver in owners_data[changed_folder].get("approvers", [])
-                if _approver not in _aggregated_owners["approvers"]
             ])
-            _aggregated_owners["reviewers"].extend([
+            _aggregated_owners["reviewers"].update([
                 _reviewer
                 for _reviewer in owners_data[changed_folder].get("reviewers", [])
-                if _reviewer not in _aggregated_owners["reviewers"]
             ])
             if changed_folder != ".":
                 _aggregated_owners["root-approvers"] = owners_data[changed_folder].get("root-approvers", True)
-    return _aggregated_owners
+    return {
+        "approvers": list(_aggregated_owners["approvers"]),
+        "reviewers": list(_aggregated_owners["reviewers"]),
+        "root-approvers": _aggregated_owners.get("root-approvers", True),
+    }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 3eb1b3c and 8406cc3.

📒 Files selected for processing (2)
  • webhook_server_container/libs/github_api.py (2 hunks)
  • webhook_server_container/tests/test_github_api.py (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • webhook_server_container/tests/test_github_api.py
🔇 Additional comments (1)
webhook_server_container/libs/github_api.py (1)

2090-2104: Ensure termination of the while loop to prevent infinite loops

The condition changed_folder != "." may not be sufficient if Path(changed_folder).parent returns the same directory. Consider adding a base case or additional checks to ensure the loop will terminate.

Run the following script to check for potential infinite loops:

@dbasunag dbasunag closed this Dec 12, 2024
@myakove myakove deleted the sigowners branch April 6, 2025 08: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.

3 participants