Skip to content

Instantly share code, notes, and snippets.

@manju4ever
Last active September 1, 2020 16:28
Show Gist options
  • Select an option

  • Save manju4ever/9ae0ec17f40236c32ae2113f473f81ae to your computer and use it in GitHub Desktop.

Select an option

Save manju4ever/9ae0ec17f40236c32ae2113f473f81ae to your computer and use it in GitHub Desktop.
Find authors in commit(s) in the time range (GitHub)
#!/usr/bin/python
import requests
import sys
import json
GITHUB_ENDPOINT = "https://api.github.com"
GIHUB_API_KEY = "84b84ed3699ff6d54c11171edc2f57fd25a9a71f"
# Print helper for the script
print("\n||| Some Github Shit |||\n")
if len(sys.argv) < 2:
print("Usage:read_repo.py <repository>")
# Ask for user inputs like repository_url
repo_url = sys.argv[1]
split_res = repo_url.split('/')
username = split_res[3]
repo_name = split_res[4]
print(f"[TRACE] username={username}, repository={repo_name}")
'''
Following headers are required to be added
* Authorization: token <PERSONAL ACCESS TOKEN>
'''
request_headers = {
'Authorization': f"token {GIHUB_API_KEY}"
}
def getRepositoriesOfUser(username):
try:
request_url = f"{GITHUB_ENDPOINT}/users/{username}/repos?per_page=1000"
request = requests.get(request_url)
results = request.json()
return results
except Exception as someErr:
print("[ERROR] Fetching user repos failed", someErr)
return None
# GET /repos/:owner/:repo/commits, params = since, until
def findCommits(owner, repo, start_date, end_date):
try:
print("[DEBUG] Finding files in repo:", repo, start_date, end_date)
request_params = f"?since={start_date}&until={end_date}"
request_url = f"{GITHUB_ENDPOINT}/repos/{owner}/{repo}/commits"
commits_in_repo = requests.get(request_url + request_params, headers=request_headers)
print("[TRACE] user commits url:", commits_in_repo.url)
results = commits_in_repo.json()
print("[TRACE] results of findCommits:", results)
return results
except Exception as someErr:
print("[ERROR] Fetching Commits Failed - ", someErr)
return None
def findAuthors(history_list):
print("[DEBUG] Total Commits Found", len(history_list))
unique_authors = set()
for record in history_list:
# print(record)
unique_authors.add(record["commit"]["author"]["name"])
print("[TRACE] Unique Commit URL:", record["url"])
print("[TRACE] Total unique authors found:", len(unique_authors))
return unique_authors
def findTouchesKPI(history_list):
fileAuthorDict = {}
print('[DEBUG] Total Commits Found', len(history_list))
try:
for commit_info in history_list:
print("[TRACE] Accessing - ", commit_info["url"])
author_of_commit = commit_info["commit"]["author"]["name"]
with requests.get(commit_info["url"], headers=request_headers) as response:
data = response.json()
all_files = data["files"]
for eachFile in all_files:
filename = eachFile["filename"]
print("[TRACE] Got the filename as:", filename)
if filename not in fileAuthorDict:
print("[TRACE] File init for first time:", filename)
fileAuthorDict[str(filename)] = {
"count": 1,
"authors": set([str(author_of_commit)])
}
else:
print("[TRACE] File already found in map:", filename, fileAuthorDict[str(filename)])
fileAuthorDict[str(filename)]["count"] = fileAuthorDict[str(filename)]["count"] + 1
fileAuthorDict[str(filename)]["authors"].add(str(author_of_commit))
print("[DEBUG] Results of findTouchesKPI")
print(fileAuthorDict)
return fileAuthorDict
except Exception as someErr:
print('[ERROR] Fetching touched kpi failed !', someErr)
return fileAuthorDict
'''
The rest of the code starts from here
* Every commit has
-> filenames
-> author of the commit
* Final / Intermediate Output:
{
"hello.txt": {
"count": 12,
"authors": set("manju", "siddu")
},
"file_X": {
"count": N,
"authors": set(a1, a2, a3, ...)
}
}
'''
repos = getRepositoriesOfUser(username)
start_date = '2020-08-10T00:00:44+0000'
end_date = '2020-08-29T00:00:44+0000'
repo_commits = findCommits(username, repo_name, start_date, end_date)
result = findTouchesKPI(repo_commits)
# Check from Here siddu
for k, v in result.items():
print(f"\n-- {k} | touched: {v['count']} times, by: {len(v['authors'])} authors\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment