Skip to content

Instantly share code, notes, and snippets.

@cgopalan
Created February 25, 2015 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgopalan/c3fcefc7a5dbeab47a0c to your computer and use it in GitHub Desktop.
Save cgopalan/c3fcefc7a5dbeab47a0c to your computer and use it in GitHub Desktop.
Searches all repos in an organization for a text
"""
Search for a text in all repos for an organization.
Currently via the web UI you can only search per repo.
This will only return number of matches for the text and not
the filenames (as of now).
You need an OAuth Token for this to work.
Run it as:
python3 search_all_org_repos.py <search text>
TODO:
1. More details for a match.
2. Verbose output if necessary.
"""
import sys
import json
import urllib.request
import time
# Your OAuth Token here
OAUTH_TOKEN='xyz'
# Your organization name here
ORGANIZATION = 'sailthru'
def search(text):
""" Searches the repos for the org and only lists the names of the repos in which the match is found. """
search_repo_str = 'https://api.github.com/search/code?access_token={0}&per_page=100&q={1}+repo:{2}/{3}'
list_repo_str = 'https://api.github.com/orgs/{0}/repos?access_token={1}&per_page=100'.format(ORGANIZATION,
OAUTH_TOKEN)
all_repo_info = json.loads(urllib.request.urlopen(list_repo_str).read().decode())
all_repos = [(x['name'], x['language']) for x in all_repo_info]
# Loop over each repo and search!
print("Checking {0} repos: ".format(len(all_repos)))
results = []
for repo,lang in all_repos:
finds = json.loads(urllib.request.urlopen(search_repo_str.format(OAUTH_TOKEN, text,
ORGANIZATION, repo)).read().decode())
if finds:
if finds['total_count'] > 0:
print("S", end="", flush=True)
# Gather the search results to be printed at the end.
results.append("Repo: {0} Language: {1} Total count: {2}".format(repo, lang, finds['total_count']))
else:
print(".", end="", flush=True)
# Github allows only about 20 requests per min.
time.sleep(3)
print('\n')
if results:
for x in results:
print(x)
else:
print("No results found for {0}".format(text))
if __name__ == '__main__':
if len(sys.argv) == 2:
search(sys.argv[1])
else:
print("Run the program like this: python3 search_all_org_repos.py our_search_text_here")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment