Using PyGitHub to Manage and Track GitHub Repositories

Using PyGitHub to Manage and Track GitHub Repositories
SHARE

Because GitHub will only provide stats for the previous 14 days, if we want to maintain long-term stats, we need to pull stats and store them. PyGitHub is a useful Python library for doing that using the GitHub API v3. This Python script will pull stats from queried repositories and store them in a CSV.

import os import csv from github import Github GITHUB_KEY = os.environ.get("POETRY_GITHUB_KEY") g = Github(GITHUB_KEY) with open("repos.csv", "w", newline="", encoding="utf-8") as csvfile: repo_writer = csv.writer(csvfile, delimiter=",") repo_writer.writerow([ "Repo Name", "Repo Link", "Repo Pushed At", "Forks", "StarGazers", "Open Issues", "Security Alerts", "Open Pull Requests"]) for repo in g.search_repositories(query="user:schuettc cdk"): repo_writer.writerow( [ repo.name, repo.html_url, repo.pushed_at, repo.forks_count, repo.stargazers_count, repo.open_issues_count, repo.get_vulnerability_alert(), repo.get_pulls().totalCount, ] )

This script will query GitHub for user:schuettc cdk. This query will return all GitHub repositories created by user schuettc (me) with cdk in the repository name. The result will be used to create a CSV file that contains these repositories and various GitHub stats.

Be sure to create a GitHub personal access token with an appropriate scope and save as POETRY_GITHUB_KEY in a local .env file to use.

The result will be something like this:

CSV