diff options
Diffstat (limited to 'bot')
| -rw-r--r-- | bot/constants.py | 6 | ||||
| -rw-r--r-- | bot/seasons/evergreen/issues.py | 79 |
2 files changed, 54 insertions, 31 deletions
diff --git a/bot/constants.py b/bot/constants.py index 0d4321c8..124c48ae 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -86,6 +86,12 @@ class Emojis: terning5 = "<:terning5:431249716328792064>" terning6 = "<:terning6:431249726705369098>" + issue = "<:IssueOpen:629695470327037963>", + issue_closed = "<:IssueClosed:629695470570307614>", + pull_request = "<:PROpen:629695470175780875>", + pull_request_closed = "<:PRClosed:629695470519713818>", + merge = "<:PRMerged:629695470570176522>" + class Lovefest: role_id = int(environ.get("LOVEFEST_ROLE_ID", 542431903886606399)) diff --git a/bot/seasons/evergreen/issues.py b/bot/seasons/evergreen/issues.py index 438ab475..5f1dcfb6 100644 --- a/bot/seasons/evergreen/issues.py +++ b/bot/seasons/evergreen/issues.py @@ -4,10 +4,20 @@ import discord from discord.ext import commands from bot.constants import Colours +from bot.constants import Emojis from bot.decorators import override_in_channel log = logging.getLogger(__name__) +icons = {"issue": Emojis.issue, + "issue-closed": Emojis.issue_closed, + "pull-request": Emojis.pull_request, + "pull-request-closed": Emojis.pull_request_closed, + "merge": Emojis.merge} + +RESP_VALUE = {404: "Issue/pull request not located! Please enter a valid number!", + 403: "rate limit has been hit! Please try again later!"} + class Issues(commands.Cog): """Cog that allows users to retrieve issues from GitHub.""" @@ -15,43 +25,50 @@ class Issues(commands.Cog): def __init__(self, bot: commands.Bot): self.bot = bot - @commands.command(aliases=("issues",)) + @commands.command(aliases=("pr",)) @override_in_channel() - async def issue( - self, ctx: commands.Context, number: int, repository: str = "seasonalbot", user: str = "python-discord" - ) -> None: - """Command to retrieve issues from a GitHub repository.""" - api_url = f"https://api.github.com/repos/{user}/{repository}/issues/{number}" - failed_status = { - 404: f"Issue #{number} doesn't exist in the repository {user}/{repository}.", - 403: f"Rate limit exceeded. Please wait a while before trying again!" - } - - async with self.bot.http_session.get(api_url) as r: - json_data = await r.json() - response_code = r.status + async def issue(self, ctx: commands.Context, number: int, repository: str = "seasonalbot", + user: str = "python-discord") -> None: + """Command to get issues/pull request from GitHub.""" + url = f"https://api.github.com/repos/{user}/{repository}/issues/{str(number)}" + mergeURL = f"https://api.github.com/repos/{user}/{repository}/pulls/{str(number)}/merge" - if response_code in failed_status: - return await ctx.send(failed_status[response_code]) - - repo_url = f"https://github.com/{user}/{repository}" - issue_embed = discord.Embed(colour=Colours.bright_green) - issue_embed.add_field(name="Repository", value=f"[{user}/{repository}]({repo_url})", inline=False) - issue_embed.add_field(name="Issue Number", value=f"#{number}", inline=False) - issue_embed.add_field(name="Status", value=json_data["state"].title()) - issue_embed.add_field(name="Link", value=json_data["html_url"], inline=False) - - description = json_data["body"] - if len(description) > 1024: - placeholder = " [...]" - description = f"{description[:1024 - len(placeholder)]}{placeholder}" + async with self.bot.http_session.get(url) as r: + json_data = await r.json() - issue_embed.add_field(name="Description", value=description, inline=False) + if r.status in RESP_VALUE: + return await ctx.send(f"[{str(r.status)}] {RESP_VALUE.get(r.status)}") + # the original call is made to the issues API endpoint + # if a issue or PR exists then there will be something returned + # if the word 'issues' is present within the response then we can simply pull the data we need from the + # return data received from the API + if "issues" in json_data.get("html_url"): + if json_data.get("state") == "open": + iconURL = icons.get("issue") + else: + iconURL = icons.get("issue_closed") + # if the word 'issues' is not contained within the returned data and there is no error code then we know that + # the requested data is a pr, hence to get the specifics on it we have to call the PR API endpoint allowing us + # to get the specific information in relation to the PR that is not provided via the issues endpoint + else: + async with self.bot.http_session.get(mergeURL) as m: + if json_data.get("state") == "open": + iconURL = icons.get("pull-request") + # when the status is 204 this means that the state of the PR is merged + elif m.status == 204: + iconURL = icons.get("merge") + # else by the process of elimination, the pull request has been closed + else: + iconURL = icons.get("pull-request-closed") - await ctx.send(embed=issue_embed) + resp = discord.Embed(colour=Colours.bright_green) + resp.set_author( + name=f"{iconURL} | [{repository}] #{number} {json_data.get('title')}", + url=json_data.get("html_url")) + await ctx.send(embed=resp) def setup(bot: commands.Bot) -> None: - """Github Issues Cog Load.""" + """Cog Retrieves Issues From Github.""" bot.add_cog(Issues(bot)) log.info("Issues cog loaded") |