diff options
| author | 2020-10-01 00:15:14 -0400 | |
|---|---|---|
| committer | 2020-10-01 00:15:14 -0400 | |
| commit | deee8cfebc69e63ddc0060bc888e7f00b90efe6e (patch) | |
| tree | 1ad0a17dfbf3efca46a830ab0a01f772dac22ffa /bot | |
| parent | Check the GitHub user exists before searching for their PRs. (diff) | |
Removed unnecessary GitHub API request and checked response message instead.
This check is to see if a GitHub user is non-existent. We do not want to log
this as an error.
Diffstat (limited to '')
| -rw-r--r-- | bot/exts/halloween/hacktoberstats.py | 19 | 
1 files changed, 13 insertions, 6 deletions
| diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index a2261caf..ed1755e3 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -24,6 +24,11 @@ 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.""" @@ -247,19 +252,21 @@ class HacktoberStats(commands.Cog):          )          async with aiohttp.ClientSession() as session: -            async with session.get(f"https://api.github.com/users/{github_username}", headers=REQUEST_HEADERS) as resp: -                if resp.status == 404: -                    logging.debug(f"No GitHub user found named '{github_username}'") -                    return -              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 | 
