PR Commenter
PR Commenter is a Python library that simplifies the process of adding, updating, and removing automated comments on GitHub Pull Requests. It leverages PyGithub to interact with the GitHub API, providing a clean interface for managing discussion threads. The current version is 0.2.4, and releases are generally made on an 'as-needed' basis, with a focus on stability for its core functionality.
Common errors
-
github.GithubException.BadCredentialsException: 401 {'message': 'Bad credentials'}cause The GitHub Personal Access Token (PAT) provided is invalid, expired, or has insufficient permissions.fixVerify your `GITHUB_TOKEN` environment variable or direct string value. Check the PAT's expiration date and ensure it has the necessary 'repo' scope (or specific 'issues' / 'pull requests' write permissions) on GitHub. -
github.GithubException.UnknownObjectException: 404 {'message': 'Not Found' ...}cause The repository name or pull request number provided to `g.get_repo()` or `repo.get_pull()` does not exist or is inaccessible to the authenticated user.fixDouble-check the `repo_name` (e.g., 'owner/repo') and `pr_number`. Ensure the GitHub token has read access to that specific repository and pull request. -
Comments are added, but `update_comment` or `remove_comment` doesn't work or creates duplicates.
cause The `comment_id` used for `update_comment` or `remove_comment` does not match an existing comment added by `pr-commenter`, or `add_comment` was called multiple times with the same `comment_id`.fixEnsure that the `comment_id` you pass to `update_comment` or `remove_comment` exactly matches the ID of a comment previously created with `add_comment`. `add_comment` should only be called once per unique `comment_id` to prevent duplicates if you intend to update it later.
Warnings
- gotcha The `comment_id` parameter is crucial for managing automated comments. If you reuse the same `comment_id` with `add_comment`, it will create duplicate comments instead of updating existing ones. `update_comment` and `remove_comment` rely on this ID to find the correct comment.
- gotcha GitHub Personal Access Tokens (PATs) used with `PyGithub` and `pr-commenter` must have sufficient permissions. Typically, the 'repo' scope is required to create, update, and delete comments on pull requests.
- gotcha There is no built-in `remove_all_comments` method for all comments added by the `PRCommenter` instance. If you need to remove multiple automated comments, you must iterate through them and call `remove_comment` for each `comment_id`.
Install
-
pip install pr-commenter
Imports
- PRCommenter
from pr_commenter import PRCommenter
from pr_commenter.pr_commenter import PRCommenter
Quickstart
import os
from pr_commenter.pr_commenter import PRCommenter
from github import Github
# Ensure GITHUB_TOKEN is set as an environment variable with 'repo' scope
github_token = os.environ.get('GITHUB_TOKEN', '')
if not github_token:
raise ValueError("GITHUB_TOKEN environment variable not set.")
g = Github(github_token)
# Replace with your actual repository and PR number
repo_name = "octocat/Spoon-Knife" # Example: 'your_user/your_repo_name'
pr_number = 1 # Example: your PR number
try:
repo = g.get_repo(repo_name)
pr = repo.get_pull(pr_number)
pc = PRCommenter(pr)
# Add a new comment
comment_id = "my_unique_automation_id_123"
print(f"Adding/Updating comment with ID: {comment_id}")
pc.add_comment("This is an automated comment.", comment_id)
# Update an existing comment
pc.update_comment("This is an UPDATED automated comment.", comment_id)
# Remove a comment
# pc.remove_comment(comment_id)
print("PR Commenter operations completed.")
except Exception as e:
print(f"An error occurred: {e}")