Created
December 6, 2020 16:29
-
-
Save gayu19/7f1be16c42a19d4760bf6de39cf3ffea to your computer and use it in GitHub Desktop.
Gives you monthly commits of particular repository for last year
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
import requests | |
import os | |
from pprint import pprint | |
from datetime import datetime | |
token = os.getenv("GIT_TOKEN") | |
def monthyear(value): | |
"""Converts Unix timestamp to date time format | |
Args: | |
value (String): Unix value of week | |
""" | |
month_year = datetime.fromtimestamp(int(value)).strftime("%B-%Y") | |
return month_year | |
def each_month_commits(owner,repo): | |
"""Returns monthly commits of repository | |
Args: | |
owner (String): owner of repository | |
repo (String): repository name | |
Returns: | |
Dictionary of monthly commits | |
""" | |
commits_data = {} | |
url = f"https://api.github.com/repos/{owner}/{repo}/stats/commit_activity" | |
headers = {"Authorization": f"token {token}"} | |
response= requests.request("GET", url, headers=headers).json() | |
for data in response: | |
mm_yyyy = monthyear(data["week"]) | |
if mm_yyyy in commits_data: | |
commits_data[mm_yyyy] += data["total"] | |
else: | |
commits_data[mm_yyyy] = data["total"] | |
return commits_data | |
pprint(each_month_commits('gkorgtest' ,'testrepo')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment