Skip to content

Instantly share code, notes, and snippets.

@gayu19
Created December 6, 2020 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gayu19/7f1be16c42a19d4760bf6de39cf3ffea to your computer and use it in GitHub Desktop.
Save gayu19/7f1be16c42a19d4760bf6de39cf3ffea to your computer and use it in GitHub Desktop.
Gives you monthly commits of particular repository for last year
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