66 lines
2.1 KiB
Python
66 lines
2.1 KiB
Python
|
import requests
|
||
|
import datetime
|
||
|
from matplotlib import pyplot as plt
|
||
|
|
||
|
PULLS_URL = "https://api.github.com/repos/{owner}/{repo}/pulls"
|
||
|
|
||
|
|
||
|
class TooManyRequests(Exception):
|
||
|
pass
|
||
|
|
||
|
|
||
|
class RepoPulls:
|
||
|
def __init__(self, owner, repo, user=None, token=None):
|
||
|
self.owner = owner
|
||
|
self.repo = repo
|
||
|
self.url = PULLS_URL.format(owner=self.owner, repo=self.repo)
|
||
|
self.auth = None
|
||
|
if user and token:
|
||
|
self.auth = requests.auth.HTTPBasicAuth(user, token)
|
||
|
self._pulls = {"open": None, "close": None}
|
||
|
|
||
|
def get_pulls(self, state="open", verbose=True):
|
||
|
assert state in self._pulls
|
||
|
if self._pulls[state] is None:
|
||
|
pulls = []
|
||
|
with requests.Session() as session:
|
||
|
if self.auth:
|
||
|
session.auth = self.auth
|
||
|
cur_page = 1
|
||
|
while True:
|
||
|
if verbose:
|
||
|
print("Requesting page {}...".format(cur_page))
|
||
|
req = session.get(
|
||
|
self.url,
|
||
|
params={"state": state, "page": cur_page, "per_page": 100},
|
||
|
)
|
||
|
if req.status_code in (403, 429):
|
||
|
raise TooManyRequests(
|
||
|
"GitHub doesn't want to hear from us anymore :c"
|
||
|
)
|
||
|
cur_pulls = req.json()
|
||
|
if len(cur_pulls) == 0:
|
||
|
break
|
||
|
cur_page += 1
|
||
|
pulls += cur_pulls
|
||
|
|
||
|
self._pulls[state] = pulls
|
||
|
|
||
|
return self._pulls[state]
|
||
|
|
||
|
def get_open_ages(self):
|
||
|
pulls = self.get_pulls("open")
|
||
|
created_dates = [
|
||
|
datetime.date.fromisoformat(pull["created_at"][:10]) for pull in pulls
|
||
|
]
|
||
|
today = datetime.date.today()
|
||
|
ages = [(today - created_on).days for created_on in created_dates]
|
||
|
|
||
|
return ages
|
||
|
|
||
|
def plot_ages(self, *args, show=False, **kwargs):
|
||
|
ages = self.get_open_ages()
|
||
|
plt.hist(ages, *args, **kwargs)
|
||
|
if show:
|
||
|
plt.show()
|