Last active
February 19, 2024 14:48
-
-
Save tilayealemu/56fd45aab4f6943fac8447cc2019595c to your computer and use it in GitHub Desktop.
List organization's repos, last commit date to master and committer
This file contains hidden or 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 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