Skip to content

Instantly share code, notes, and snippets.

@lukereding
Created November 24, 2019 15:34
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 lukereding/972cf51a811a91688104752d9a0a16f4 to your computer and use it in GitHub Desktop.
Save lukereding/972cf51a811a91688104752d9a0a16f4 to your computer and use it in GitHub Desktop.
Summary counts of comments on issues by users in a github repo
import requests
import pandas as pd
import altair as alt
from typing import List, Tuple
def download_comments(repo: str, user: str) -> List[str]:
url = f"https://api.github.com/repos/{user}/{repo}/issues/comments"
res = requests.get(url)
if res.ok:
return res.json()
else:
raise Exception("Couldn't download the data from github")
def get_dates_and_users(comments: List[str] = None) -> pd.DataFrame:
"""Parse the output from download_comments()"""
ret: List[Tuple[str, str]] = []
for comment in comments:
ret.append((comment["created_at"], comment["user"]["login"]))
df = pd.DataFrame(ret, columns=["date","user"])
# set the date as the index
df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date')
return df
def get_summary_df(df, timeframe: str = "week") -> pd.DataFrame:
if timeframe == "week":
return df.groupby("user").resample('W').count().rename({"user": "count"}, axis=1).reset_index()
elif timeframe == "month":
return df.groupby("user").resample('M').count().rename({"user": "count"}, axis=1).reset_index()
else:
print(f"Using timeframe of {timeframe} is not supported.")
raise
repo = ""
user = ""
comments = download_comments(repo, user)
df = get_dates_and_users(comments)
wrangled = get_summary_df(df)
chart = (alt.Chart(wrangled)
.mark_line(point=True)
.encode(x="date",
y="count",
color="user",
tooltip=['user', 'count']
)
.properties(
title=f"Comments on issues in {repo} over time"
)
.interactive()
)
chart.save(f"{repo}_comments.html")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment