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

fix: support adding comments for fork repo #78

Closed

Conversation

shenxianpeng
Copy link
Contributor

@shenxianpeng shenxianpeng commented Oct 29, 2024

closes #77

Summary by CodeRabbit

  • New Features

    • Enhanced GitHub Actions workflow to allow modifications to repository contents and added ID token permissions for improved authentication.
    • Integrated retry logic for GitHub API interactions to improve reliability during transient errors.
  • Bug Fixes

    • Adjusted the PyGithub package version to ensure compatibility with the updated functionality.

@shenxianpeng shenxianpeng changed the title feat: pdate commit-check.yml feat: update commit-check.yml Oct 29, 2024
@shenxianpeng shenxianpeng added the bug Something isn't working label Oct 29, 2024
@shenxianpeng shenxianpeng changed the title feat: update commit-check.yml fix: support adding comments for fork repo Oct 29, 2024
@shenxianpeng
Copy link
Contributor Author

Will take a look this link to see if any solution https://github.com/search?q=PyGitHub+Resource+not+accessible+by+integration&type=issues

@shenxianpeng
Copy link
Contributor Author

@shenxianpeng
Copy link
Contributor Author

@CodeRabbit review

Copy link

coderabbitai bot commented Oct 31, 2024

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

coderabbitai bot commented Oct 31, 2024

Walkthrough

The pull request modifies the GitHub Actions workflow configuration by updating permissions in the .github/workflows/commit-check.yml file. It changes the contents permission from read to write and adds id-token permission with a value of write. Additionally, the main.py file is updated to include retry logic for GitHub API interactions by modifying the import statement and instantiation of the Github client. Lastly, the requirements.txt file reflects a downgrade of the PyGithub package version from 2.4.0 to 2.1.0.post0.

Changes

File Change Summary
.github/workflows/commit-check.yml Updated contents permission to write, added id-token permission with write.
main.py Added GithubRetry to the import and updated Github client instantiation for retry logic.
requirements.txt Downgraded PyGithub package version from 2.4.0 to 2.1.0.post0.

Assessment against linked issues

Objective Addressed Explanation
Posting comments (pr-comments) doesn't work for fork repo (#77) The changes do not address the 403 error for forked repos.

🐰 In the meadow, I hop with glee,
Permissions changed, oh what a spree!
With retries in place, we'll comment with flair,
Forked repos, take heed, we’re almost there!
Let's celebrate this code, so bright and new,
A hop and a skip, for changes that grew! 🌼


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

Copy link

@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: 1

🧹 Outside diff range and nitpick comments (2)
.github/workflows/commit-check.yml (1)

12-14: Consider adding security documentation for elevated permissions.

While the permission changes are necessary, it's recommended to document why these elevated permissions are required, especially for security auditing purposes.

Add a comment block explaining the permissions:

    permissions:  # use permissions because of use pr-comments
+     # Enhanced permissions required for commenting on forked repository PRs:
+     # - contents: write - Required for creating/updating PR comments
+     # - id-token: write - Required for authentication with forked repositories
      contents: write
      pull-requests: write
      id-token: write
main.py (1)

107-108: Remove commented-out code.

The old initialization can be safely removed as it's tracked in version control.

-        # g = Github(token)
         g = Github(token, retry=GithubRetry())
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between f87ca4d and f163f3a.

📒 Files selected for processing (3)
  • .github/workflows/commit-check.yml (1 hunks)
  • main.py (2 hunks)
  • requirements.txt (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • requirements.txt
🔇 Additional comments (3)
.github/workflows/commit-check.yml (1)

12-14: Permission changes look appropriate for handling forked repository comments.

The elevated permissions are necessary to resolve the 403 Forbidden error when posting comments on pull requests from forked repositories. The changes align with GitHub's documentation for handling cross-repository interactions.

Let's verify the minimal required permissions:

✅ Verification successful

Permission configuration is appropriate and consistent across workflows

The elevated permissions are justified and properly scoped:

  • Both workflows use similar permission sets (contents: write, pull-requests: write)
  • The additional id-token: write in commit-check.yml is necessary for authentication with forked repos
  • Permissions align with their specific triggers:
    • commit-check.yml runs on pull requests to main
    • used-by.yml runs on schedule/manual dispatch
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if there are any other workflows in the repository with similar permissions
# to ensure consistency and identify potential over-privileged workflows

# Search for workflow files with permission declarations
echo "Checking other workflow permissions..."
rg -l 'permissions:' .github/workflows/

Length of output: 191


Script:

#!/bin/bash
# Let's examine the permissions in both workflow files to compare them

echo "=== Permissions in commit-check.yml ==="
rg -A5 'permissions:' .github/workflows/commit-check.yml

echo -e "\n=== Permissions in used-by.yml ==="
rg -A5 'permissions:' .github/workflows/used-by.yml

# Also check the triggers to understand the context
echo -e "\n=== Workflow triggers in commit-check.yml ==="
rg "on:" -A5 .github/workflows/commit-check.yml

echo -e "\n=== Workflow triggers in used-by.yml ==="
rg "on:" -A5 .github/workflows/used-by.yml

Length of output: 1412

main.py (2)

6-6: LGTM! Good addition of retry mechanism.

Adding GithubRetry is a good approach to handle transient GitHub API errors, which could help with the 403 Forbidden errors on forked repositories.


107-108: Verify retry configuration with PyGithub 2.1.0.post0.

The code uses GithubRetry() with default settings. Let's verify the retry configuration works as expected with the downgraded PyGithub version.

✅ Verification successful

Let me gather more information about the GithubRetry usage and configuration.


GithubRetry configuration is compatible and properly implemented

The code correctly uses GithubRetry() with PyGithub 2.1.0.post0. The default retry configuration is appropriate for this use case as:

  • The GithubRetry class is properly imported from the github module
  • PyGithub 2.1.0.post0 is explicitly specified in requirements.txt
  • The retry handler is correctly instantiated without custom parameters, which provides sensible defaults for retrying failed API requests
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify GithubRetry compatibility and configuration in PyGithub 2.1.0.post0

# Test 1: Check if GithubRetry is available in the installed version
pip freeze | grep -i pygithub

# Test 2: Search for GithubRetry usage patterns in the codebase
rg -l "GithubRetry"

# Test 3: Look for any retry-related configurations
rg "retry.*=|retries.*=" 

Length of output: 212


Script:

#!/bin/bash
# Check the imports and configuration of GithubRetry
rg -A 5 -B 5 "from github import.*GithubRetry" 

# Look for any custom retry configurations or settings
rg -A 3 "GithubRetry\(" 

# Check requirements.txt or similar files for PyGithub version
fd "requirements.*\.txt$" --exec cat {}

Length of output: 764

Comment on lines +107 to +108
# g = Github(token)
g = Github(token, retry=GithubRetry())
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider enhancing error handling for specific GitHub API errors.

The current error handling catches all exceptions generically. Consider adding specific handling for GitHub API errors, especially for authentication and permission issues that are common with forked repositories.

         g = Github(token, retry=GithubRetry())
         repo = g.get_repo(repo_name)
         pull_request = repo.get_issue(int(pr_number))
@@ -144,8 +144,17 @@
         return 0 if result_text is None else 1
-    except Exception as e:
-        print(f"Error posting PR comment: {e}", file=sys.stderr)
-        return 1
+    except github.GithubException as e:
+        if e.status == 403:
+            print(f"Permission denied: {e.data.get('message', '')}", file=sys.stderr)
+            print("This might be due to restricted permissions on forked repositories.", file=sys.stderr)
+        elif e.status == 404:
+            print(f"Resource not found: {e.data.get('message', '')}", file=sys.stderr)
+        else:
+            print(f"GitHub API error: {e}", file=sys.stderr)
+        return 1
+    except Exception as e:
+        print(f"Unexpected error: {e}", file=sys.stderr)
+        return 1

Also applies to: 144-146

@shenxianpeng shenxianpeng deleted the shenxianpeng-patch-1 branch November 11, 2024 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Posting comments (pr-comments) doesn't work for fork repo
1 participant