From 9b62d84554fe8a27e3baef5e73eaac9127b2e54d Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Sat, 23 Jan 2021 17:39:51 +0530 Subject: Add ability to automatically send issues if matching # --- bot/exts/evergreen/issues.py | 96 +++++++++++++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 27 deletions(-) (limited to 'bot/exts/evergreen/issues.py') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index e419a6f5..b701346c 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -1,5 +1,7 @@ import logging import random +import re +import typing as t import discord from discord.ext import commands @@ -13,9 +15,7 @@ BAD_RESPONSE = { 404: "Issue/pull request not located! Please enter a valid number!", 403: "Rate limit has been hit! Please try again later!" } - MAX_REQUESTS = 10 - REQUEST_HEADERS = dict() if GITHUB_TOKEN := Tokens.github: REQUEST_HEADERS["Authorization"] = f"token {GITHUB_TOKEN}" @@ -27,31 +27,19 @@ class Issues(commands.Cog): def __init__(self, bot: commands.Bot): self.bot = bot - @commands.command(aliases=("pr",)) - @override_in_channel(WHITELISTED_CHANNELS + (Channels.dev_contrib, Channels.dev_branding)) - async def issue( - self, - ctx: commands.Context, - numbers: commands.Greedy[int], - repository: str = "sir-lancebot", - user: str = "python-discord" - ) -> None: - """Command to retrieve issue(s) from a GitHub repository.""" + async def fetch_issues( + self, + numbers: set, + repository: str, + user: str + ) -> t.Union[str, list]: + """Retrieve issue(s) from a GitHub repository.""" links = [] - numbers = set(numbers) # Convert from list to set to remove duplicates, if any - if not numbers: - await ctx.invoke(self.bot.get_command('help'), 'issue') - return + return "Numbers not found." if len(numbers) > MAX_REQUESTS: - embed = discord.Embed( - title=random.choice(ERROR_REPLIES), - color=Colours.soft_red, - description=f"Too many issues/PRs! (maximum of {MAX_REQUESTS})" - ) - await ctx.send(embed=embed) - return + return "Max requests hit." for number in numbers: url = f"https://api.github.com/repos/{user}/{repository}/issues/{number}" @@ -63,7 +51,7 @@ class Issues(commands.Cog): if r.status in BAD_RESPONSE: log.warning(f"Received response {r.status} from: {url}") - return await ctx.send(f"[{str(r.status)}] #{number} {BAD_RESPONSE.get(r.status)}") + return f"[{str(r.status)}] #{number} {BAD_RESPONSE.get(r.status)}" # The initial API request is made to the issues API endpoint, which will return information # if the issue or PR is present. However, the scope of information returned for PRs differs @@ -92,15 +80,69 @@ class Issues(commands.Cog): issue_url = json_data.get("html_url") links.append([icon_url, f"[{repository}] #{number} {json_data.get('title')}", issue_url]) - # Issue/PR format: emoji to show if open/closed/merged, number and the title as a singular link. - description_list = ["{0} [{1}]({2})".format(*link) for link in links] + return links + + @staticmethod + def get_embed(result: list, user: str = "python-discord", repository: str = "") -> discord.Embed: + """Get Response Embed.""" + description_list = ["{0} [{1}]({2})".format(*link) for link in result] resp = discord.Embed( colour=Colours.bright_green, description='\n'.join(description_list) ) resp.set_author(name="GitHub", url=f"https://github.com/{user}/{repository}") - await ctx.send(embed=resp) + return resp + + @commands.command(aliases=("pr",)) + @override_in_channel(WHITELISTED_CHANNELS + (Channels.dev_contrib, Channels.dev_branding)) + async def issue( + self, + ctx: commands.Context, + numbers: commands.Greedy[int], + repository: str = "sir-lancebot", + user: str = "python-discord" + ) -> None: + """Command to retrieve issue(s) from a GitHub repository.""" + print(numbers) + result = await self.fetch_issues(set(numbers), repository, user) + + if result == "Numbers not found.": + await ctx.invoke(self.bot.get_command('help'), 'issue') + + elif result == "Max requests hit.": + embed = discord.Embed( + title=random.choice(ERROR_REPLIES), + color=Colours.soft_red, + description=f"Too many issues/PRs! (maximum of {MAX_REQUESTS})" + ) + await ctx.send(embed=embed) + + elif isinstance(result, list): + # Issue/PR format: emoji to show if open/closed/merged, number and the title as a singular link. + resp = self.get_embed(result, user, repository) + await ctx.send(embed=resp) + + elif isinstance(result, str): + await ctx.send(result) + + @commands.Cog.listener() + async def on_message(self, message: discord.Message) -> None: + """Command to retrieve issue(s) from a GitHub repository using automatic linking if matching #.""" + message_repo_issue_map = re.findall(r".+?(bot|meta|sir-lancebot|logcord)#(\d+)", message.content) + links = [] + + if message_repo_issue_map: + for repo_issue in message_repo_issue_map: + result = await self.fetch_issues({repo_issue[1]}, repo_issue[0], "python-discord") + if isinstance(result, list): + links.append(*result) + + if not links: + return + + resp = self.get_embed(links, "python-discord") + await message.channel.send(embed=resp) def setup(bot: commands.Bot) -> None: -- cgit v1.2.3 From fbe073cb5e8dca831b4126c5b221844032870d26 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Tue, 26 Jan 2021 16:28:36 +0530 Subject: Add in codeblock check --- bot/exts/evergreen/issues.py | 48 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) (limited to 'bot/exts/evergreen/issues.py') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index b701346c..1924d822 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -4,7 +4,7 @@ import re import typing as t import discord -from discord.ext import commands +from discord.ext import commands, tasks from bot.constants import Channels, Colours, ERROR_REPLIES, Emojis, Tokens, WHITELISTED_CHANNELS from bot.utils.decorators import override_in_channel @@ -17,15 +17,43 @@ BAD_RESPONSE = { } MAX_REQUESTS = 10 REQUEST_HEADERS = dict() +PYDIS_REPOS = "https://api.github.com/orgs/python-discord/repos" + if GITHUB_TOKEN := Tokens.github: REQUEST_HEADERS["Authorization"] = f"token {GITHUB_TOKEN}" - class Issues(commands.Cog): """Cog that allows users to retrieve issues from GitHub.""" def __init__(self, bot: commands.Bot): self.bot = bot + self.repos = [] + self.get_pydis_repos.start() + + @tasks.loop(minutes=30) + async def get_pydis_repos(self) -> None: + async with self.bot.http_session.get(PYDIS_REPOS) as resp: + if resp.status == 200: + data = await resp.json() + for repo in data: + self.repos.append(repo["full_name"].split("/")[1]) + else: + log.debug(f"Failed to get latest Pydis repositories. Status code {resp.status}") + + @staticmethod + def check_in_block(message: discord.Message, repo_issue: str) -> bool: + block = ( + re.findall(r"```([\s\S]*)?```", message.content) + or re.findall(r"```*\n([\s\S]*)?\n```", message.content) + or re.findall(r"```*([\s\S]*)?\n```", message.content) + or re.findall(r"```*\n([\s\S]*)?```", message.content) + or re.findall(r"`([\s\S]*)?`", message.content) + ) + print(block) + + if "#".join(repo_issue.split(" ")) in "".join([*block]): + return True + return False async def fetch_issues( self, @@ -44,7 +72,7 @@ class Issues(commands.Cog): for number in numbers: url = f"https://api.github.com/repos/{user}/{repository}/issues/{number}" merge_url = f"https://api.github.com/repos/{user}/{repository}/pulls/{number}/merge" - + print(url) log.trace(f"Querying GH issues API: {url}") async with self.bot.http_session.get(url, headers=REQUEST_HEADERS) as r: json_data = await r.json() @@ -104,7 +132,6 @@ class Issues(commands.Cog): user: str = "python-discord" ) -> None: """Command to retrieve issue(s) from a GitHub repository.""" - print(numbers) result = await self.fetch_issues(set(numbers), repository, user) if result == "Numbers not found.": @@ -129,14 +156,19 @@ class Issues(commands.Cog): @commands.Cog.listener() async def on_message(self, message: discord.Message) -> None: """Command to retrieve issue(s) from a GitHub repository using automatic linking if matching #.""" - message_repo_issue_map = re.findall(r".+?(bot|meta|sir-lancebot|logcord)#(\d+)", message.content) + repo_regex = "|".join(repo for repo in self.repos) + message_repo_issue_map = re.findall(fr".+?({repo_regex})#(\d+)", message.content) links = [] if message_repo_issue_map: for repo_issue in message_repo_issue_map: - result = await self.fetch_issues({repo_issue[1]}, repo_issue[0], "python-discord") - if isinstance(result, list): - links.append(*result) + if self.check_in_block(message, " ".join([*repo_issue])): + print("in") + continue + else: + result = await self.fetch_issues({repo_issue[1]}, repo_issue[0], "python-discord") + if isinstance(result, list): + links.append(*result) if not links: return -- cgit v1.2.3 From 2dcdec0defb0f75478cd344cabd431d02b506741 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Tue, 26 Jan 2021 16:36:24 +0530 Subject: Remove debug code and add docstrings. --- bot/exts/evergreen/issues.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'bot/exts/evergreen/issues.py') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index 1924d822..ba8a70cf 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -22,6 +22,7 @@ PYDIS_REPOS = "https://api.github.com/orgs/python-discord/repos" if GITHUB_TOKEN := Tokens.github: REQUEST_HEADERS["Authorization"] = f"token {GITHUB_TOKEN}" + class Issues(commands.Cog): """Cog that allows users to retrieve issues from GitHub.""" @@ -32,6 +33,7 @@ class Issues(commands.Cog): @tasks.loop(minutes=30) async def get_pydis_repos(self) -> None: + """Get all python-discord repositories on github.""" async with self.bot.http_session.get(PYDIS_REPOS) as resp: if resp.status == 200: data = await resp.json() @@ -42,6 +44,7 @@ class Issues(commands.Cog): @staticmethod def check_in_block(message: discord.Message, repo_issue: str) -> bool: + """Check whether the # is in codeblocks.""" block = ( re.findall(r"```([\s\S]*)?```", message.content) or re.findall(r"```*\n([\s\S]*)?\n```", message.content) @@ -49,7 +52,6 @@ class Issues(commands.Cog): or re.findall(r"```*\n([\s\S]*)?```", message.content) or re.findall(r"`([\s\S]*)?`", message.content) ) - print(block) if "#".join(repo_issue.split(" ")) in "".join([*block]): return True @@ -163,7 +165,6 @@ class Issues(commands.Cog): if message_repo_issue_map: for repo_issue in message_repo_issue_map: if self.check_in_block(message, " ".join([*repo_issue])): - print("in") continue else: result = await self.fetch_issues({repo_issue[1]}, repo_issue[0], "python-discord") -- cgit v1.2.3 From d80308f509489bfe6120020758721093af2df7f9 Mon Sep 17 00:00:00 2001 From: Shivansh-007 <69356296+Shivansh-007@users.noreply.github.com> Date: Tue, 26 Jan 2021 16:39:02 +0530 Subject: Remove debug code. Co-authored-by: Xithrius <15021300+Xithrius@users.noreply.github.com> --- bot/exts/evergreen/issues.py | 1 - 1 file changed, 1 deletion(-) (limited to 'bot/exts/evergreen/issues.py') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index ba8a70cf..946da354 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -74,7 +74,6 @@ class Issues(commands.Cog): for number in numbers: url = f"https://api.github.com/repos/{user}/{repository}/issues/{number}" merge_url = f"https://api.github.com/repos/{user}/{repository}/pulls/{number}/merge" - print(url) log.trace(f"Querying GH issues API: {url}") async with self.bot.http_session.get(url, headers=REQUEST_HEADERS) as r: json_data = await r.json() -- cgit v1.2.3 From ec45aac56861f3b7b8813957bf7e0e3a890fc44a Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Tue, 26 Jan 2021 16:54:30 +0530 Subject: Cache repo regex --- bot/exts/evergreen/issues.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot/exts/evergreen/issues.py') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index ba8a70cf..55bc8cf0 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -39,6 +39,7 @@ class Issues(commands.Cog): data = await resp.json() for repo in data: self.repos.append(repo["full_name"].split("/")[1]) + self.repo_regex = "|".join(repo for repo in self.repos) else: log.debug(f"Failed to get latest Pydis repositories. Status code {resp.status}") @@ -158,8 +159,7 @@ class Issues(commands.Cog): @commands.Cog.listener() async def on_message(self, message: discord.Message) -> None: """Command to retrieve issue(s) from a GitHub repository using automatic linking if matching #.""" - repo_regex = "|".join(repo for repo in self.repos) - message_repo_issue_map = re.findall(fr".+?({repo_regex})#(\d+)", message.content) + message_repo_issue_map = re.findall(fr".+?({self.repo_regex})#(\d+)", message.content) links = [] if message_repo_issue_map: -- cgit v1.2.3 From 65e5558df35892c2136f9182d07307b496582019 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Tue, 26 Jan 2021 16:56:18 +0530 Subject: Use list.extend while appending issue links --- bot/exts/evergreen/issues.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/evergreen/issues.py') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index 55bc8cf0..724be737 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -169,7 +169,7 @@ class Issues(commands.Cog): else: result = await self.fetch_issues({repo_issue[1]}, repo_issue[0], "python-discord") if isinstance(result, list): - links.append(*result) + links.extend(result) if not links: return -- cgit v1.2.3 From d973c1266b93a5baeb32a2f04ed797b8d0f0cd1b Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Tue, 26 Jan 2021 17:07:48 +0530 Subject: Change PYDIS_REPOS to PYTHON_DISCORD_REPOS --- bot/exts/evergreen/issues.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'bot/exts/evergreen/issues.py') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index 724be737..95187551 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -17,7 +17,7 @@ BAD_RESPONSE = { } MAX_REQUESTS = 10 REQUEST_HEADERS = dict() -PYDIS_REPOS = "https://api.github.com/orgs/python-discord/repos" +PYTHON_DISCORD_REPOS = "https://api.github.com/orgs/python-discord/repos" if GITHUB_TOKEN := Tokens.github: REQUEST_HEADERS["Authorization"] = f"token {GITHUB_TOKEN}" @@ -34,7 +34,7 @@ class Issues(commands.Cog): @tasks.loop(minutes=30) async def get_pydis_repos(self) -> None: """Get all python-discord repositories on github.""" - async with self.bot.http_session.get(PYDIS_REPOS) as resp: + async with self.bot.http_session.get(PYTHON_DISCORD_REPOS) as resp: if resp.status == 200: data = await resp.json() for repo in data: @@ -155,8 +155,9 @@ class Issues(commands.Cog): elif isinstance(result, str): await ctx.send(result) - + @commands.Cog.listener() + @override_in_channel(WHITELISTED_CHANNELS + (Channels.dev_contrib, Channels.dev_branding)) async def on_message(self, message: discord.Message) -> None: """Command to retrieve issue(s) from a GitHub repository using automatic linking if matching #.""" message_repo_issue_map = re.findall(fr".+?({self.repo_regex})#(\d+)", message.content) -- cgit v1.2.3 From 24f915bbe94452f80c7ab57b90b0f48d09e07e63 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Tue, 26 Jan 2021 17:13:12 +0530 Subject: Make the python_discord_repos' orgranisation name configurable --- bot/exts/evergreen/issues.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'bot/exts/evergreen/issues.py') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index 95187551..d816770c 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -17,7 +17,7 @@ BAD_RESPONSE = { } MAX_REQUESTS = 10 REQUEST_HEADERS = dict() -PYTHON_DISCORD_REPOS = "https://api.github.com/orgs/python-discord/repos" +PYTHON_DISCORD_REPOS = "https://api.github.com/orgs/{repo}/repos" if GITHUB_TOKEN := Tokens.github: REQUEST_HEADERS["Authorization"] = f"token {GITHUB_TOKEN}" @@ -34,7 +34,7 @@ class Issues(commands.Cog): @tasks.loop(minutes=30) async def get_pydis_repos(self) -> None: """Get all python-discord repositories on github.""" - async with self.bot.http_session.get(PYTHON_DISCORD_REPOS) as resp: + async with self.bot.http_session.get(PYTHON_DISCORD_REPOS.format(repo="python-discord")) as resp: if resp.status == 200: data = await resp.json() for repo in data: @@ -155,7 +155,7 @@ class Issues(commands.Cog): elif isinstance(result, str): await ctx.send(result) - + @commands.Cog.listener() @override_in_channel(WHITELISTED_CHANNELS + (Channels.dev_contrib, Channels.dev_branding)) async def on_message(self, message: discord.Message) -> None: -- cgit v1.2.3 From b6cda1a14a2a2e69dac59de675cbea75638c3f64 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Tue, 26 Jan 2021 17:23:30 +0530 Subject: Use enums for fetch issue errors. --- bot/exts/evergreen/issues.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'bot/exts/evergreen/issues.py') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index d816770c..bd62fc22 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -2,6 +2,7 @@ import logging import random import re import typing as t +from enum import Enum import discord from discord.ext import commands, tasks @@ -23,6 +24,12 @@ if GITHUB_TOKEN := Tokens.github: REQUEST_HEADERS["Authorization"] = f"token {GITHUB_TOKEN}" +class FetchIssueErrors(Enum): + value_error = "Numbers not found." + max_requests = "Max requests hit." + + + class Issues(commands.Cog): """Cog that allows users to retrieve issues from GitHub.""" @@ -67,10 +74,10 @@ class Issues(commands.Cog): """Retrieve issue(s) from a GitHub repository.""" links = [] if not numbers: - return "Numbers not found." + return FetchIssueErrors.value_error if len(numbers) > MAX_REQUESTS: - return "Max requests hit." + return FetchIssueErrors.max_requests for number in numbers: url = f"https://api.github.com/repos/{user}/{repository}/issues/{number}" @@ -137,10 +144,10 @@ class Issues(commands.Cog): """Command to retrieve issue(s) from a GitHub repository.""" result = await self.fetch_issues(set(numbers), repository, user) - if result == "Numbers not found.": + if result == FetchIssueErrors.value_error: await ctx.invoke(self.bot.get_command('help'), 'issue') - elif result == "Max requests hit.": + elif result == FetchIssueErrors.max_requests: embed = discord.Embed( title=random.choice(ERROR_REPLIES), color=Colours.soft_red, -- cgit v1.2.3 From 9d5352fcf1c4b9d09d9aba9632689f54beeb4087 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Tue, 26 Jan 2021 17:24:26 +0530 Subject: Fix lint issues --- bot/exts/evergreen/issues.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/evergreen/issues.py') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index bd62fc22..d258cc3c 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -2,7 +2,7 @@ import logging import random import re import typing as t -from enum import Enum +from enum import Enum import discord from discord.ext import commands, tasks -- cgit v1.2.3 From 8b71066b6dd645918cdceef389eb98d334c62434 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Tue, 26 Jan 2021 17:27:32 +0530 Subject: Add docstring to FetchIssueErrors and remove extra new line --- bot/exts/evergreen/issues.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'bot/exts/evergreen/issues.py') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index d6790edf..c9f87957 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -25,11 +25,12 @@ if GITHUB_TOKEN := Tokens.github: class FetchIssueErrors(Enum): + """Errors returned in fetch issues.""" + value_error = "Numbers not found." max_requests = "Max requests hit." - class Issues(commands.Cog): """Cog that allows users to retrieve issues from GitHub.""" -- cgit v1.2.3 From d2f9aa0d2236426e88334483959938328950f357 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Wed, 27 Jan 2021 10:09:05 +0530 Subject: Improve code block regex --- bot/exts/evergreen/issues.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'bot/exts/evergreen/issues.py') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index c9f87957..d2c70d4b 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -23,6 +23,12 @@ PYTHON_DISCORD_REPOS = "https://api.github.com/orgs/{repo}/repos" if GITHUB_TOKEN := Tokens.github: REQUEST_HEADERS["Authorization"] = f"token {GITHUB_TOKEN}" +CODE_BLOCK_RE = re.compile( + r"^`([^`\n]+)`" # Inline codeblock + r"|```(.+?)```", # Multiline codeblock + re.DOTALL | re.MULTILINE +) + class FetchIssueErrors(Enum): """Errors returned in fetch issues.""" @@ -54,15 +60,11 @@ class Issues(commands.Cog): @staticmethod def check_in_block(message: discord.Message, repo_issue: str) -> bool: """Check whether the # is in codeblocks.""" - block = ( - re.findall(r"```([\s\S]*)?```", message.content) - or re.findall(r"```*\n([\s\S]*)?\n```", message.content) - or re.findall(r"```*([\s\S]*)?\n```", message.content) - or re.findall(r"```*\n([\s\S]*)?```", message.content) - or re.findall(r"`([\s\S]*)?`", message.content) - ) + block = re.findall(CODE_BLOCK_RE, message.content) - if "#".join(repo_issue.split(" ")) in "".join([*block]): + if not block: + return False + elif "#".join(repo_issue.split(" ")) in "".join([*block[0]]): return True return False -- cgit v1.2.3 From a7ffa0926b47bf40cd034eeb2b5e9d5dcd04798f Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Wed, 27 Jan 2021 10:43:19 +0530 Subject: Add check for in category and in remove overide_channel check and do it without decorator --- bot/constants.py | 8 ++++++++ bot/exts/evergreen/issues.py | 19 ++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) (limited to 'bot/exts/evergreen/issues.py') diff --git a/bot/constants.py b/bot/constants.py index 1d41a53e..ce1ca29a 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -9,6 +9,7 @@ __all__ = ( "AdventOfCode", "Branding", "Channels", + "Categories", "Client", "Colours", "Emojis", @@ -100,6 +101,7 @@ class Channels(NamedTuple): big_brother_logs = 468507907357409333 bot = 267659945086812160 checkpoint_test = 422077681434099723 + organisation = 551789653284356126 devalerts = 460181980097675264 devlog = int(environ.get("CHANNEL_DEVLOG", 622895325144940554)) dev_contrib = 635950537262759947 @@ -128,6 +130,12 @@ class Channels(NamedTuple): voice_chat_1 = 799647045886541885 +class Categories(NamedTuple): + development = 411199786025484308 + devprojects = 787641585624940544 + media = 799054581991997460 + + class Client(NamedTuple): name = "Sir Lancebot" guild = int(environ.get("BOT_GUILD", 267624335836053506)) diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index d2c70d4b..4ab3df2c 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -7,8 +7,7 @@ from enum import Enum import discord from discord.ext import commands, tasks -from bot.constants import Channels, Colours, ERROR_REPLIES, Emojis, Tokens, WHITELISTED_CHANNELS -from bot.utils.decorators import override_in_channel +from bot.constants import Categories, Channels, Colours, ERROR_REPLIES, Emojis, Tokens, WHITELISTED_CHANNELS log = logging.getLogger(__name__) @@ -16,13 +15,19 @@ BAD_RESPONSE = { 404: "Issue/pull request not located! Please enter a valid number!", 403: "Rate limit has been hit! Please try again later!" } + MAX_REQUESTS = 10 REQUEST_HEADERS = dict() -PYTHON_DISCORD_REPOS = "https://api.github.com/orgs/{repo}/repos" +PYTHON_DISCORD_REPOS = "https://api.github.com/orgs/{repo}/repos" if GITHUB_TOKEN := Tokens.github: REQUEST_HEADERS["Authorization"] = f"token {GITHUB_TOKEN}" +WHITELISTED_CATEGORIES = ( + Categories.devprojects, Categories.media, Categories.development +) +WHITELISTED_CHANNELS += (Channels.organisation) + CODE_BLOCK_RE = re.compile( r"^`([^`\n]+)`" # Inline codeblock r"|```(.+?)```", # Multiline codeblock @@ -135,7 +140,6 @@ class Issues(commands.Cog): return resp @commands.command(aliases=("pr",)) - @override_in_channel(WHITELISTED_CHANNELS + (Channels.dev_contrib, Channels.dev_branding)) async def issue( self, ctx: commands.Context, @@ -144,6 +148,9 @@ class Issues(commands.Cog): user: str = "python-discord" ) -> None: """Command to retrieve issue(s) from a GitHub repository.""" + if ctx.channel.category not in WHITELISTED_CATEGORIES or ctx.channel.category in WHITELISTED_CHANNELS: + return + result = await self.fetch_issues(set(numbers), repository, user) if result == FetchIssueErrors.value_error: @@ -166,9 +173,11 @@ class Issues(commands.Cog): await ctx.send(result) @commands.Cog.listener() - @override_in_channel(WHITELISTED_CHANNELS + (Channels.dev_contrib, Channels.dev_branding)) async def on_message(self, message: discord.Message) -> None: """Command to retrieve issue(s) from a GitHub repository using automatic linking if matching #.""" + if message.channel.category not in WHITELISTED_CATEGORIES or message.channel.category in WHITELISTED_CHANNELS: + return + message_repo_issue_map = re.findall(fr".+?({self.repo_regex})#(\d+)", message.content) links = [] -- cgit v1.2.3 From 65260f66e6e7d3a9bff8b19c4a498643d2e664aa Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Wed, 27 Jan 2021 16:58:53 +0530 Subject: Fix return type annotations --- bot/exts/evergreen/issues.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot/exts/evergreen/issues.py') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index 4ab3df2c..8b02e874 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -26,7 +26,7 @@ if GITHUB_TOKEN := Tokens.github: WHITELISTED_CATEGORIES = ( Categories.devprojects, Categories.media, Categories.development ) -WHITELISTED_CHANNELS += (Channels.organisation) +WHITELISTED_CHANNELS += Channels.organisation CODE_BLOCK_RE = re.compile( r"^`([^`\n]+)`" # Inline codeblock @@ -78,7 +78,7 @@ class Issues(commands.Cog): numbers: set, repository: str, user: str - ) -> t.Union[str, list]: + ) -> t.Union[FetchIssueErrors, str, list]: """Retrieve issue(s) from a GitHub repository.""" links = [] if not numbers: -- cgit v1.2.3 From 0ae98d0d79c62cb71cc8287c0b391eef14f284ba Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Sat, 30 Jan 2021 06:50:31 +0530 Subject: Fix channel and category check logic --- bot/exts/evergreen/issues.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'bot/exts/evergreen/issues.py') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index 8b02e874..55ded054 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -26,7 +26,7 @@ if GITHUB_TOKEN := Tokens.github: WHITELISTED_CATEGORIES = ( Categories.devprojects, Categories.media, Categories.development ) -WHITELISTED_CHANNELS += Channels.organisation +WHITELISTED_CHANNELS_ON_MESSAGE = (Channels.organisation,) CODE_BLOCK_RE = re.compile( r"^`([^`\n]+)`" # Inline codeblock @@ -58,7 +58,7 @@ class Issues(commands.Cog): data = await resp.json() for repo in data: self.repos.append(repo["full_name"].split("/")[1]) - self.repo_regex = "|".join(repo for repo in self.repos) + self.repo_regex = "|".join(self.repos) else: log.debug(f"Failed to get latest Pydis repositories. Status code {resp.status}") @@ -148,7 +148,10 @@ class Issues(commands.Cog): user: str = "python-discord" ) -> None: """Command to retrieve issue(s) from a GitHub repository.""" - if ctx.channel.category not in WHITELISTED_CATEGORIES or ctx.channel.category in WHITELISTED_CHANNELS: + if not( + ctx.channel.category.id in WHITELISTED_CATEGORIES + or ctx.channel.id in WHITELISTED_CHANNELS + ): return result = await self.fetch_issues(set(numbers), repository, user) @@ -175,7 +178,10 @@ class Issues(commands.Cog): @commands.Cog.listener() async def on_message(self, message: discord.Message) -> None: """Command to retrieve issue(s) from a GitHub repository using automatic linking if matching #.""" - if message.channel.category not in WHITELISTED_CATEGORIES or message.channel.category in WHITELISTED_CHANNELS: + if not( + message.channel.category.id in WHITELISTED_CATEGORIES + or message.channel.id in WHITELISTED_CHANNELS_ON_MESSAGE + ): return message_repo_issue_map = re.findall(fr".+?({self.repo_regex})#(\d+)", message.content) @@ -183,9 +189,7 @@ class Issues(commands.Cog): if message_repo_issue_map: for repo_issue in message_repo_issue_map: - if self.check_in_block(message, " ".join([*repo_issue])): - continue - else: + if not self.check_in_block(message, " ".join([*repo_issue])): result = await self.fetch_issues({repo_issue[1]}, repo_issue[0], "python-discord") if isinstance(result, list): links.extend(result) -- cgit v1.2.3 From ac44307a4d4c7aabfafee0af89630a0f748fee00 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Sat, 30 Jan 2021 17:19:22 +0530 Subject: Fix PYTHON_DISCORD_REPOS format variable name and give it a better name --- bot/exts/evergreen/issues.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot/exts/evergreen/issues.py') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index 55ded054..72d88d04 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -19,7 +19,7 @@ BAD_RESPONSE = { MAX_REQUESTS = 10 REQUEST_HEADERS = dict() -PYTHON_DISCORD_REPOS = "https://api.github.com/orgs/{repo}/repos" +REPOS_API = "https://api.github.com/orgs/{org}/repos" if GITHUB_TOKEN := Tokens.github: REQUEST_HEADERS["Authorization"] = f"token {GITHUB_TOKEN}" @@ -53,7 +53,7 @@ class Issues(commands.Cog): @tasks.loop(minutes=30) async def get_pydis_repos(self) -> None: """Get all python-discord repositories on github.""" - async with self.bot.http_session.get(PYTHON_DISCORD_REPOS.format(repo="python-discord")) as resp: + async with self.bot.http_session.get(REPOS_API.format(org="python-discord")) as resp: if resp.status == 200: data = await resp.json() for repo in data: -- cgit v1.2.3 From 45af7ff6803e75d70be8d78e232b3d30803ae65d Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Sun, 31 Jan 2021 05:13:35 +0530 Subject: Add mod_meta and mod_tools channels to whitelisted --- bot/constants.py | 2 ++ bot/exts/evergreen/issues.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'bot/exts/evergreen/issues.py') diff --git a/bot/constants.py b/bot/constants.py index ce1ca29a..1234ef3b 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -116,6 +116,8 @@ class Channels(NamedTuple): message_log = 467752170159079424 mod_alerts = 473092532147060736 modlog = 282638479504965634 + mod_meta = 775412552795947058 + mod_tools = 775413915391098921 off_topic_0 = 291284109232308226 off_topic_1 = 463035241142026251 off_topic_2 = 463035268514185226 diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index 72d88d04..f24e0717 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -26,7 +26,7 @@ if GITHUB_TOKEN := Tokens.github: WHITELISTED_CATEGORIES = ( Categories.devprojects, Categories.media, Categories.development ) -WHITELISTED_CHANNELS_ON_MESSAGE = (Channels.organisation,) +WHITELISTED_CHANNELS_ON_MESSAGE = (Channels.organisation, Channels.mod_meta, Channels.mod_tools) CODE_BLOCK_RE = re.compile( r"^`([^`\n]+)`" # Inline codeblock -- cgit v1.2.3 From 8a92054bab4c3dbd0e42d633d11a87461e1189e1 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Mon, 1 Feb 2021 05:31:48 +0530 Subject: Remove brackets from .join --- bot/exts/evergreen/issues.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/evergreen/issues.py') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index f24e0717..72ca6de4 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -189,7 +189,7 @@ class Issues(commands.Cog): if message_repo_issue_map: for repo_issue in message_repo_issue_map: - if not self.check_in_block(message, " ".join([*repo_issue])): + if not self.check_in_block(message, " ".join(repo_issue)): result = await self.fetch_issues({repo_issue[1]}, repo_issue[0], "python-discord") if isinstance(result, list): links.extend(result) -- cgit v1.2.3