From fcad0c8421a185ea696d02bf92d78fb7e21fc63e Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Thu, 22 Oct 2020 07:09:17 +0000 Subject: improve message when user not found --- bot/exts/halloween/hacktoberstats.py | 21 ++++++++++++++------- 1 file changed, 14 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 94bfe138..e8ed1d1b 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, timedelta from pathlib import Path -from typing import List, Tuple, Union +from typing import List, Optional, Tuple, Union import aiohttp import discord @@ -179,11 +179,15 @@ class HacktoberStats(commands.Cog): async with ctx.typing(): prs = await self.get_october_prs(github_username) + if isinstance(prs, str): # it will be a string if user not found or no october prs found + await ctx.send(prs) + return + if 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}'") + await ctx.send(f"No valid hacktoberfest contributions found for '{github_username}'") async def build_embed(self, github_username: str, prs: List[dict]) -> discord.Embed: """Return a stats embed built from github_username's PRs.""" @@ -232,7 +236,7 @@ class HacktoberStats(commands.Cog): return stats_embed @staticmethod - async def get_october_prs(github_username: str) -> Union[List[dict], None]: + async def get_october_prs(github_username: str) -> Optional[Union[List[dict], str]]: """ Query GitHub's API for PRs created during the month of October by github_username. @@ -279,15 +283,18 @@ 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}'") + message = f"No GitHub user found named '{github_username}'" + logging.debug(message) else: logging.error(f"GitHub API request for '{github_username}' failed with message: {api_message}") - return + message = None + return message 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 + message = f"No october PRs found for GitHub user: '{github_username}'" + logging.info(message) + return message logging.info(f"Found {len(jsonresp['items'])} Hacktoberfest PRs for GitHub user: '{github_username}'") outlist = [] # list of pr information dicts that will get returned -- cgit v1.2.3 From df9d809c223d01b4e7bb056468fed7eb5e5989d1 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Thu, 22 Oct 2020 07:49:11 +0000 Subject: improve implementation with return value instead of string/None now its empty list/None --- bot/exts/halloween/hacktoberstats.py | 23 +++++++++++------------ 1 file changed, 11 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 9ae9c227..9fb71651 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -179,8 +179,8 @@ class HacktoberStats(commands.Cog): async with ctx.typing(): prs = await self.get_october_prs(github_username) - if isinstance(prs, str): # it will be a string if user not found or no october prs found - await ctx.send(prs) + if prs is None: # it will be a None if user not found + await ctx.send("GitHub user not found: " + github_username) return if prs: @@ -236,7 +236,7 @@ class HacktoberStats(commands.Cog): return stats_embed @staticmethod - async def get_october_prs(github_username: str) -> Optional[Union[List[dict], str]]: + async def get_october_prs(github_username: str) -> Optional[List[dict]]: """ Query GitHub's API for PRs created during the month of October by github_username. @@ -256,7 +256,8 @@ class HacktoberStats(commands.Cog): "number": int } - Otherwise, return None + Otherwise, return empty list + None will be returned when github user not found """ logging.info(f"Fetching Hacktoberfest Stats for GitHub user: '{github_username}'") base_url = "https://api.github.com/search/issues?q=" @@ -283,18 +284,16 @@ 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: - message = f"No GitHub user found named '{github_username}'" - logging.debug(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}") - message = None - return message + return [] # not returning None here, because that should be for when user not found if jsonresp["total_count"] == 0: # Short circuit if there aren't any PRs - message = f"No october PRs found for GitHub user: '{github_username}'" - logging.info(message) - return message + logging.info(f"No october PRs found for GitHub user: '{github_username}'") + return [] logging.info(f"Found {len(jsonresp['items'])} Hacktoberfest PRs for GitHub user: '{github_username}'") outlist = [] # list of pr information dicts that will get returned @@ -340,7 +339,7 @@ class HacktoberStats(commands.Cog): jsonresp2 = await HacktoberStats._fetch_url(topics_query_url, GITHUB_TOPICS_ACCEPT_HEADER) if jsonresp2.get("names") is None: logging.error(f"Error fetching topics for {shortname}: {jsonresp2['message']}") - return + return [] # PRs after oct 3 that doesn't have 'hacktoberfest-accepted' label # must be in repo with 'hacktoberfest' topic -- cgit v1.2.3 From 664c64ffb9b163e663740fd9fe56fc977fb1c36d Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Thu, 22 Oct 2020 07:56:51 +0000 Subject: better no-prs 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 9fb71651..1640e6cf 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -187,7 +187,7 @@ class HacktoberStats(commands.Cog): 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 hacktoberfest contributions found for '{github_username}'") + await ctx.send(f"No valid hacktoberfest PRs found for '{github_username}'") async 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 2b379c2f8f352caf694913c3cb298599463c87c0 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Sat, 31 Oct 2020 05:18:21 +0000 Subject: Fix capitalization and grammar For Hacktoberstats --- bot/exts/halloween/hacktoberstats.py | 24 ++++++++++++------------ 1 file changed, 12 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 1640e6cf..0f6b9924 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -179,7 +179,7 @@ class HacktoberStats(commands.Cog): async with ctx.typing(): prs = await self.get_october_prs(github_username) - if prs is None: # it will be a None if user not found + if prs is None: # Will be None if the user was not found await ctx.send("GitHub user not found: " + github_username) return @@ -187,14 +187,14 @@ class HacktoberStats(commands.Cog): 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 hacktoberfest PRs found for '{github_username}'") + await ctx.send(f"No valid Hacktoberfest PRs found for '{github_username}'") 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 = await self._categorize_prs(prs) - n = len(accepted) + len(in_review) # total number of PRs + n = len(accepted) + len(in_review) # Total number of PRs if n >= PRS_FOR_SHIRT: shirtstr = f"**{github_username} is eligible for a T-shirt or a tree!**" elif n == PRS_FOR_SHIRT - 1: @@ -220,7 +220,7 @@ class HacktoberStats(commands.Cog): icon_url="https://avatars1.githubusercontent.com/u/35706162?s=200&v=4" ) - # this will handle when no PRs in_review or accepted + # 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( @@ -257,7 +257,7 @@ class HacktoberStats(commands.Cog): } Otherwise, return empty list - None will be returned when github user not found + None will be returned when the GitHub user was not found """ logging.info(f"Fetching Hacktoberfest Stats for GitHub user: '{github_username}'") base_url = "https://api.github.com/search/issues?q=" @@ -288,11 +288,11 @@ class HacktoberStats(commands.Cog): return else: logging.error(f"GitHub API request for '{github_username}' failed with message: {api_message}") - return [] # not returning None here, because that should be for when user not found + return [] # No October PRs were found due to error if jsonresp["total_count"] == 0: # Short circuit if there aren't any PRs - logging.info(f"No october PRs found for GitHub user: '{github_username}'") + logging.info(f"No October PRs found for GitHub user: '{github_username}'") return [] logging.info(f"Found {len(jsonresp['items'])} Hacktoberfest PRs for GitHub user: '{github_username}'") @@ -310,7 +310,7 @@ class HacktoberStats(commands.Cog): "number": item["number"] } - # if the PR has 'invalid' or 'spam' labels, the PR must be + # 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(itemdict): @@ -323,17 +323,17 @@ class HacktoberStats(commands.Cog): outlist.append(itemdict) continue - # checking PR's labels for "hacktoberfest-accepted" + # Checking PR's labels for "hacktoberfest-accepted" if HacktoberStats._has_label(item, "hacktoberfest-accepted"): outlist.append(itemdict) continue - # no need to query github if repo topics are fetched before already + # 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 + # 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) @@ -344,7 +344,7 @@ class HacktoberStats(commands.Cog): # 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 + hackto_topics[shortname] = True # Cache result in the dict for later use if needed outlist.append(itemdict) return outlist -- cgit v1.2.3 From 147e37a53073b6e4291e1d386f8d2a6b0114f535 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Sat, 31 Oct 2020 07:28:56 +0000 Subject: Put GitHub user-not-found message in embed With random `NEGATIVE_REPLIES` + color=red --- bot/exts/halloween/hacktoberstats.py | 7 +++++-- 1 file changed, 5 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 0f6b9924..bb5a6325 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -1,5 +1,6 @@ import json import logging +import random import re from collections import Counter from datetime import datetime, timedelta @@ -10,7 +11,7 @@ import aiohttp import discord from discord.ext import commands -from bot.constants import Channels, Month, Tokens, WHITELISTED_CHANNELS +from bot.constants import Channels, Month, NEGATIVE_REPLIES, Tokens, WHITELISTED_CHANNELS from bot.utils.decorators import in_month, override_in_channel from bot.utils.persist import make_persistent @@ -180,7 +181,9 @@ class HacktoberStats(commands.Cog): prs = await self.get_october_prs(github_username) if prs is None: # Will be None if the user was not found - await ctx.send("GitHub user not found: " + github_username) + await ctx.send(embed=discord.Embed(title=random.choice(NEGATIVE_REPLIES), + description=f"GitHub user `{github_username}` was not found.", + colour=discord.Colour.red())) return if prs: -- cgit v1.2.3 From 12a0832d9259b20eb622bcc4a0b5e2c2d766628a Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Sat, 31 Oct 2020 15:38:28 +0800 Subject: Fix capitalization of 'PR' in hacktoberstats.py --- 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 bb5a6325..a5ec4dc4 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -336,7 +336,7 @@ class HacktoberStats(commands.Cog): if hackto_topics[shortname]: outlist.append(itemdict) continue - # Fetch topics for the pr repo + # Fetch topics for the PR's 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) -- cgit v1.2.3 From 57800121c067d22396055e30e3e3a91899c9b4fa Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Sat, 31 Oct 2020 15:42:59 +0800 Subject: Continue loop if repo topics API request errored --- 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 a5ec4dc4..701ef0b3 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -342,7 +342,7 @@ class HacktoberStats(commands.Cog): jsonresp2 = await HacktoberStats._fetch_url(topics_query_url, GITHUB_TOPICS_ACCEPT_HEADER) if jsonresp2.get("names") is None: logging.error(f"Error fetching topics for {shortname}: {jsonresp2['message']}") - return [] + continue # Assume the repo doesn't have the `hacktoberfest` topic if API request errored # PRs after oct 3 that doesn't have 'hacktoberfest-accepted' label # must be in repo with 'hacktoberfest' topic -- cgit v1.2.3 From 4550d27308b17c6107f123469cf529c35318654a Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Thu, 3 Dec 2020 09:45:43 +0800 Subject: Remove unused pathlib.Path import --- bot/exts/halloween/hacktoberstats.py | 1 - 1 file changed, 1 deletion(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index ae949f53..a599af73 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -3,7 +3,6 @@ import random import re from collections import Counter from datetime import datetime, timedelta -from pathlib import Path from typing import List, Optional, Tuple, Union import aiohttp -- cgit v1.2.3 From 030a9996664cb6a52e027406a76d35c62e30a72a Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Wed, 6 Jan 2021 17:42:37 +0800 Subject: Formatting and add full stop to docstring - bot/exts/halloween/hacktoberstats.py line 130, better readability - same file line 208-209 add full stop --- bot/exts/halloween/hacktoberstats.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'bot/exts/halloween/hacktoberstats.py') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index a599af73..a1c55922 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -127,9 +127,13 @@ class HacktoberStats(commands.Cog): prs = await self.get_october_prs(github_username) if prs is None: # Will be None if the user was not found - await ctx.send(embed=discord.Embed(title=random.choice(NEGATIVE_REPLIES), - description=f"GitHub user `{github_username}` was not found.", - colour=discord.Colour.red())) + await ctx.send( + embed=discord.Embed( + title=random.choice(NEGATIVE_REPLIES), + description=f"GitHub user `{github_username}` was not found.", + colour=discord.Colour.red() + ) + ) return if prs: @@ -205,8 +209,8 @@ class HacktoberStats(commands.Cog): "number": int } - Otherwise, return empty list - None will be returned when the GitHub user was not found + Otherwise, return empty list. + None will be returned when the GitHub user was not found. """ logging.info(f"Fetching Hacktoberfest Stats for GitHub user: '{github_username}'") base_url = "https://api.github.com/search/issues?q=" -- cgit v1.2.3