From 9a5219bf62f9d502387a22701ca0c329088042f3 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Sat, 3 Oct 2020 16:06:57 +0800 Subject: exclude spam label in get_october_prs for hacktoberstats, prs with spam labels doesnt count --- bot/exts/halloween/hacktoberstats.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index ed1755e3..a6ed765e 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -162,7 +162,7 @@ class HacktoberStats(commands.Cog): """ Query GitHub's API for PRs created by a GitHub user during the month of October. - PRs with the 'invalid' tag are ignored + PRs with an 'invalid' or 'spam' label are ignored If a valid github_username is provided, an embed is generated and posted to the channel @@ -220,7 +220,7 @@ class HacktoberStats(commands.Cog): """ Query GitHub's API for PRs created during the month of October by github_username. - PRs with an 'invalid' tag are ignored + PRs with an 'invalid' or 'spam' label are ignored If PRs are found, return a list of dicts with basic PR information @@ -235,7 +235,7 @@ class HacktoberStats(commands.Cog): """ logging.info(f"Generating Hacktoberfest PR query for GitHub user: '{github_username}'") base_url = "https://api.github.com/search/issues?q=" - not_label = "invalid" + not_labels = ("invalid", "spam") action_type = "pr" is_query = f"public+author:{github_username}" not_query = "draft" @@ -243,7 +243,8 @@ class HacktoberStats(commands.Cog): per_page = "300" query_url = ( f"{base_url}" - f"-label:{not_label}" + f"-label:{not_labels[0]}" + f"-label:{not_labels[1]}" f"+type:{action_type}" f"+is:{is_query}" f"+-is:{not_query}" -- cgit v1.2.3 From c97d27e1faee410ee52e736143f5e925a7b68d04 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Mon, 5 Oct 2020 07:36:56 +0800 Subject: fix query syntax --- bot/exts/halloween/hacktoberstats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index a6ed765e..47b83023 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -244,7 +244,7 @@ class HacktoberStats(commands.Cog): query_url = ( f"{base_url}" f"-label:{not_labels[0]}" - f"-label:{not_labels[1]}" + f"+-label:{not_labels[1]}" f"+type:{action_type}" f"+is:{is_query}" f"+-is:{not_query}" -- cgit v1.2.3 From a5e5afff1a6422ff4fcd6685f81346083ffb120f Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Mon, 5 Oct 2020 08:02:05 +0800 Subject: improve no PR message --- bot/exts/halloween/hacktoberstats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 47b83023..2f10a8e8 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -175,7 +175,7 @@ class HacktoberStats(commands.Cog): stats_embed = self.build_embed(github_username, prs) await ctx.send('Here are some stats!', embed=stats_embed) else: - await ctx.send(f"No October GitHub contributions found for '{github_username}'") + await ctx.send(f"No valid October GitHub contributions found for '{github_username}'") def build_embed(self, github_username: str, prs: List[dict]) -> discord.Embed: """Return a stats embed built from github_username's PRs.""" -- cgit v1.2.3 From 65b2228595925fc13f3c3c73062be0494f53e622 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Mon, 5 Oct 2020 08:04:15 +0800 Subject: add logging generated url --- bot/exts/halloween/hacktoberstats.py | 1 + 1 file changed, 1 insertion(+) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 2f10a8e8..53f6d9dc 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -251,6 +251,7 @@ class HacktoberStats(commands.Cog): f"+created:{date_range}" f"&per_page={per_page}" ) + logging.info(f"Query URL generated: {query_url}") async with aiohttp.ClientSession() as session: async with session.get(query_url, headers=REQUEST_HEADERS) as resp: -- cgit v1.2.3 From adb383a84007eb2c84a63f9b1f9324fdd2fcfbcb Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Mon, 5 Oct 2020 16:41:27 +0800 Subject: update logging query url --- bot/exts/halloween/hacktoberstats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 53f6d9dc..af79ce7e 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -251,7 +251,7 @@ class HacktoberStats(commands.Cog): f"+created:{date_range}" f"&per_page={per_page}" ) - logging.info(f"Query URL generated: {query_url}") + logging.debug(f"GitHub query URL generated: {query_url}") async with aiohttp.ClientSession() as session: async with session.get(query_url, headers=REQUEST_HEADERS) as resp: -- cgit v1.2.3 From 1e21838a47365d319e9c305ed2ff515d9f6392ce Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Mon, 5 Oct 2020 16:43:05 +0800 Subject: update generation of query url moved `author:` outside of `is_query` for better readability and understanding --- bot/exts/halloween/hacktoberstats.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index af79ce7e..9d91a310 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -237,7 +237,7 @@ class HacktoberStats(commands.Cog): base_url = "https://api.github.com/search/issues?q=" not_labels = ("invalid", "spam") action_type = "pr" - is_query = f"public+author:{github_username}" + is_query = f"public" not_query = "draft" date_range = f"{CURRENT_YEAR}-10-01T00:00:00%2B14:00..{CURRENT_YEAR}-10-31T23:59:59-11:00" per_page = "300" @@ -247,6 +247,7 @@ class HacktoberStats(commands.Cog): 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}" -- cgit v1.2.3 From d8138538a66d69760c1eac36af05c93745c3e076 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Mon, 5 Oct 2020 18:51:25 +0800 Subject: fix for flake8 - no need fstring --- bot/exts/halloween/hacktoberstats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 9d91a310..be750dd4 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -237,7 +237,7 @@ class HacktoberStats(commands.Cog): base_url = "https://api.github.com/search/issues?q=" not_labels = ("invalid", "spam") action_type = "pr" - is_query = f"public" + 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" per_page = "300" -- cgit v1.2.3 From d438b275ea13a3ab3118e6d02869e16a8ef64ffe Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Wed, 7 Oct 2020 08:43:21 +0000 Subject: WIP: update stats fetching logic - draft code" --- bot/exts/halloween/hacktoberstats.py | 81 ++++++++++++++++++++++++++++++------ 1 file changed, 69 insertions(+), 12 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') 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,9 +317,36 @@ 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: """ -- cgit v1.2.3 From 8c754b158be92c119126ba86ea73510aa93a0e57 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Wed, 7 Oct 2020 11:01:22 +0000 Subject: almost finished --- bot/exts/halloween/hacktoberstats.py | 119 +++++++++++++---------------------- 1 file changed, 42 insertions(+), 77 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 2b46192f..be4534b4 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -29,6 +29,8 @@ GITHUB_NONEXISTENT_USER_MESSAGE = ( "or you do not have permission to view the users." ) +GITHUB_TOPICS_ACCEPT_HEADER = {"Accept": "application/vnd.github.mercy-preview+json"} + class HacktoberStats(commands.Cog): """Hacktoberfest statistics Cog.""" @@ -239,7 +241,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-03T23:59:59-11:00" + date_range = f"{CURRENT_YEAR}-10-01T00:00:00%2B14:00..{CURRENT_YEAR}-11-01T00:00:00-11:00" per_page = "300" query_url = ( f"{base_url}" @@ -252,87 +254,12 @@ class HacktoberStats(commands.Cog): f"+created:{date_range}" f"&per_page={per_page}" ) - logging.debug(f"First GitHub query URL generated: {query_url}") + logging.debug(f"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: - 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.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: - 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: - 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" - ), - } - 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"] @@ -347,6 +274,44 @@ class HacktoberStats(commands.Cog): return True + + 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.info(f"Found {len(jsonresp['items'])} Hacktoberfest PRs for GitHub user: '{github_username}'") + outlist = [] + oct3 = datetime(int(CURRENT_YEAR), 10, 3, 0, 0, 0) + topics_query_url = f"https://api.github.com/repos/{shortname}/topics" + 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) + + async with aiohttp.ClientSession() as session: + async with session.get(topics_query_url, headers=GITHUB_TOPICS_ACCEPT_HEADER) as resp: + jsonresp2 = await resp.json() + + if not _valid_github_response(jsonresp2, github_username): + return + + # PRs before oct 3 without invalid/spam willl count + if itemdict["created_at"] < oct3: + outlist.append(itemdict) + continue + # PRs after must be in repo with 'hacktoberfest' topic + # unless it has 'hacktoberfest-accepted' label + if ("hacktoberfest" in jsonresp2["names"]) or ("hacktoberfest-accpeted" in jsonresp["labels"]): + outlist.append(itemdict) + return outlist + @staticmethod def _get_shortname(in_url: str) -> str: """ -- cgit v1.2.3 From 2f13cc65d8ec14f40f9c8bbd52edddf91038b912 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Wed, 7 Oct 2020 11:01:22 +0000 Subject: almost finished --- bot/exts/halloween/hacktoberstats.py | 119 +++++++++++++---------------------- 1 file changed, 42 insertions(+), 77 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 2b46192f..be4534b4 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -29,6 +29,8 @@ GITHUB_NONEXISTENT_USER_MESSAGE = ( "or you do not have permission to view the users." ) +GITHUB_TOPICS_ACCEPT_HEADER = {"Accept": "application/vnd.github.mercy-preview+json"} + class HacktoberStats(commands.Cog): """Hacktoberfest statistics Cog.""" @@ -239,7 +241,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-03T23:59:59-11:00" + date_range = f"{CURRENT_YEAR}-10-01T00:00:00%2B14:00..{CURRENT_YEAR}-11-01T00:00:00-11:00" per_page = "300" query_url = ( f"{base_url}" @@ -252,87 +254,12 @@ class HacktoberStats(commands.Cog): f"+created:{date_range}" f"&per_page={per_page}" ) - logging.debug(f"First GitHub query URL generated: {query_url}") + logging.debug(f"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: - 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.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: - 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: - 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" - ), - } - 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"] @@ -347,6 +274,44 @@ class HacktoberStats(commands.Cog): return True + + 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.info(f"Found {len(jsonresp['items'])} Hacktoberfest PRs for GitHub user: '{github_username}'") + outlist = [] + oct3 = datetime(int(CURRENT_YEAR), 10, 3, 0, 0, 0) + topics_query_url = f"https://api.github.com/repos/{shortname}/topics" + 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) + + async with aiohttp.ClientSession() as session: + async with session.get(topics_query_url, headers=GITHUB_TOPICS_ACCEPT_HEADER) as resp: + jsonresp2 = await resp.json() + + if not _valid_github_response(jsonresp2, github_username): + return + + # PRs before oct 3 without invalid/spam willl count + if itemdict["created_at"] < oct3: + outlist.append(itemdict) + continue + # PRs after must be in repo with 'hacktoberfest' topic + # unless it has 'hacktoberfest-accepted' label + if ("hacktoberfest" in jsonresp2["names"]) or ("hacktoberfest-accpeted" in jsonresp["labels"]): + outlist.append(itemdict) + return outlist + @staticmethod def _get_shortname(in_url: str) -> str: """ -- cgit v1.2.3 From 2847ec637cd7eec23d781692bc03b3323bfdf4ec Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Thu, 8 Oct 2020 00:43:50 +0000 Subject: small fixes, still need testing --- bot/exts/halloween/hacktoberstats.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index be4534b4..76afde46 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -266,15 +266,14 @@ class HacktoberStats(commands.Cog): # 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}'") + logging.debug(f"No GitHub user found named '{github_username}'") return False else: - logging.error(f"GitHub API request for '{username}' failed with message: {api_message}") + logging.error(f"GitHub API request for '{github_username}' failed with message: {api_message}") return False - + return True - 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}'") @@ -283,7 +282,7 @@ class HacktoberStats(commands.Cog): # logging.info(f"Found {len(jsonresp['items'])} Hacktoberfest PRs for GitHub user: '{github_username}'") outlist = [] oct3 = datetime(int(CURRENT_YEAR), 10, 3, 0, 0, 0) - topics_query_url = f"https://api.github.com/repos/{shortname}/topics" + for item in jsonresp["items"]: shortname = HacktoberStats._get_shortname(item["repository_url"]) itemdict = { @@ -293,25 +292,26 @@ class HacktoberStats(commands.Cog): item["created_at"], r"%Y-%m-%dT%H:%M:%SZ" ), } - # outlist.append(itemdict) - - async with aiohttp.ClientSession() as session: - async with session.get(topics_query_url, headers=GITHUB_TOPICS_ACCEPT_HEADER) as resp: - jsonresp2 = await resp.json() - - if not _valid_github_response(jsonresp2, github_username): - return - # PRs before oct 3 without invalid/spam willl count + # PRs before oct 3 without invalid/spam will count if itemdict["created_at"] < oct3: outlist.append(itemdict) continue + + topics_query_url = f"https://api.github.com/repos/{shortname}/topics" + async with aiohttp.ClientSession() as session: + async with session.get(topics_query_url, headers=GITHUB_TOPICS_ACCEPT_HEADER) as resp: + jsonresp2 = await resp.json() + + if not ("names" in jsonresp2.keys()): + log.error("Error fetching topics for " + shortname + ": " + jsonresp2["message"]) + # PRs after must be in repo with 'hacktoberfest' topic # unless it has 'hacktoberfest-accepted' label if ("hacktoberfest" in jsonresp2["names"]) or ("hacktoberfest-accpeted" in jsonresp["labels"]): outlist.append(itemdict) return outlist - + @staticmethod def _get_shortname(in_url: str) -> str: """ -- cgit v1.2.3 From 1e12cc720b4db72a8b76f19f21456f10a6ef4f8e Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Thu, 8 Oct 2020 03:31:46 +0000 Subject: fix for when pr has no labels --- bot/exts/halloween/hacktoberstats.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 50608d56..f4b8f51c 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -298,6 +298,7 @@ class HacktoberStats(commands.Cog): continue topics_query_url = f"https://api.github.com/repos/{shortname}/topics" + log.debug("Fetching repo topics for " + shortname + " with url: " + topics_query_url) async with aiohttp.ClientSession() as session: async with session.get(topics_query_url, headers=GITHUB_TOPICS_ACCEPT_HEADER) as resp: jsonresp2 = await resp.json() @@ -307,6 +308,8 @@ class HacktoberStats(commands.Cog): # PRs after must be in repo with 'hacktoberfest' topic # unless it has 'hacktoberfest-accepted' label + if not ("labels" in jsonresp.keys()): + continue if ("hacktoberfest" in jsonresp2["names"]) or ("hacktoberfest-accpeted" in jsonresp["labels"]): outlist.append(itemdict) return outlist -- cgit v1.2.3 From 1f6d9fbf3535ca4a2c77f4a69f3211c7ec8072f3 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Thu, 8 Oct 2020 03:34:13 +0000 Subject: return instead of return boolean --- bot/exts/halloween/hacktoberstats.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index f4b8f51c..fc66bfa8 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -267,12 +267,10 @@ class HacktoberStats(commands.Cog): # 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}'") - return False + return else: logging.error(f"GitHub API request for '{github_username}' failed with message: {api_message}") - return False - - return True + return if jsonresp["total_count"] == 0: # Short circuit if there aren't any PRs -- cgit v1.2.3 From ff9abdaad65000ef8b56ed21e58bca2ac227773e Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Thu, 8 Oct 2020 03:36:35 +0000 Subject: refactor if else --- bot/exts/halloween/hacktoberstats.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index fc66bfa8..4f4ea122 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -267,10 +267,9 @@ class HacktoberStats(commands.Cog): # 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}'") - return else: logging.error(f"GitHub API request for '{github_username}' failed with message: {api_message}") - return + return if jsonresp["total_count"] == 0: # Short circuit if there aren't any PRs -- cgit v1.2.3 From 9864520a4b91ab42a694f0db5a9d1589c90a5c7e Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Thu, 8 Oct 2020 03:38:41 +0000 Subject: fix logging --- bot/exts/halloween/hacktoberstats.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 4f4ea122..2a81b91b 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -276,7 +276,7 @@ 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}'") + logging.info(f"Found {len(jsonresp['items'])} Hacktoberfest PRs for GitHub user: '{github_username}'") outlist = [] oct3 = datetime(int(CURRENT_YEAR), 10, 3, 0, 0, 0) for item in jsonresp["items"]: @@ -295,13 +295,13 @@ class HacktoberStats(commands.Cog): continue topics_query_url = f"https://api.github.com/repos/{shortname}/topics" - log.debug("Fetching repo topics for " + shortname + " with url: " + topics_query_url) + logging.debug("Fetching repo topics for " + shortname + " with url: " + topics_query_url) async with aiohttp.ClientSession() as session: async with session.get(topics_query_url, headers=GITHUB_TOPICS_ACCEPT_HEADER) as resp: jsonresp2 = await resp.json() if not ("names" in jsonresp2.keys()): - log.error("Error fetching topics for " + shortname + ": " + jsonresp2["message"]) + logging.error("Error fetching topics for " + shortname + ": " + jsonresp2["message"]) # PRs after must be in repo with 'hacktoberfest' topic # unless it has 'hacktoberfest-accepted' label -- cgit v1.2.3 From 3f14e0999bb8cde85db530fbb939e3355f9ff597 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Thu, 8 Oct 2020 03:45:10 +0000 Subject: updated comments --- bot/exts/halloween/hacktoberstats.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 2a81b91b..c5c4b383 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -289,11 +289,12 @@ class HacktoberStats(commands.Cog): ), } - # PRs before oct 3 without invalid/spam will count + # PRs before oct 3 no need to check for topics if itemdict["created_at"] < oct3: outlist.append(itemdict) continue + # fetch topics for the pr repo topics_query_url = f"https://api.github.com/repos/{shortname}/topics" logging.debug("Fetching repo topics for " + shortname + " with url: " + topics_query_url) async with aiohttp.ClientSession() as session: @@ -303,7 +304,7 @@ class HacktoberStats(commands.Cog): if not ("names" in jsonresp2.keys()): logging.error("Error fetching topics for " + shortname + ": " + jsonresp2["message"]) - # PRs after must be in repo with 'hacktoberfest' topic + # PRs after oct 3 must be in repo with 'hacktoberfest' topic # unless it has 'hacktoberfest-accepted' label if not ("labels" in jsonresp.keys()): continue -- cgit v1.2.3 From 7092b8933968b439ae9ae406ae78d43fd7531cc9 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Thu, 8 Oct 2020 03:54:24 +0000 Subject: refactor --- bot/exts/halloween/hacktoberstats.py | 70 ++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 35 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index c5c4b383..2258dd70 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -275,42 +275,42 @@ class HacktoberStats(commands.Cog): # Short circuit if there aren't any PRs 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 = [] - oct3 = datetime(int(CURRENT_YEAR), 10, 3, 0, 0, 0) - 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" - ), - } - # PRs before oct 3 no need to check for topics - if itemdict["created_at"] < oct3: - outlist.append(itemdict) - continue - - # fetch topics for the pr repo - topics_query_url = f"https://api.github.com/repos/{shortname}/topics" - logging.debug("Fetching repo topics for " + shortname + " with url: " + topics_query_url) - async with aiohttp.ClientSession() as session: - async with session.get(topics_query_url, headers=GITHUB_TOPICS_ACCEPT_HEADER) as resp: - jsonresp2 = await resp.json() - - if not ("names" in jsonresp2.keys()): - logging.error("Error fetching topics for " + shortname + ": " + jsonresp2["message"]) - - # PRs after oct 3 must be in repo with 'hacktoberfest' topic - # unless it has 'hacktoberfest-accepted' label - if not ("labels" in jsonresp.keys()): - continue - if ("hacktoberfest" in jsonresp2["names"]) or ("hacktoberfest-accpeted" in jsonresp["labels"]): - outlist.append(itemdict) - return outlist + logging.info(f"Found {len(jsonresp['items'])} Hacktoberfest PRs for GitHub user: '{github_username}'") + outlist = [] + oct3 = datetime(int(CURRENT_YEAR), 10, 3, 0, 0, 0) + 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" + ), + } + + # PRs before oct 3 no need to check for topics + if itemdict["created_at"] < oct3: + outlist.append(itemdict) + continue + + # fetch topics for the pr repo + topics_query_url = f"https://api.github.com/repos/{shortname}/topics" + logging.debug("Fetching repo topics for " + shortname + " with url: " + topics_query_url) + async with aiohttp.ClientSession() as session: + async with session.get(topics_query_url, headers=GITHUB_TOPICS_ACCEPT_HEADER) as resp: + jsonresp2 = await resp.json() + + if not ("names" in jsonresp2.keys()): + logging.error("Error fetching topics for " + shortname + ": " + jsonresp2["message"]) + + # PRs after oct 3 must be in repo with 'hacktoberfest' topic + # unless it has 'hacktoberfest-accepted' label + if not ("labels" in jsonresp.keys()): + continue + if ("hacktoberfest" in jsonresp2["names"]) or ("hacktoberfest-accpeted" in jsonresp["labels"]): + outlist.append(itemdict) + return outlist @staticmethod def _get_shortname(in_url: str) -> str: -- cgit v1.2.3 From 8994e4eb1e6d38fad14e8d7cd20c6545d3356954 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Thu, 8 Oct 2020 08:15:01 +0000 Subject: update docstr and comments --- bot/exts/halloween/hacktoberstats.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 2258dd70..4f5436af 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -29,6 +29,7 @@ GITHUB_NONEXISTENT_USER_MESSAGE = ( "or you do not have permission to view the users." ) +# using repo topics API during preview period requires an accept header GITHUB_TOPICS_ACCEPT_HEADER = {"Accept": "application/vnd.github.mercy-preview+json"} @@ -166,6 +167,10 @@ class HacktoberStats(commands.Cog): PRs with an 'invalid' or 'spam' label are ignored + For PRs created after October 3rd, they have to be in a repository that has a + 'hacktoberfest' topic, unless the PR is labelled 'hacktoberfest-accepted' for it + to count. + If a valid github_username is provided, an embed is generated and posted to the channel Otherwise, post a helpful error message @@ -224,6 +229,10 @@ class HacktoberStats(commands.Cog): PRs with an 'invalid' or 'spam' label are ignored + For PRs created after October 3rd, they have to be in a repository that has a + 'hacktoberfest' topic, unless the PR is labelled 'hacktoberfest-accepted' for it + to count. + If PRs are found, return a list of dicts with basic PR information For each PR: @@ -277,7 +286,7 @@ class HacktoberStats(commands.Cog): return logging.info(f"Found {len(jsonresp['items'])} Hacktoberfest PRs for GitHub user: '{github_username}'") - outlist = [] + outlist = [] # list of pr information dicts that will get returned oct3 = datetime(int(CURRENT_YEAR), 10, 3, 0, 0, 0) for item in jsonresp["items"]: shortname = HacktoberStats._get_shortname(item["repository_url"]) @@ -296,17 +305,17 @@ class HacktoberStats(commands.Cog): # fetch topics for the pr repo topics_query_url = f"https://api.github.com/repos/{shortname}/topics" - logging.debug("Fetching repo topics for " + shortname + " with url: " + topics_query_url) + logging.debug(f"Fetching repo topics for {shortname} with url: {topics_query_url}") async with aiohttp.ClientSession() as session: async with session.get(topics_query_url, headers=GITHUB_TOPICS_ACCEPT_HEADER) as resp: jsonresp2 = await resp.json() if not ("names" in jsonresp2.keys()): - logging.error("Error fetching topics for " + shortname + ": " + jsonresp2["message"]) + logging.error(f"Error fetching topics for {shortname}: {jsonresp2['message']}") # PRs after oct 3 must be in repo with 'hacktoberfest' topic # unless it has 'hacktoberfest-accepted' label - if not ("labels" in jsonresp.keys()): + if not ("labels" in jsonresp.keys()): # if PR has no labels continue if ("hacktoberfest" in jsonresp2["names"]) or ("hacktoberfest-accpeted" in jsonresp["labels"]): outlist.append(itemdict) -- cgit v1.2.3 From 6a1ddc00d553074655613f0972815da3579f50e8 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Thu, 8 Oct 2020 08:36:37 +0000 Subject: fix topics and label logic --- bot/exts/halloween/hacktoberstats.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 4f5436af..ab12bf1e 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -299,7 +299,15 @@ class HacktoberStats(commands.Cog): } # PRs before oct 3 no need to check for topics - if itemdict["created_at"] < oct3: + # continue the loop if 'hacktoberfest-accepted' is labeled then + # there is no need to check for its topics + if (itemdict["created_at"] < oct3): + outlist.append(itemdict) + continue + if not ("labels" in item.keys()): # if PR has no labels + continue + # checking whether "hacktoberfest-accepted" is one of the PR's labels + if any(label["name"].casefold() == "hacktoberfest-accepted" for label in item["labels"]): outlist.append(itemdict) continue @@ -313,11 +321,9 @@ class HacktoberStats(commands.Cog): if not ("names" in jsonresp2.keys()): logging.error(f"Error fetching topics for {shortname}: {jsonresp2['message']}") - # PRs after oct 3 must be in repo with 'hacktoberfest' topic - # unless it has 'hacktoberfest-accepted' label - if not ("labels" in jsonresp.keys()): # if PR has no labels - continue - if ("hacktoberfest" in jsonresp2["names"]) or ("hacktoberfest-accpeted" in jsonresp["labels"]): + # PRs after oct 3 that doesn't have 'hacktoberfest-accepted' label + # must be in repo with 'hacktoberfest' topic + if "hacktoberfest" in jsonresp2["names"]: outlist.append(itemdict) return outlist -- cgit v1.2.3 From 8f43a0bd261e200e49562bed109eadf3219934f8 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Fri, 9 Oct 2020 08:52:40 +0000 Subject: update return type for `get_october_prs` it returns None when no PRs found, so I put that in `Union` with `List[dict]` --- bot/exts/halloween/hacktoberstats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index ab12bf1e..7db2d86a 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -223,7 +223,7 @@ class HacktoberStats(commands.Cog): return stats_embed @staticmethod - async def get_october_prs(github_username: str) -> List[dict]: + async def get_october_prs(github_username: str) -> Union[List[dict], None]: """ Query GitHub's API for PRs created during the month of October by github_username. -- cgit v1.2.3 From 6d79f76b57d9fab52a32c68c572db4f8a7175da1 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Fri, 9 Oct 2020 09:00:39 +0000 Subject: forgot to import --- bot/exts/halloween/hacktoberstats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 7db2d86a..e77a71f1 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -4,7 +4,7 @@ import re from collections import Counter from datetime import datetime from pathlib import Path -from typing import List, Tuple +from typing import List, Tuple, Union import aiohttp import discord -- cgit v1.2.3 From 21c174768a570b09d948e98b7a377323597a7735 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Fri, 16 Oct 2020 02:52:04 +0000 Subject: display in-review, accepted separately --- bot/exts/halloween/hacktoberstats.py | 70 +++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 29 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index e77a71f1..dae70e3d 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -1,8 +1,8 @@ import json import logging import re -from collections import Counter -from datetime import datetime +# from collections import Counter +from datetime import datetime, timedelta from pathlib import Path from typing import List, Tuple, Union @@ -32,6 +32,8 @@ GITHUB_NONEXISTENT_USER_MESSAGE = ( # using repo topics API during preview period requires an accept header GITHUB_TOPICS_ACCEPT_HEADER = {"Accept": "application/vnd.github.mercy-preview+json"} +REVIEW_DAYS = 14 + class HacktoberStats(commands.Cog): """Hacktoberfest statistics Cog.""" @@ -187,9 +189,11 @@ class HacktoberStats(commands.Cog): def build_embed(self, github_username: str, prs: List[dict]) -> discord.Embed: """Return a stats embed built from github_username's PRs.""" logging.info(f"Building Hacktoberfest embed for GitHub user: '{github_username}'") - pr_stats = self._summarize_prs(prs) + prs_dict = self._categorize_prs(prs) + accepted = prs_dict['accepted'] + in_review = prs_dict['in_review'] - n = pr_stats['n_prs'] + n = len(accepted) + len(in_review) if n >= PRS_FOR_SHIRT: shirtstr = f"**{github_username} has earned a T-shirt or a tree!**" elif n == PRS_FOR_SHIRT - 1: @@ -201,7 +205,7 @@ class HacktoberStats(commands.Cog): title=f"{github_username}'s Hacktoberfest", color=discord.Color(0x9c4af7), description=( - f"{github_username} has made {n} " + f"{github_username} has made {n} eligible" f"{HacktoberStats._contributionator(n)} in " f"October\n\n" f"{shirtstr}\n\n" @@ -215,8 +219,12 @@ class HacktoberStats(commands.Cog): icon_url="https://avatars1.githubusercontent.com/u/35706162?s=200&v=4" ) stats_embed.add_field( - name="Top 5 Repositories:", - value=self._build_top5str(pr_stats) + name="In Review", + value=self._build_prs_string(in_review, github_username) + ) + stats_embed.add_field( + name="Accepted", + value=self._build_prs_string(accepted, github_username) ) logging.info(f"Hacktoberfest PR built for GitHub user '{github_username}'") @@ -341,38 +349,42 @@ class HacktoberStats(commands.Cog): return re.findall(exp, in_url)[0] @staticmethod - def _summarize_prs(prs: List[dict]) -> dict: + def _categorize_prs(prs: List[dict]) -> dict: """ - Generate statistics from an input list of PR dictionaries, as output by get_october_prs. + Categorize PRs into 'in_review' and 'accepted'. - Return a dictionary containing: - { - "n_prs": int - "top5": [(repo_shortname, ncontributions), ...] - } + PRs created less than 14 days ago are 'in_review', PRs that are not + are 'accepted' (after 14 days review period). """ - contributed_repos = [pr["repo_shortname"] for pr in prs] - return {"n_prs": len(prs), "top5": Counter(contributed_repos).most_common(5)} + now = datetime.now() + in_review = [] + accepted = [] + for pr in prs: + if (pr['created_at'] + timedelta(REVIEW_DAYS)) < now: + in_review.append(pr) + else: + accepted.append(pr) + + out_dict = { + "in_review": in_review, + "accepted": accepted + } + return out_dict @staticmethod - def _build_top5str(stats: List[tuple]) -> str: + def _build_prs_string(prs: List[tuple], user: str) -> str: """ - Build a string from the Top 5 contributions that is compatible with a discord.Embed field. - - Top 5 contributions should be a list of tuples, as output in the stats dictionary by - _summarize_prs + Builds a discord embed compatible string for a list of PRs. - String is of the form: - n contribution(s) to [shortname](url) - ... + Repository name with the link to pull requests authored by 'user' for + each PR. """ base_url = "https://www.github.com/" - contributionstrs = [] - for repo in stats['top5']: - n = repo[1] - contributionstrs.append(f"{n} {HacktoberStats._contributionator(n)} to [{repo[0]}]({base_url}{repo[0]})") + str_list = [] + for pr in prs: + str_list.append(f"[{pr['repo_shortname']}]({base_url}{pr['repo_shortname']}/pulls/{user})") - return "\n".join(contributionstrs) + return "\n".join(str_list) @staticmethod def _contributionator(n: int) -> str: -- cgit v1.2.3 From 835b6ab8f0364a5e6cc550a1a8878d78549d4d26 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Fri, 16 Oct 2020 04:40:32 +0000 Subject: add n prs count and max number --- bot/exts/halloween/hacktoberstats.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index dae70e3d..755888d7 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -1,7 +1,7 @@ import json import logging import re -# from collections import Counter +from collections import Counter from datetime import datetime, timedelta from pathlib import Path from typing import List, Tuple, Union @@ -193,9 +193,9 @@ class HacktoberStats(commands.Cog): accepted = prs_dict['accepted'] in_review = prs_dict['in_review'] - n = len(accepted) + len(in_review) + n = len(accepted) + len(in_review) # total number of PRs if n >= PRS_FOR_SHIRT: - shirtstr = f"**{github_username} has earned a T-shirt or a tree!**" + shirtstr = f"**{github_username} is eligible for a T-shirt or a tree!**" elif n == PRS_FOR_SHIRT - 1: shirtstr = f"**{github_username} is 1 PR away from a T-shirt or a tree!**" else: @@ -205,7 +205,7 @@ class HacktoberStats(commands.Cog): title=f"{github_username}'s Hacktoberfest", color=discord.Color(0x9c4af7), description=( - f"{github_username} has made {n} eligible" + f"{github_username} has made {n} valid " f"{HacktoberStats._contributionator(n)} in " f"October\n\n" f"{shirtstr}\n\n" @@ -381,8 +381,18 @@ class HacktoberStats(commands.Cog): """ base_url = "https://www.github.com/" str_list = [] - for pr in prs: - str_list.append(f"[{pr['repo_shortname']}]({base_url}{pr['repo_shortname']}/pulls/{user})") + repo_list = [pr["repo_shortname"] for pr in prs] + prs_list = Counter(repo_list).most_common(5) # get first 5 counted PRs + more = len(prs) - sum(i[1] for i in prs_list) + + for pr in prs_list: + # for example: https://www.github.com/python-discord/bot/pulls/octocat + # will display pull requests authored by octocat. + # pr[1] is the number of PRs to the repo + string = f"[{pr[0]}]({base_url}{pr[0]}/pulls/{user}) ({pr[1]})" + str_list.append(string) + if more: + str_list.append(f"...and {more} more") return "\n".join(str_list) -- cgit v1.2.3 From ad8a59a0e38d6e2a35552b7300a0880c739ab218 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Fri, 16 Oct 2020 05:20:38 +0000 Subject: update calling static methods --- bot/exts/halloween/hacktoberstats.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 755888d7..ebce9c87 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -18,6 +18,7 @@ log = logging.getLogger(__name__) CURRENT_YEAR = datetime.now().year # Used to construct GH API query PRS_FOR_SHIRT = 4 # Minimum number of PRs before a shirt is awarded +REVIEW_DAYS = 14 # number of days needed after PR can be mature HACKTOBER_WHITELIST = WHITELISTED_CHANNELS + (Channels.hacktoberfest_2020,) REQUEST_HEADERS = {"User-Agent": "Python Discord Hacktoberbot"} @@ -32,8 +33,6 @@ GITHUB_NONEXISTENT_USER_MESSAGE = ( # using repo topics API during preview period requires an accept header GITHUB_TOPICS_ACCEPT_HEADER = {"Accept": "application/vnd.github.mercy-preview+json"} -REVIEW_DAYS = 14 - class HacktoberStats(commands.Cog): """Hacktoberfest statistics Cog.""" @@ -55,7 +54,7 @@ class HacktoberStats(commands.Cog): get that user's contributions """ if not github_username: - author_id, author_mention = HacktoberStats._author_mention_from_context(ctx) + author_id, author_mention = self._author_mention_from_context(ctx) if str(author_id) in self.linked_accounts.keys(): github_username = self.linked_accounts[author_id]["github_username"] @@ -86,7 +85,7 @@ class HacktoberStats(commands.Cog): } } """ - author_id, author_mention = HacktoberStats._author_mention_from_context(ctx) + author_id, author_mention = self._author_mention_from_context(ctx) if github_username: if str(author_id) in self.linked_accounts.keys(): old_username = self.linked_accounts[author_id]["github_username"] @@ -111,7 +110,7 @@ class HacktoberStats(commands.Cog): @override_in_channel(HACKTOBER_WHITELIST) async def unlink_user(self, ctx: commands.Context) -> None: """Remove the invoking user's account link from the log.""" - author_id, author_mention = HacktoberStats._author_mention_from_context(ctx) + author_id, author_mention = self._author_mention_from_context(ctx) stored_user = self.linked_accounts.pop(author_id, None) if stored_user: @@ -206,7 +205,7 @@ class HacktoberStats(commands.Cog): color=discord.Color(0x9c4af7), description=( f"{github_username} has made {n} valid " - f"{HacktoberStats._contributionator(n)} in " + f"{self._contributionator(n)} in " f"October\n\n" f"{shirtstr}\n\n" ) -- cgit v1.2.3 From 2e049eec4bd7d18a317c0453600045f707a3db2d Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Fri, 16 Oct 2020 05:24:11 +0000 Subject: handle no PR for a section --- bot/exts/halloween/hacktoberstats.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index ebce9c87..6843ded2 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -217,13 +217,17 @@ class HacktoberStats(commands.Cog): url="https://hacktoberfest.digitalocean.com", icon_url="https://avatars1.githubusercontent.com/u/35706162?s=200&v=4" ) + + # this will handle when no PRs in_review or accepted + review_str = self._build_prs_string(in_review, github_username) or "None" + accepted_str = self._build_prs_string(accepted, github_username) or "None" stats_embed.add_field( name="In Review", - value=self._build_prs_string(in_review, github_username) + value=review_str ) stats_embed.add_field( name="Accepted", - value=self._build_prs_string(accepted, github_username) + value=accepted_str ) logging.info(f"Hacktoberfest PR built for GitHub user '{github_username}'") -- cgit v1.2.3 From 608025d3e16846cc9192ef02d77bff466810f06a Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Fri, 16 Oct 2020 08:05:33 +0000 Subject: minor design changes --- bot/exts/halloween/hacktoberstats.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 6843ded2..218505fb 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -222,11 +222,11 @@ class HacktoberStats(commands.Cog): review_str = self._build_prs_string(in_review, github_username) or "None" accepted_str = self._build_prs_string(accepted, github_username) or "None" stats_embed.add_field( - name="In Review", + name=":clock1: In Review", value=review_str ) stats_embed.add_field( - name="Accepted", + name=":tada: Accepted", value=accepted_str ) @@ -392,7 +392,7 @@ class HacktoberStats(commands.Cog): # for example: https://www.github.com/python-discord/bot/pulls/octocat # will display pull requests authored by octocat. # pr[1] is the number of PRs to the repo - string = f"[{pr[0]}]({base_url}{pr[0]}/pulls/{user}) ({pr[1]})" + string = f"{pr[1]} to [{pr[0]}]({base_url}{pr[0]}/pulls/{user})" str_list.append(string) if more: str_list.append(f"...and {more} more") -- cgit v1.2.3 From 747893309206ea397585eea27413a4f6b6bd0ff5 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Fri, 16 Oct 2020 09:59:37 +0000 Subject: fix in_review and accepted PRs swapped --- bot/exts/halloween/hacktoberstats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 218505fb..dbe83ad1 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -363,7 +363,7 @@ class HacktoberStats(commands.Cog): in_review = [] accepted = [] for pr in prs: - if (pr['created_at'] + timedelta(REVIEW_DAYS)) < now: + if (pr['created_at'] + timedelta(REVIEW_DAYS)) > now: in_review.append(pr) else: accepted.append(pr) -- cgit v1.2.3 From 2eb5da7c93886fde7d2979065006cb295cadd05e Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Sat, 17 Oct 2020 07:38:32 +0000 Subject: almost done, needs testing --- bot/exts/halloween/hacktoberstats.py | 118 +++++++++++++++++++++++++---------- 1 file changed, 86 insertions(+), 32 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index dbe83ad1..b0b64be9 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -188,9 +188,7 @@ class HacktoberStats(commands.Cog): def build_embed(self, github_username: str, prs: List[dict]) -> discord.Embed: """Return a stats embed built from github_username's PRs.""" logging.info(f"Building Hacktoberfest embed for GitHub user: '{github_username}'") - prs_dict = self._categorize_prs(prs) - accepted = prs_dict['accepted'] - in_review = prs_dict['in_review'] + in_review, accepted = self._categorize_prs(prs) n = len(accepted) + len(in_review) # total number of PRs if n >= PRS_FOR_SHIRT: @@ -238,7 +236,7 @@ class HacktoberStats(commands.Cog): """ Query GitHub's API for PRs created during the month of October by github_username. - PRs with an 'invalid' or 'spam' label are ignored + PRs with an 'invalid' or 'spam' label are ignored unless it is merged or approved For PRs created after October 3rd, they have to be in a repository that has a 'hacktoberfest' topic, unless the PR is labelled 'hacktoberfest-accepted' for it @@ -247,17 +245,17 @@ class HacktoberStats(commands.Cog): If PRs are found, return a list of dicts with basic PR information For each PR: - { + { "repo_url": str "repo_shortname": str (e.g. "python-discord/seasonalbot") "created_at": datetime.datetime - } + "number": int + } Otherwise, return None """ - logging.info(f"Generating Hacktoberfest PR query for GitHub user: '{github_username}'") + logging.info(f"Fetching Hacktoberfest Stats for GitHub user: '{github_username}'") base_url = "https://api.github.com/search/issues?q=" - not_labels = ("invalid", "spam") action_type = "pr" is_query = "public" not_query = "draft" @@ -265,8 +263,6 @@ class HacktoberStats(commands.Cog): per_page = "300" 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}" @@ -276,10 +272,7 @@ class HacktoberStats(commands.Cog): ) logging.debug(f"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() - + jsonresp = await HacktoberStats._fetch_url(query_url, REQUEST_HEADERS) if "message" in jsonresp.keys(): # One of the parameters is invalid, short circuit for now api_message = jsonresp["errors"][0]["message"] @@ -307,28 +300,31 @@ class HacktoberStats(commands.Cog): "created_at": datetime.strptime( item["created_at"], r"%Y-%m-%dT%H:%M:%SZ" ), + "number": item["number"] } + # if the PR has 'invalid' or 'spam' labels, the PR must be + # either merged or approved for it to be included + if HacktoberStats._has_label(item, ["invalid", "spam"]): + if not HacktoberStats._is_accepted(item): + continue + # PRs before oct 3 no need to check for topics - # continue the loop if 'hacktoberfest-accepted' is labeled then + # continue the loop if 'hacktoberfest-accepted' is labelled then # there is no need to check for its topics - if (itemdict["created_at"] < oct3): + if itemdict["created_at"] < oct3: outlist.append(itemdict) continue - if not ("labels" in item.keys()): # if PR has no labels - continue - # checking whether "hacktoberfest-accepted" is one of the PR's labels - if any(label["name"].casefold() == "hacktoberfest-accepted" for label in item["labels"]): + + # checking PR's labels for "hacktoberfest-accepted" + if HacktoberStats._has_label(item, "hacktoberfest-accepted"): outlist.append(itemdict) continue # fetch topics for the pr repo topics_query_url = f"https://api.github.com/repos/{shortname}/topics" logging.debug(f"Fetching repo topics for {shortname} with url: {topics_query_url}") - async with aiohttp.ClientSession() as session: - async with session.get(topics_query_url, headers=GITHUB_TOPICS_ACCEPT_HEADER) as resp: - jsonresp2 = await resp.json() - + jsonresp2 = await HacktoberStats._fetch_url(topics_query_url, GITHUB_TOPICS_ACCEPT_HEADER) if not ("names" in jsonresp2.keys()): logging.error(f"Error fetching topics for {shortname}: {jsonresp2['message']}") @@ -338,6 +334,65 @@ class HacktoberStats(commands.Cog): outlist.append(itemdict) return outlist + @staticmethod + async def _fetch_url(url: str, headers: dict) -> dict: + """Retrieve API response from URL.""" + async with aiohttp.ClientSession() as session: + async with session.get(url, headers=headers) as resp: + jsonresp = await resp.json() + return jsonresp + + @staticmethod + def _has_label(pr: dict, labels: Union[List[str], str]) -> bool: + """ + Check if a PR has label 'labels'. + + 'labels' can be a string or a list of strings, if it's a list of strings + it will return true if any of the labels match. + """ + if not pr.get("labels"): # if PR has no labels + return False + if (isinstance(labels, str)) and (any(label["name"].casefold() == labels for label in pr["labels"])): + return True + for item in labels: + if any(label["name"].casefold() == item for label in pr["labels"]): + return True + return False + + @staticmethod + async def _is_accepted(pr: dict) -> bool: + """Check if a PR is merged, approved, or labelled hacktoberfest-accepted.""" + # checking for merge status + query_url = f"https://api.github.com/repos/{pr['repo_shortname']}/pulls" + query_url += pr["number"] + jsonresp = await HacktoberStats._fetch_url(query_url, REQUEST_HEADERS) + + if "message" in jsonresp.keys(): + logging.error( + f"Error fetching PR stats for #{pr['number']} in repo {pr['repo_shortname']}:\n" + f"{jsonresp['message']}" + ) + return False + if ("merged" in jsonresp.keys()) and (jsonresp["merged"] == "true"): + return True + + # checking for the label, using `jsonresp` which has the label information + if HacktoberStats._has_label(jsonresp, "hacktoberfest-accepted"): + return True + + # checking approval + query_url += "/reviews" + jsonresp2 = await HacktoberStats._fetch_url(query_url, REQUEST_HEADERS) + if "message" in jsonresp2.keys(): + logging.error( + f"Error fetching PR reviews for #{pr['number']} in repo {pr['repo_shortname']}:\n" + f"{jsonresp2['message']}" + ) + return False + if len(jsonresp2) == 0: # if PR has no reviews + return False + return any(item['status'] == "APPROVED" for item in jsonresp2) + @staticmethod def _get_shortname(in_url: str) -> str: """ @@ -352,12 +407,15 @@ class HacktoberStats(commands.Cog): return re.findall(exp, in_url)[0] @staticmethod - def _categorize_prs(prs: List[dict]) -> dict: + def _categorize_prs(prs: List[dict]) -> tuple: """ - Categorize PRs into 'in_review' and 'accepted'. + Categorize PRs into 'in_review' and 'accepted' and returns as a tuple. PRs created less than 14 days ago are 'in_review', PRs that are not are 'accepted' (after 14 days review period). + + PRs that are accepted must either be merged, approved, or labelled + 'hacktoberfest-accepted. """ now = datetime.now() in_review = [] @@ -365,14 +423,10 @@ class HacktoberStats(commands.Cog): for pr in prs: if (pr['created_at'] + timedelta(REVIEW_DAYS)) > now: in_review.append(pr) - else: + elif HacktoberStats._is_accepted(pr): accepted.append(pr) - out_dict = { - "in_review": in_review, - "accepted": accepted - } - return out_dict + return in_review, accepted @staticmethod def _build_prs_string(prs: List[tuple], user: str) -> str: -- cgit v1.2.3 From 41a0b02d891930927cd475a01df4b4219cdb5ec3 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Sat, 17 Oct 2020 17:52:36 +0800 Subject: async fixes --- bot/exts/halloween/hacktoberstats.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index b0b64be9..a05ea2f9 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -180,15 +180,15 @@ class HacktoberStats(commands.Cog): prs = await self.get_october_prs(github_username) if prs: - stats_embed = self.build_embed(github_username, prs) + stats_embed = await self.build_embed(github_username, prs) await ctx.send('Here are some stats!', embed=stats_embed) else: await ctx.send(f"No valid October GitHub contributions found for '{github_username}'") - def build_embed(self, github_username: str, prs: List[dict]) -> discord.Embed: + async def build_embed(self, github_username: str, prs: List[dict]) -> discord.Embed: """Return a stats embed built from github_username's PRs.""" logging.info(f"Building Hacktoberfest embed for GitHub user: '{github_username}'") - in_review, accepted = self._categorize_prs(prs) + in_review, accepted = await self._categorize_prs(prs) n = len(accepted) + len(in_review) # total number of PRs if n >= PRS_FOR_SHIRT: @@ -306,7 +306,7 @@ class HacktoberStats(commands.Cog): # if the PR has 'invalid' or 'spam' labels, the PR must be # either merged or approved for it to be included if HacktoberStats._has_label(item, ["invalid", "spam"]): - if not HacktoberStats._is_accepted(item): + if not await HacktoberStats._is_accepted(item): continue # PRs before oct 3 no need to check for topics @@ -363,8 +363,8 @@ class HacktoberStats(commands.Cog): async def _is_accepted(pr: dict) -> bool: """Check if a PR is merged, approved, or labelled hacktoberfest-accepted.""" # checking for merge status - query_url = f"https://api.github.com/repos/{pr['repo_shortname']}/pulls" - query_url += pr["number"] + query_url = f"https://api.github.com/repos/{pr['repo_shortname']}/pulls/" + query_url += str(pr["number"]) jsonresp = await HacktoberStats._fetch_url(query_url, REQUEST_HEADERS) if "message" in jsonresp.keys(): @@ -407,7 +407,7 @@ class HacktoberStats(commands.Cog): return re.findall(exp, in_url)[0] @staticmethod - def _categorize_prs(prs: List[dict]) -> tuple: + async def _categorize_prs(prs: List[dict]) -> tuple: """ Categorize PRs into 'in_review' and 'accepted' and returns as a tuple. @@ -423,7 +423,7 @@ class HacktoberStats(commands.Cog): for pr in prs: if (pr['created_at'] + timedelta(REVIEW_DAYS)) > now: in_review.append(pr) - elif HacktoberStats._is_accepted(pr): + elif await HacktoberStats._is_accepted(pr): accepted.append(pr) return in_review, accepted -- cgit v1.2.3 From 87f6969d0a3190b0b95846c7bbb808054ba17ead Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Sun, 18 Oct 2020 05:17:46 +0000 Subject: fix some bugs and allow topics caching --- bot/exts/halloween/hacktoberstats.py | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index a05ea2f9..94bfe138 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -22,17 +22,17 @@ REVIEW_DAYS = 14 # number of days needed after PR can be mature HACKTOBER_WHITELIST = WHITELISTED_CHANNELS + (Channels.hacktoberfest_2020,) REQUEST_HEADERS = {"User-Agent": "Python Discord Hacktoberbot"} +# using repo topics API during preview period requires an accept header +GITHUB_TOPICS_ACCEPT_HEADER = {"Accept": "application/vnd.github.mercy-preview+json"} if GITHUB_TOKEN := Tokens.github: REQUEST_HEADERS["Authorization"] = f"token {GITHUB_TOKEN}" + GITHUB_TOPICS_ACCEPT_HEADER["Authorization"] = f"token {GITHUB_TOKEN}" GITHUB_NONEXISTENT_USER_MESSAGE = ( "The listed users cannot be searched either because the users do not exist " "or you do not have permission to view the users." ) -# using repo topics API during preview period requires an accept header -GITHUB_TOPICS_ACCEPT_HEADER = {"Accept": "application/vnd.github.mercy-preview+json"} - class HacktoberStats(commands.Cog): """Hacktoberfest statistics Cog.""" @@ -292,6 +292,7 @@ class HacktoberStats(commands.Cog): logging.info(f"Found {len(jsonresp['items'])} Hacktoberfest PRs for GitHub user: '{github_username}'") outlist = [] # list of pr information dicts that will get returned oct3 = datetime(int(CURRENT_YEAR), 10, 3, 0, 0, 0) + hackto_topics = {} # cache whether each repo has the appropriate topic (bool values) for item in jsonresp["items"]: shortname = HacktoberStats._get_shortname(item["repository_url"]) itemdict = { @@ -321,16 +322,23 @@ class HacktoberStats(commands.Cog): outlist.append(itemdict) continue + # no need to query github if repo topics are fetched before already + if shortname in hackto_topics.keys(): + if hackto_topics[shortname]: + outlist.append(itemdict) + continue # fetch topics for the pr repo topics_query_url = f"https://api.github.com/repos/{shortname}/topics" logging.debug(f"Fetching repo topics for {shortname} with url: {topics_query_url}") jsonresp2 = await HacktoberStats._fetch_url(topics_query_url, GITHUB_TOPICS_ACCEPT_HEADER) - if not ("names" in jsonresp2.keys()): + if jsonresp2.get("names") is None: logging.error(f"Error fetching topics for {shortname}: {jsonresp2['message']}") + return # PRs after oct 3 that doesn't have 'hacktoberfest-accepted' label # must be in repo with 'hacktoberfest' topic if "hacktoberfest" in jsonresp2["names"]: + hackto_topics[shortname] = True # cache result in the dict for later use if needed outlist.append(itemdict) return outlist @@ -373,7 +381,7 @@ class HacktoberStats(commands.Cog): f"{jsonresp['message']}" ) return False - if ("merged" in jsonresp.keys()) and (jsonresp["merged"] == "true"): + if ("merged" in jsonresp.keys()) and jsonresp["merged"]: return True # checking for the label, using `jsonresp` which has the label information @@ -383,15 +391,23 @@ class HacktoberStats(commands.Cog): # checking approval query_url += "/reviews" jsonresp2 = await HacktoberStats._fetch_url(query_url, REQUEST_HEADERS) - if "message" in jsonresp2.keys(): + if isinstance(jsonresp2, dict): + # if API request is unsuccessful it will be a dict with the error in 'message' logging.error( f"Error fetching PR reviews for #{pr['number']} in repo {pr['repo_shortname']}:\n" f"{jsonresp2['message']}" ) return False + # if it is successful it will be a list instead of a dict if len(jsonresp2) == 0: # if PR has no reviews return False - return any(item['status'] == "APPROVED" for item in jsonresp2) + + # loop through reviews and check for approval + for item in jsonresp2: + if "status" in item.keys(): + if item['status'] == "APPROVED": + return True + return False @staticmethod def _get_shortname(in_url: str) -> str: -- cgit v1.2.3 From e374391631d1c2698bc777f5dc3b2076ed0d0eca Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Wed, 21 Oct 2020 11:11:48 +0000 Subject: fix keyerror --- bot/exts/halloween/hacktoberstats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 94bfe138..c872e241 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -307,7 +307,7 @@ class HacktoberStats(commands.Cog): # if the PR has 'invalid' or 'spam' labels, the PR must be # either merged or approved for it to be included if HacktoberStats._has_label(item, ["invalid", "spam"]): - if not await HacktoberStats._is_accepted(item): + if not await HacktoberStats._is_accepted(itemdict): continue # PRs before oct 3 no need to check for topics -- cgit v1.2.3 From 93168514ef59009377aa091161d78bfb2a1aa8ca Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Fri, 23 Oct 2020 08:30:25 +0000 Subject: fix time and date specific things --- bot/exts/halloween/hacktoberstats.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 94bfe138..0b75ca91 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -259,7 +259,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}-11-01T00:00:00-11:00" + date_range = f"{CURRENT_YEAR}-09-30T10:00Z..{CURRENT_YEAR}-11-01T12:00Z" per_page = "300" query_url = ( f"{base_url}" @@ -291,7 +291,7 @@ class HacktoberStats(commands.Cog): logging.info(f"Found {len(jsonresp['items'])} Hacktoberfest PRs for GitHub user: '{github_username}'") outlist = [] # list of pr information dicts that will get returned - oct3 = datetime(int(CURRENT_YEAR), 10, 3, 0, 0, 0) + oct3 = datetime(int(CURRENT_YEAR), 10, 3, 23, 59, 59, tzinfo=None) hackto_topics = {} # cache whether each repo has the appropriate topic (bool values) for item in jsonresp["items"]: shortname = HacktoberStats._get_shortname(item["repository_url"]) @@ -434,12 +434,13 @@ class HacktoberStats(commands.Cog): 'hacktoberfest-accepted. """ now = datetime.now() + oct3 = datetime(CURRENT_YEAR, 10, 3, 23, 59, 59, tzinfo=None) in_review = [] accepted = [] for pr in prs: if (pr['created_at'] + timedelta(REVIEW_DAYS)) > now: in_review.append(pr) - elif await HacktoberStats._is_accepted(pr): + elif (pr['created_at'] <= oct3) or await HacktoberStats._is_accepted(pr): accepted.append(pr) return in_review, accepted -- cgit v1.2.3