aboutsummaryrefslogtreecommitdiffstats
path: root/bot/exts
diff options
context:
space:
mode:
Diffstat (limited to 'bot/exts')
-rw-r--r--bot/exts/halloween/hacktoberstats.py81
1 files changed, 69 insertions, 12 deletions
diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py
index be750dd4..2b46192f 100644
--- a/bot/exts/halloween/hacktoberstats.py
+++ b/bot/exts/halloween/hacktoberstats.py
@@ -239,7 +239,7 @@ class HacktoberStats(commands.Cog):
action_type = "pr"
is_query = "public"
not_query = "draft"
- date_range = f"{CURRENT_YEAR}-10-01T00:00:00%2B14:00..{CURRENT_YEAR}-10-31T23:59:59-11:00"
+ date_range = f"{CURRENT_YEAR}-10-01T00:00:00%2B14:00..{CURRENT_YEAR}-10-03T23:59:59-11:00"
per_page = "300"
query_url = (
f"{base_url}"
@@ -252,22 +252,54 @@ class HacktoberStats(commands.Cog):
f"+created:{date_range}"
f"&per_page={per_page}"
)
- logging.debug(f"GitHub query URL generated: {query_url}")
+ logging.debug(f"First GitHub query URL generated: {query_url}")
async with aiohttp.ClientSession() as session:
async with session.get(query_url, headers=REQUEST_HEADERS) as resp:
jsonresp = await resp.json()
- if "message" in jsonresp.keys():
- # One of the parameters is invalid, short circuit for now
- api_message = jsonresp["errors"][0]["message"]
+ if not _valid_github_response(jsonresp, github_username):
+ return
- # Ignore logging non-existent users or users we do not have permission to see
- if api_message == GITHUB_NONEXISTENT_USER_MESSAGE:
- logging.debug(f"No GitHub user found named '{github_username}'")
+ else:
+ if jsonresp["total_count"] == 0:
+ # Short circuit if there aren't any PRs
+ logging.info(f"No Hacktoberfest PRs found for GitHub user: '{github_username}'")
+ return
else:
- logging.error(f"GitHub API request for '{github_username}' failed with message: {api_message}")
+ # logging.info(f"Found {len(jsonresp['items'])} Hacktoberfest PRs for GitHub user: '{github_username}'")
+ outlist = []
+ for item in jsonresp["items"]:
+ shortname = HacktoberStats._get_shortname(item["repository_url"])
+ itemdict = {
+ "repo_url": f"https://www.github.com/{shortname}",
+ "repo_shortname": shortname,
+ "created_at": datetime.strptime(
+ item["created_at"], r"%Y-%m-%dT%H:%M:%SZ"
+ ),
+ }
+ outlist.append(itemdict)
+ # return outlist
+
+ date_range = f"{CURRENT_YEAR}-10-01T00:00:00%2B14:00..{CURRENT_YEAR}-11-01T23:59:59-11:00"
+ query_url = (
+ f"{base_url}"
+ f"-label:{not_labels[0]}"
+ f"+-label:{not_labels[1]}"
+ f"+type:{action_type}"
+ f"+is:{is_query}"
+ f"+author:{github_username}"
+ f"+-is:{not_query}"
+ f"+created:{date_range}"
+ f"&per_page={per_page}"
+ )
+ logging.debug(f"Second GitHub query URL generated: {query_url}")
+
+ async with aiohttp.ClientSession() as session:
+ async with session.get(query_url, headers=REQUEST_HEADERS) as resp:
+ jsonresp = await resp.json()
+ if not _valid_github_response(jsonresp, github_username):
return
else:
@@ -276,8 +308,6 @@ class HacktoberStats(commands.Cog):
logging.info(f"No Hacktoberfest PRs found for GitHub user: '{github_username}'")
return
else:
- logging.info(f"Found {len(jsonresp['items'])} Hacktoberfest PRs for GitHub user: '{github_username}'")
- outlist = []
for item in jsonresp["items"]:
shortname = HacktoberStats._get_shortname(item["repository_url"])
itemdict = {
@@ -287,10 +317,37 @@ class HacktoberStats(commands.Cog):
item["created_at"], r"%Y-%m-%dT%H:%M:%SZ"
),
}
- outlist.append(itemdict)
+ query_url = f"https://api.github.com/repos/{shortname}/topics"
+ accept_header = {"Accept": "application/vnd.github.mercy-preview+json"}
+
+ async with aiohttp.ClientSession() as session:
+ async with session.get(query_url, headers=accept_header) as resp:
+ jsonresp2 = await resp.json()
+
+ if not _valid_github_response(jsonresp2, github_username):
+ return
+
+ if ("hacktoberfest" in jsonresp2["names"]) or ("hacktoberfest-accpeted" in jsonresp["labels"]):
+ outlist.append(itemdict)
return outlist
@staticmethod
+ def _valid_github_response(jsonresp: str, username: str):
+ if "message" in jsonresp.keys():
+ # One of the parameters is invalid, short circuit for now
+ api_message = jsonresp["errors"][0]["message"]
+
+ # Ignore logging non-existent users or users we do not have permission to see
+ if api_message == GITHUB_NONEXISTENT_USER_MESSAGE:
+ logging.debug(f"No GitHub user found named '{username}'")
+ return False
+ else:
+ logging.error(f"GitHub API request for '{username}' failed with message: {api_message}")
+ return False
+
+ return True
+
+ @staticmethod
def _get_shortname(in_url: str) -> str:
"""
Extract shortname from https://api.github.com/repos/* URL.