Search code examples
pygithub

I need a faster way to find repos with PyGitHub


My organization has a ton of repos in GitHub. I have a subset of these repos that I want to programmatically detect. All of the repos I want start with the same string. The code below correctly finds them, but it takes about a minute to do it because of all of the repos present. Is there a faster way to do this?

def FindMyRepos():
  global ACCESS_TOKEN, ORGANIZATION, PREFIX
  repos = []
  # Log into GitHub
  gh = Github(ACCESS_TOKEN)
  if not gh: return 1
  # Connect to organization
  org = gh.get_organization(ORGANIZATION)
  if not org: return 2
  # Look for repos that start with PREFIX
  for r in org.get_repos():
    if r.name.startswith(PREFIX):
      repos.append(r.name)

Solution

  • After a lot of searching, I have found the answer. I need to use the search_repositories method. The one 'gotcha' is that this method is part of the Github object but not the organization object.

    def FindMyRepos():
      global ACCESS_TOKEN, ORGANIZATION, PREFIX
      repos = []
      # Log into GitHub
      gh = Github(ACCESS_TOKEN)
      if not gh: return 1
      # Look for repos in ORGANIZATION containing PREFIX
      for r in gh.search_repositorires(query=ORGANIZATION+'/'+PREFIX):
        # Only include repos starting with PREFIX
        if r.name.startswith(PREFIX):
          repos.append(r.name)
    

    The reason for the if r.name.startswith(PREFIX) is because for some reason the search will return all repos in the ORGANIZATION that have the string PREFIX in their name anywhere.