126 lines
3.7 KiB
Python
126 lines
3.7 KiB
Python
from __future__ import annotations
|
|
|
|
from pyforgejo.api.repository import (
|
|
repo_get,
|
|
repo_list_topics
|
|
)
|
|
|
|
import discord
|
|
from discord import app_commands
|
|
from discord.ext import commands
|
|
|
|
import utils
|
|
|
|
from datetime import datetime
|
|
import json
|
|
from typing import TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from bot import Nijika
|
|
|
|
|
|
class Repo(commands.Cog):
|
|
|
|
def __init__(self, bot: Nijika):
|
|
self.bot: Nijika = bot
|
|
|
|
grp = app_commands.Group(
|
|
name="repo",
|
|
description="Repository related commands"
|
|
)
|
|
|
|
@grp.command(name="get", description="Get a repository")
|
|
@app_commands.describe(
|
|
target=("Target repository. "
|
|
"Example: kita/codeijika"))
|
|
async def repoGet(self, i: discord.Interaction, target: str):
|
|
|
|
targetFmt = target.split("/")
|
|
if not len(targetFmt) == 2:
|
|
return await i.response.send_message(embed=discord.Embed(
|
|
title="Invalid format",
|
|
color=self.bot.error_color
|
|
))
|
|
|
|
res = await repo_get.asyncio_detailed(
|
|
targetFmt[0],
|
|
targetFmt[1],
|
|
client=self.bot.forgejoClient
|
|
)
|
|
|
|
if res.status_code == 404:
|
|
return await i.response.send_message(embed=discord.Embed(
|
|
title="Error",
|
|
description=("Could not find repository. "
|
|
"Please make sure the repository exists and is "
|
|
"not private."),
|
|
color=self.bot.error_color
|
|
))
|
|
|
|
topicRes = await repo_list_topics.asyncio_detailed(
|
|
targetFmt[0],
|
|
targetFmt[1],
|
|
client=self.bot.forgejoClient
|
|
)
|
|
|
|
data = json.loads(res.content)
|
|
topicData = json.loads(topicRes.content)
|
|
|
|
if data["empty"]:
|
|
return await i.response.send_message(
|
|
"The repository you specified does not contain any content.")
|
|
|
|
embed = discord.Embed(
|
|
title=data["full_name"],
|
|
url=data["html_url"],
|
|
color=self.bot.accent_color
|
|
)
|
|
|
|
if data["avatar_url"] != "":
|
|
embed.set_thumbnail(url=data["avatar_url"])
|
|
|
|
embed.set_footer(text=(
|
|
"Created: {created} • "
|
|
"Last updated: {last}").format(
|
|
created=datetime.fromisoformat(data["created_at"]),
|
|
last=datetime.fromisoformat(data["updated_at"])
|
|
))
|
|
|
|
embed.set_author(
|
|
name=data["owner"]["username"],
|
|
url=utils.build_instance_url(
|
|
data["owner"]["username"]
|
|
),
|
|
icon_url=data["owner"]["avatar_url"]
|
|
)
|
|
|
|
if data["language"] != "":
|
|
embed.add_field(
|
|
name="Programming Language", value=data["language"],
|
|
inline=False)
|
|
|
|
embed.add_field(
|
|
name="Stars", value=data["stars_count"]) \
|
|
.add_field(
|
|
name="Forks", value=data["forks_count"]) \
|
|
.add_field(
|
|
name="Watchers", value=data["watchers_count"])
|
|
|
|
if data["has_issues"]:
|
|
embed.add_field(
|
|
name="Issues", value=data["open_issues_count"])
|
|
|
|
if data["has_pull_requests"]:
|
|
embed.add_field(
|
|
name="PRs", value=data["open_pr_counter"])
|
|
|
|
if len(topicData["topics"]) > 0:
|
|
embed.add_field(
|
|
name="Topics", value=", ".join(topicData["topics"]),
|
|
inline=False)
|
|
|
|
return await i.response.send_message(embed=embed)
|
|
|
|
|
|
async def setup(bot: Nijika):
|
|
await bot.add_cog(Repo(bot))
|