diff options
| author | 2020-10-01 10:18:39 +0200 | |
|---|---|---|
| committer | 2020-10-01 10:18:39 +0200 | |
| commit | 2bfe5461320cf03f6e84d30d6cea5a5be00c0c28 (patch) | |
| tree | 1ad0a17dfbf3efca46a830ab0a01f772dac22ffa /bot/exts | |
| parent | Merge pull request #466 from python-discord/joseph/hacktoberfest-fixes (diff) | |
| parent | Removed unnecessary GitHub API request and checked response message instead. (diff) | |
Merge pull request #468 from python-discord/Den4200/hackstats-fix
Hacktoberfest Stats: Check GitHub user exists before searching for PRs
Diffstat (limited to 'bot/exts')
| -rw-r--r-- | bot/exts/halloween/hacktoberstats.py | 23 | 
1 files changed, 19 insertions, 4 deletions
| diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 9dd0e1a4..ed1755e3 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -10,7 +10,7 @@ import aiohttp  import discord  from discord.ext import commands -from bot.constants import Channels, Month, WHITELISTED_CHANNELS +from bot.constants import Channels, Month, Tokens, WHITELISTED_CHANNELS  from bot.utils.decorators import in_month, override_in_channel  from bot.utils.persist import make_persistent @@ -20,6 +20,15 @@ CURRENT_YEAR = datetime.now().year  # Used to construct GH API query  PRS_FOR_SHIRT = 4  # Minimum number of PRs before a shirt is awarded  HACKTOBER_WHITELIST = WHITELISTED_CHANNELS + (Channels.hacktoberfest_2020,) +REQUEST_HEADERS = {"User-Agent": "Python Discord Hacktoberbot"} +if GITHUB_TOKEN := Tokens.github: +    REQUEST_HEADERS["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." +) +  class HacktoberStats(commands.Cog):      """Hacktoberfest statistics Cog.""" @@ -242,16 +251,22 @@ class HacktoberStats(commands.Cog):              f"&per_page={per_page}"          ) -        headers = {"user-agent": "Discord Python Hacktoberbot"}          async with aiohttp.ClientSession() as session: -            async with session.get(query_url, headers=headers) as resp: +            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"] -            logging.error(f"GitHub API request for '{github_username}' failed with message: {api_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 '{github_username}'") +            else: +                logging.error(f"GitHub API request for '{github_username}' failed with message: {api_message}") +              return +          else:              if jsonresp["total_count"] == 0:                  # Short circuit if there aren't any PRs | 
