Skip to content

Instantly share code, notes, and snippets.

@tilayealemu
Last active February 19, 2024 14:48
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save tilayealemu/56fd45aab4f6943fac8447cc2019595c to your computer and use it in GitHub Desktop.
Save tilayealemu/56fd45aab4f6943fac8447cc2019595c to your computer and use it in GitHub Desktop.
List organization's repos, last commit date to master and committer
# Script to fetch all repos under a git organization
# Returns last committer to master and date of commit
# Results sorted by commit date
# Replace ORG_NAME, USERNAME, and PASSWORD variables
import urllib2
import requests
import json
ORG_NAME = 'my-org'
USERNAME = 'my-user'
PASSWORD = 'my-pass'
def rest_call(url):
return requests.get(url, auth=(USERNAME, PASSWORD))
def last_commit(repo):
url = "https://api.github.com/repos/" + ORG_NAME + "/" + repo + "/commits"
response = rest_call(url)
content = json.loads(response.content)
try:
commit_date = content[0]["commit"]["author"]["date"]
author = content[0]["commit"]["author"]["name"]
return (commit_date, author)
except KeyError:
if "message" in content and content["message"] == "Git Repository is empty.":
return ("<empty_repo>", "")
raise Exception("Don't know what to do with: " + content)
def list_repos():
repos = []
url = "https://api.github.com/orgs/" + ORG_NAME + "/repos"
while (url is not None):
print("processing repo list " + url)
response = rest_call(url)
content = json.loads(response.content)
for item in content:
repo = item["name"]
print("processing repo " + repo)
commit_date,author = last_commit(repo)
repos.append((repo, commit_date, author))
if "next" in response.links:
url = response.links["next"]["url"]
else:
url = None
return repos
repos = list_repos()
reposs = sorted(repos, key=lambda r: r[1])
for r in reposs:
print(r[0] + "," + r[1] + "," + r[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment