Skip to content

Instantly share code, notes, and snippets.

@ctubbsii
Created July 3, 2024 19:16
Show Gist options
  • Save ctubbsii/e13d37aaff2360ab46dda2c210bc4260 to your computer and use it in GitHub Desktop.
Save ctubbsii/e13d37aaff2360ab46dda2c210bc4260 to your computer and use it in GitHub Desktop.
A simple example of using pygithub to list repos for cloning with an OAuth2 token
#! /usr/bin/python3
# This script is a very basic example of how to list repos using pygithub and
# clone them with an OAuth2 token. Ideally, you'd retrieve the token from a
# secure keystore using something like libsecret, rather than hard-code it into
# the script, and you'd clone using a method that prevents the token from being
# stored in your shell history for security, but this is just an example.
# tested with python3-pygithub-1.58.2-5.fc40.noarch
# docs at https://pygithub.readthedocs.io/
from github import Github
# variables to edit
org_name = 'MYORGNAME'
token = 'ghp_TOKEN-GOES-HERE'
# authenticate and grab the Organization object
org = Github(token).get_organization(org_name)
# iterate through the resulting Repository objects
# could filter on 'public' or 'private' instead of 'all' if desired
for repo in org.get_repos(type='all'):
print(f'private:{repo.private}|archived:{repo.archived}|clonecmd:\'git clone https://oauth2:{token}@github.com/{org_name}/{repo.name}\'')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment