Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

GitHub PR Statistics

%load_ext autoreload
%autoreload 2

import logging

# Initialize logger.
logging.basicConfig(level=logging.INFO)
_LOG = logging.getLogger(__name__)
import helpers.github_utils as hgitutil

GitHub PR Statistics

Demonstrate how to use helpers.github_utils to:

  • Connect to the GitHub API
  • Count open PRs broken down by author and draft/ready status
  • Count closed PRs over a specified time period broken down by author

Configuration

Set the target repository and optional date range for closed PR filtering.

# Target repository in "owner/repo" format.
REPO_NAME = "causify-ai/helpers"
# Optional date range for closed PR stats (set to None to include all time).
START_DATE = "2024-01-01"
END_DATE = "2024-12-31"

Connect to GitHub API

# Authenticate using the GITHUB_ACCESS_TOKEN environment variable.
gh_api = hgitutil.GitHubAPI()
client = gh_api.get_client()
_LOG.info("Connected to GitHub API.")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[4], line 2
      1 # Authenticate using the GITHUB_ACCESS_TOKEN environment variable.
----> 2 gh_api = hgitutil.GitHubAPI()
      3 client = gh_api.get_client()
      4 _LOG.info("Connected to GitHub API.")

File /git_root/helpers_root/helpers/github_utils.py:105, in GitHubAPI.__init__(self, access_token, base_url)
    103 self.access_token = access_token or os.getenv("GITHUB_ACCESS_TOKEN")
    104 if not self.access_token:
--> 105     raise ValueError(
    106         "GitHub Access Token is required. Set it as an environment variable or pass it explicitly."
    107     )
    108 auth = github.Auth.Token(self.access_token)
    109 self.github = (
    110     github.Github(base_url=base_url, auth=auth)
    111     if base_url
    112     else github.Github(auth=auth)
    113 )

ValueError: GitHub Access Token is required. Set it as an environment variable or pass it explicitly.

Fetch Repository

repo_obj = client.get_repo(REPO_NAME)
_LOG.info("Fetched repository: %s", REPO_NAME)

Open PR Statistics

Count and display all open PRs broken down by author and draft/ready status.

_LOG.info("Fetching open PRs...")
open_stats = hgitutil.count_open_prs_by_author(repo_obj)
hgitutil.print_open_pr_stats(open_stats)

Closed PR Statistics

Count and display closed PRs in the specified date range, broken down by author.

period = hgitutil.utc_period(START_DATE, END_DATE)
_LOG.info("Fetching closed PRs for period %s to %s...", START_DATE, END_DATE)
closed_stats = hgitutil.count_closed_prs_by_author(repo_obj, period=period)
hgitutil.print_closed_pr_stats(closed_stats, period=period)

Closed PR Statistics (All Time)

Count and display closed PRs across all time, broken down by author.

_LOG.info("Fetching closed PRs for all time...")
closed_stats_all = hgitutil.count_closed_prs_by_author(repo_obj)
hgitutil.print_closed_pr_stats(closed_stats_all)

Close Connection

gh_api.close_connection()
_LOG.info("Connection closed.")