Last active
April 12, 2022 18:57
-
-
Save TriplEight/5c1ff08212744c69495d8ca5a3567643 to your computer and use it in GitHub Desktop.
List organization's repos, last master commit date and author
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Script to fetch all repos under a git organization | |
# Returns last committer to master, date of commit | |
# and if the repo is archived. | |
# | |
# Results sorted by commit date | |
# Replace ORG_NAME, USERNAME, and PASSWORD variables | |
# import urllib2 | |
from urllib.request import urlopen | |
import requests | |
import json | |
ORG_NAME = 'org' | |
USERNAME = 'user' | |
PASSWORD = 'token' | |
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: | |
# print(json.dumps(item, indent=4, sort_keys=True)) | |
repo = item["name"] | |
print("processing repo " + repo) | |
commit_date,author = last_commit(repo) | |
archived = str(item["archived"]) | |
repos.append((repo, commit_date, author, archived)) | |
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] + "," + r[3]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment