From 189e15b1dd551a15a7e64547683c0a071ab9d152 Mon Sep 17 00:00:00 2001 From: Zevaryx Date: Thu, 22 Jul 2021 11:47:41 -0600 Subject: [PATCH] Add gitlab cog, needs more work, refs #30 --- config.example.yaml | 3 +- jarvis/cogs/gitlab.py | 153 ++++++++++++++++++++++++++++++++++++++++++ jarvis/config.py | 2 + 3 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 jarvis/cogs/gitlab.py diff --git a/config.example.yaml b/config.example.yaml index 0083067..b320cb0 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -7,7 +7,8 @@ password: pass host: localhost port: 27017 - api_urls: + urls: url_name: url url_name2: url2 max_messages: 1000 + gitlab_token: null diff --git a/jarvis/cogs/gitlab.py b/jarvis/cogs/gitlab.py new file mode 100644 index 0000000..0147f24 --- /dev/null +++ b/jarvis/cogs/gitlab.py @@ -0,0 +1,153 @@ +import re +from datetime import datetime + +import gitlab +from discord.ext import commands +from discord_slash import SlashContext, cog_ext +from discord_slash.utils.manage_commands import create_option + +from jarvis.config import get_config +from jarvis.utils import build_embed +from jarvis.utils.field import Field + +guild_ids = [862402786116763668] + + +class GitlabCog(commands.Cog): + def __init__(self, bot): + self.bot = bot + config = get_config() + self._gitlab = gitlab.Gitlab( + "https://git.zevaryx.com", private_token=config.gitlab_token + ) + # J.A.R.V.I.S. GitLab ID is 29 + self.project = self._gitlab.projects.get(29) + + @cog_ext.cog_subcommand( + base="gl", + name="issue", + description="Get an issue from GitLab", + guild_ids=guild_ids, + options=[ + create_option( + name="id", description="Issue ID", option_type=4, required=True + ) + ], + ) + async def _issue(self, ctx: SlashContext, id: int): + try: + issue = self.project.issues.get(int(id)) + except gitlab.exceptions.GitlabGetError: + await ctx.send("Issue does not exist.", hidden=True) + return + assignee = issue.assignee + if assignee: + assignee = assignee["name"] + + created_at = datetime.strptime( + issue.created_at, "%Y-%m-%dT%H:%M:%S.%fZ" + ).strftime("%Y-%m-%d %H:%M:%S UTC") + fields = [ + Field( + name="State", value=issue.state[0].upper() + issue.state[1:] + ), + Field(name="Assignee", value=assignee), + Field(name="Labels", value="\n".join(issue.labels)), + ] + color = self.project.labels.get(issue.labels[0]).color + fields.append(Field(name="Created At", value=created_at)) + if issue.state == "closed": + closed_at = datetime.strptime( + issue.closed_at, "%Y-%m-%dT%H:%M:%S.%fZ" + ).strftime("%Y-%m-%d %H:%M:%S UTC") + fields.append(Field(name="Closed At", value=closed_at)) + if issue.milestone: + fields.append( + Field( + name="Milestone", + value=f"[{issue.milestone['title']}]" + + f"({issue.milestone['web_url']})", + inline=False, + ) + ) + if len(issue.title) > 200: + issue.title = issue.title[:200] + "..." + embed = build_embed( + title=f"[#{issue.iid}] {issue.title}", + description=issue.description, + fields=fields, + color=color, + url=issue.web_url, + ) + embed.set_author( + name=issue.author["name"], + icon_url=issue.author["avatar_url"], + url=issue.author["web_url"], + ) + embed.set_thumbnail( + url="https://about.gitlab.com/images" + + "/press/logo/png/gitlab-icon-rgb.png" + ) + await ctx.send(embed=embed) + + @cog_ext.cog_subcommand( + base="gl", + name="milestone", + description="Get a milestone from GitLab", + guild_ids=guild_ids, + options=[ + create_option( + name="id", + description="Milestone ID", + option_type=4, + required=True, + ) + ], + ) + async def _milestone(self, ctx: SlashContext, id: int): + try: + milestone = self.project.milestones.get(int(id)) + except gitlab.exceptions.GitlabGetError: + await ctx.send("Issue does not exist.", hidden=True) + return + + created_at = datetime.strptime( + milestone.created_at, "%Y-%m-%dT%H:%M:%S.%fZ" + ).strftime("%Y-%m-%d %H:%M:%S UTC") + + fields = [ + Field( + name="State", + value=milestone.state[0].upper() + milestone.state[1:], + ), + Field(name="Start Date", value=milestone.start_date), + Field(name="Due Date", value=milestone.due_date), + Field(name="Created At", value=created_at), + ] + + if milestone.updated_at: + updated_at = datetime.strptime( + milestone.updated_at, "%Y-%m-%dT%H:%M:%S.%fZ" + ).strftime("%Y-%m-%d %H:%M:%S UTC") + fields.append(Field(name="Updated At", value=updated_at)) + + if len(milestone.title) > 200: + milestone.title = milestone.title[:200] + "..." + + embed = build_embed( + title=f"[#{milestone.iid}] {milestone.title}", + description=milestone.description, + fields=fields, + color="#00FFEE", + url=milestone.web_url, + ) + embed.set_thumbnail( + url="https://about.gitlab.com/images" + + "/press/logo/png/gitlab-icon-rgb.png" + ) + await ctx.send(embed=embed) + + +def setup(bot): + if get_config().gitlab_token: + bot.add_cog(GitlabCog(bot)) diff --git a/jarvis/config.py b/jarvis/config.py index 0ba4eb3..3f3883d 100644 --- a/jarvis/config.py +++ b/jarvis/config.py @@ -24,6 +24,7 @@ class Config(object): logo: str, mongo: dict, urls: dict, + gitlab_token: str = None, max_messages: int = 1000, ): self.token = token @@ -32,6 +33,7 @@ class Config(object): self.mongo = mongo self.urls = urls self.max_messages = max_messages + self.gitlab_token = gitlab_token db = DBManager(config=mongo).mongo.jarvis.config db_config = db.find() for item in db_config: