Skip to content

Instantly share code, notes, and snippets.

@morido
Last active April 11, 2024 19:32
Show Gist options
  • Save morido/9817399 to your computer and use it in GitHub Desktop.
Save morido/9817399 to your computer and use it in GitHub Desktop.
detect non-contributors in an organization
#!/usr/bin/env python
# you will need http://github3py.readthedocs.org/en/latest/#installation
# I'm not sure if this works on Windows due to the use of strptime()
from github3 import login
import datetime
import json
# Amend the following two lines as necessary
user, password = 'morido', 'mysupersecretpassword'
time_of_interest_in_weeks = 24 # roughly 6 months
# Attention: There is an hourly limit of 5000 requests to the GitHub-API. This script will use a lot of these...
token = login(user, password)
organization = token.organization('openETCS')
time_of_interest_in_weeks_absolute = datetime.datetime.today() - datetime.timedelta(weeks = time_of_interest_in_weeks)
openETCSusers = []
inactiveopenETCSusers = []
def removeFromInactiveList(loginname):
if loginname in inactiveopenETCSusers:
inactiveopenETCSusers.remove(loginname)
# Setup: Get all users in the organization
for current_repo in organization.iter_repos():
for current_contributor in current_repo.iter_contributors():
openETCSusers.append(current_contributor.login)
openETCSusers = list(set(openETCSusers)) # remove duplicates
inactiveopenETCSusers = list(openETCSusers) # initialize the inactive users
# Main part: Remove all users who 'did something'
for current_repo in organization.iter_repos():
print "Processing Repository",
print current_repo.name
# Step1: remove all users who made on a commit within time_of_interest_in_weeks
commitShasToInvestigate = []
for current_commit in current_repo.iter_commits(since = time_of_interest_in_weeks_absolute):
# usually author and committer are equal, but there are very few exceptions
removeFromInactiveList(str(current_commit.author))
removeFromInactiveList(str(current_commit.committer))
commitShasToInvestigate.append(current_commit.sha)
# Note: This will probably fail to recognize contributions for commits opened in the distant past (before 'since') and updated very recently
# Step2: remove all users who commented on a commit within time_of_interest_in_weeks
for commit_to_investigate in commitShasToInvestigate:
for current_commit_comments in current_repo.iter_comments_on_commit(sha = commit_to_investigate):
removeFromInactiveList(str(current_commit_comments.user))
# Step3: remove all users who commented on something within time_of_interest_in_weeks
try:
for current_issue in current_repo.iter_issues():
for current_comment in current_issue.iter_comments():
json_data = current_comment.to_json()
json_parsed = json.loads(json.dumps(json_data))
JSON_TIMEFORMAT = '%Y-%m-%dT%H:%M:%SZ'
updated_at = datetime.datetime.strptime(json_parsed["updated_at"], JSON_TIMEFORMAT)
if updated_at > time_of_interest_in_weeks_absolute:
removeFromInactiveList(json_parsed["user"]["login"])
except Exception:
# no idea why this happens occassionally, some access rights issue?
print 'Error with Repository',
print current_repo.name
###
### OUTPUT
###
print '\n'
print 'Considered all contributions after:',
print str(time_of_interest_in_weeks_absolute)
print '\n'
print '---------------'
print 'Inactive users:'
print '---------------\n'
for current_user in inactiveopenETCSusers:
print current_user
print '\n'
print 'Summary:'
print '--------\n'
print '# Inactive users: ',
print len(inactiveopenETCSusers)
print '# All users: ',
print len(openETCSusers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment