PR Commenter

0.2.4 · active · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `PRCommenter` and use its primary methods: `add_comment`, `update_comment`, and `remove_comment`. It requires a GitHub Personal Access Token with appropriate 'repo' scope permissions, passed via the `GITHUB_TOKEN` environment variable, to interact with your repository and pull requests.

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}")

view raw JSON →