Created
February 25, 2020 14:23
-
-
Save nkammah/70bb199c1f34e41c635e1aa3601fbb94 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
import json | |
import os | |
import requests | |
import subprocess | |
import sys | |
import time | |
from requests.auth import HTTPBasicAuth | |
from datetime import datetime, timedelta | |
gh_teams = { | |
('dev', None) : [], | |
('infra', 'release') : ["mgreau", "conky5", "fatmcgav", "jmlrt"], | |
('infra', 'automation') : ["leathekd", "jonahbull", "itsmed"] | |
} | |
def graphql(query): | |
json = {"query": query} | |
r = requests.post( | |
url="https://api.github.com/graphql", | |
json=json, | |
auth=HTTPBasicAuth(os.environ["GITHUB_USER"], os.environ["GH_TOKEN"]), | |
) | |
try: | |
return r.json()["data"] | |
except: | |
return None | |
def graphql_prs(owner, repo, cursor): | |
after = "" | |
if cursor: | |
after = f' before: "{cursor}",' | |
query = """ | |
{ | |
repository(owner: "%s", name: "%s") { | |
pullRequests(last: 100, %s orderBy: {field: UPDATED_AT, direction: ASC}) { | |
edges { | |
cursor | |
node { | |
title | |
url | |
updatedAt | |
author { | |
login | |
} | |
labels(first: 50) { | |
edges { | |
node { | |
name | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
""" % ( | |
owner, | |
repo, | |
after, | |
) | |
data = graphql(query) | |
if data == None or data["repository"] == None: | |
return False | |
return data | |
def graphql_issues(owner, repo, date, cursor): | |
after = "" | |
if cursor: | |
after = f' after: "{cursor}",' | |
query = """ | |
{ | |
repository(owner: "%s", name: "%s") { | |
issues(filterBy: {since: "%s"} first: 100, %s orderBy: {field: UPDATED_AT, direction: ASC}) { | |
edges { | |
cursor | |
node { | |
title | |
url | |
updatedAt | |
comments(last: 20) { | |
edges { | |
node { | |
createdAt | |
author { | |
login | |
} | |
} | |
} | |
} | |
assignees(first: 50) { | |
edges { | |
node { | |
login | |
} | |
} | |
} | |
labels(first: 50) { | |
edges { | |
node { | |
name | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
""" % ( | |
owner, | |
repo, | |
date, | |
after, | |
) | |
data = graphql(query) | |
if data == None or data["repository"] == None: | |
return False | |
return data | |
def process_issue(area, team, issue): | |
i = issue["node"] | |
url = i["url"] | |
if len(i["comments"]["edges"]) > 0: | |
last_commenter = i["comments"]["edges"][-1]["node"]["author"]["login"] | |
if last_commenter == "botelastic": | |
return False | |
if not any([area, team]): | |
return url | |
for n in i["labels"]["edges"]: | |
if n["node"]["name"] == f"area:{area}": | |
return url | |
for a in i["assignees"]["edges"]: | |
if a["node"]["login"] in team: | |
return url | |
return False | |
def updated_since_date(updated_at, date): | |
updated = datetime.fromisoformat(updated_at[:-1]) >= datetime.fromisoformat( | |
date[:19] | |
) | |
return updated | |
def process_pr(area, team, pr, date): | |
p = pr["node"] | |
if not updated_since_date(p["updatedAt"], date): | |
return False | |
url = p["url"] | |
for n in p["labels"]["edges"]: | |
if n["node"]["name"] == f"area:{area}": | |
return url | |
if p["author"]["login"] in team: | |
return url | |
return False | |
def query_issues(org, repo): | |
issues = [] | |
cursor = "" | |
results = True | |
while results: | |
data = graphql_issues(org, repo, date, cursor) | |
i = data["repository"]["issues"]["edges"] | |
issues.extend(i) | |
if len(i) != 100: | |
results = False | |
else: | |
cursor = i[-1]["cursor"] | |
return issues | |
def query_prs(org, repo): | |
prs = [] | |
cursor = "" | |
results = True | |
while results: | |
data = graphql_prs(org, repo, cursor) | |
p = data["repository"]["pullRequests"]["edges"] | |
prs.extend(p) | |
if len(p) != 101: | |
results = False | |
else: | |
cursor = p[-1]["cursor"] | |
return prs | |
if __name__ == "__main__": | |
if len(sys.argv) == 1: | |
with open("/Users/nassimkammah/.githubdaily", "r") as tmpfile: | |
day = tmpfile.read().strip() | |
else: | |
day = sys.argv[1] + "T00" | |
date = f"{day}:00:00+0000" | |
dev_issues = query_issues("elastic", "infra") | |
print("Issues updates") | |
print('-' * 40) | |
for (repo, area) in gh_teams: | |
all_issues = query_issues("elastic", repo) | |
area_issues = [y for y in (process_issue(area, gh_teams[(repo, area)], i) for i in all_issues) if y] | |
print(f" - {area or repo:<10} : {len(area_issues)} issues") | |
if area_issues: | |
subprocess.Popen(f"open -na /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --args --new-window {' '.join(area_issues)}", shell = True) | |
print("\n\nPull Requests updates") | |
print('-' * 40) | |
for (repo, area) in gh_teams: | |
all_prs = query_prs("elastic", repo) | |
area_prs = [y for y in (process_pr(area, gh_teams[(repo, area)], pr, date) for pr in all_prs) if y] | |
print(f" - {area or repo:<10} : {len(area_prs)} PRs") | |
if area_prs: | |
subprocess.Popen(f"open -na /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --args --new-window {' '.join(area_prs)}", shell = True) | |
with open("/Users/nassimkammah/.githubdaily.last", "w") as tmpfile: | |
tmpfile.write(day) | |
with open("/Users/nassimkammah/.githubdaily", "w") as tmpfile: | |
day = datetime.strftime(datetime.utcnow(), "%Y-%m-%dT%H") | |
tmpfile.write(day) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment