Add branch command
This commit is contained in:
parent
a2f179ae35
commit
7307aafaff
1 changed files with 62 additions and 11 deletions
|
@ -9,6 +9,7 @@ config = load_config()
|
||||||
|
|
||||||
plugin = lightbulb.Plugin("SystemPlugin")
|
plugin = lightbulb.Plugin("SystemPlugin")
|
||||||
|
|
||||||
|
|
||||||
async def create_subprocess(*args):
|
async def create_subprocess(*args):
|
||||||
proc = await asyncio.create_subprocess_exec(*args,
|
proc = await asyncio.create_subprocess_exec(*args,
|
||||||
stdout=asyncio.subprocess.PIPE,
|
stdout=asyncio.subprocess.PIPE,
|
||||||
|
@ -17,24 +18,30 @@ async def create_subprocess(*args):
|
||||||
stdout, _ = await proc.communicate()
|
stdout, _ = await proc.communicate()
|
||||||
return stdout.decode("utf-8").strip()
|
return stdout.decode("utf-8").strip()
|
||||||
|
|
||||||
|
|
||||||
async def get_git_latest_commit_id():
|
async def get_git_latest_commit_id():
|
||||||
return await create_subprocess("git", "rev-parse", "--short", "HEAD")
|
return await create_subprocess("git", "rev-parse", "--short", "HEAD")
|
||||||
|
|
||||||
|
|
||||||
async def get_git_branch():
|
async def get_git_branch():
|
||||||
return await create_subprocess("git", "rev-parse", "--abbrev-ref", "HEAD")
|
return await create_subprocess("git", "rev-parse", "--abbrev-ref", "HEAD")
|
||||||
|
|
||||||
|
|
||||||
async def get_git_remote():
|
async def get_git_remote():
|
||||||
remote = await create_subprocess("git", "branch", "-vv")
|
remote = await create_subprocess("git", "branch", "-vv")
|
||||||
return remote.split("[")[1].split("]")[0]
|
return remote.split("[")[1].split("]")[0]
|
||||||
|
|
||||||
|
|
||||||
async def get_git_head_diff_branch(branch: str) -> str:
|
async def get_git_head_diff_branch(branch: str) -> str:
|
||||||
# diff HEAD to remote
|
# diff HEAD to remote
|
||||||
return await create_subprocess("git", "diff", "head", branch)
|
return await create_subprocess("git", "diff", "head", branch)
|
||||||
|
|
||||||
|
|
||||||
async def get_git_index_diff_branch(branch: str) -> str:
|
async def get_git_index_diff_branch(branch: str) -> str:
|
||||||
# diff index to remote
|
# diff index to remote
|
||||||
return await create_subprocess("git", "diff", branch)
|
return await create_subprocess("git", "diff", branch)
|
||||||
|
|
||||||
|
|
||||||
async def get_git_commits_ahead_behind(branch: str, remote: str) -> tuple[int, int]:
|
async def get_git_commits_ahead_behind(branch: str, remote: str) -> tuple[int, int]:
|
||||||
# commits ahead/behind remote
|
# commits ahead/behind remote
|
||||||
output = await create_subprocess("git", "rev-list", "--left-right", "--count", branch+"..."+remote)
|
output = await create_subprocess("git", "rev-list", "--left-right", "--count", branch+"..."+remote)
|
||||||
|
@ -42,24 +49,30 @@ async def get_git_commits_ahead_behind(branch: str, remote: str) -> tuple[int, i
|
||||||
commits_behind = int(output.split()[1])
|
commits_behind = int(output.split()[1])
|
||||||
return commits_ahead, commits_behind
|
return commits_ahead, commits_behind
|
||||||
|
|
||||||
|
|
||||||
async def get_git_dev(remote: str, branch: str) -> bool:
|
async def get_git_dev(remote: str, branch: str) -> bool:
|
||||||
# dev environment tests
|
# dev environment tests
|
||||||
if await get_git_head_diff_branch(remote) != "": # difference between HEAD and remote
|
# difference between HEAD and remote
|
||||||
|
if await get_git_head_diff_branch(remote) != "":
|
||||||
return True
|
return True
|
||||||
elif config.git_branch != branch: # branch has been changed
|
elif config.git_branch != branch: # branch has been changed
|
||||||
return True
|
return True
|
||||||
elif await get_git_index_diff_branch(remote) != "": # difference between index and remote
|
# difference between index and remote
|
||||||
|
elif await get_git_index_diff_branch(remote) != "":
|
||||||
return True
|
return True
|
||||||
elif (await get_git_commits_ahead_behind(branch, remote))[0] > 0: # ahead in commits
|
# ahead in commits
|
||||||
|
elif (await get_git_commits_ahead_behind(branch, remote))[0] > 0:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
async def get_git_update(dev_status: bool, branch, remote) -> bool:
|
async def get_git_update(dev_status: bool, branch, remote) -> bool:
|
||||||
commits_behind, _ = await get_git_commits_ahead_behind(branch, remote)
|
commits_behind, _ = await get_git_commits_ahead_behind(branch, remote)
|
||||||
if not dev_status and commits_behind > 0:
|
if not dev_status and commits_behind > 0:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
async def get_git_status() -> dict:
|
async def get_git_status() -> dict:
|
||||||
output = {}
|
output = {}
|
||||||
output["commit_id"] = await get_git_latest_commit_id()
|
output["commit_id"] = await get_git_latest_commit_id()
|
||||||
|
@ -69,6 +82,7 @@ async def get_git_status() -> dict:
|
||||||
output['update'] = await get_git_update(output['dev'], output['branch'], output['remote'])
|
output['update'] = await get_git_update(output['dev'], output['branch'], output['remote'])
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
@plugin.command
|
@plugin.command
|
||||||
@lightbulb.command("ping", "pong!", ephemeral=True)
|
@lightbulb.command("ping", "pong!", ephemeral=True)
|
||||||
@lightbulb.implements(lightbulb.SlashCommand)
|
@lightbulb.implements(lightbulb.SlashCommand)
|
||||||
|
@ -78,6 +92,7 @@ async def ping(ctx: lightbulb.Context) -> None:
|
||||||
)
|
)
|
||||||
await ctx.respond(embed)
|
await ctx.respond(embed)
|
||||||
|
|
||||||
|
|
||||||
@plugin.command
|
@plugin.command
|
||||||
@lightbulb.add_checks(lightbulb.owner_only)
|
@lightbulb.add_checks(lightbulb.owner_only)
|
||||||
@lightbulb.command("update", "update the bot!", ephemeral=True)
|
@lightbulb.command("update", "update the bot!", ephemeral=True)
|
||||||
|
@ -91,6 +106,41 @@ async def update(ctx: lightbulb.Context) -> None:
|
||||||
await ctx.respond(embed)
|
await ctx.respond(embed)
|
||||||
await ctx.bot.close()
|
await ctx.bot.close()
|
||||||
|
|
||||||
|
|
||||||
|
@plugin.command
|
||||||
|
@lightbulb.add_checks(lightbulb.owner_only)
|
||||||
|
@lightbulb.command("branch", "get or set the working branch the bot uses.", ephemeral=True)
|
||||||
|
@lightbulb.implements(lightbulb.SlashCommandGroup)
|
||||||
|
async def branch(ctx: lightbulb.Context) -> None:
|
||||||
|
output = await create_subprocess("git", "rev-parse", "--symbolic-full-name", "--abbrev-ref", "HEAD")
|
||||||
|
embed = hikari.Embed(title="Current Branch",
|
||||||
|
description=f"Currently on branch '{output}'")
|
||||||
|
await ctx.respond(embed=embed)
|
||||||
|
|
||||||
|
|
||||||
|
@branch.child
|
||||||
|
@lightbulb.option("name", "name of the branch", type=str, required=True)
|
||||||
|
@lightbulb.command("switch", "switch branches")
|
||||||
|
@lightbulb.implements(lightbulb.SlashCommand)
|
||||||
|
async def switch(ctx: lightbulb.Context) -> None:
|
||||||
|
embed = hikari.Embed(title="Restarting",
|
||||||
|
description="Restarting to switch branches!"
|
||||||
|
)
|
||||||
|
output = await create_subprocess("git", "switch", ctx.options.branch)
|
||||||
|
if "invalid reference" in output: # parsing output allows us more specificity.
|
||||||
|
embed.title = "Branch does not exist."
|
||||||
|
embed.description = "Please check your spelling."
|
||||||
|
await ctx.respond(embed)
|
||||||
|
return
|
||||||
|
elif "Already on" in output:
|
||||||
|
embed.title = f"Already on branch '{ctx.options.branch}'"
|
||||||
|
embed.description = "Just double checking, I presume?"
|
||||||
|
await ctx.respond(embed)
|
||||||
|
return
|
||||||
|
await ctx.respond(embed)
|
||||||
|
await ctx.bot.close()
|
||||||
|
|
||||||
|
|
||||||
@plugin.command
|
@plugin.command
|
||||||
@lightbulb.command("info", "get bot information such as the version, and repository link.", ephemeral=True)
|
@lightbulb.command("info", "get bot information such as the version, and repository link.", ephemeral=True)
|
||||||
@lightbulb.implements(lightbulb.SlashCommand)
|
@lightbulb.implements(lightbulb.SlashCommand)
|
||||||
|
@ -101,12 +151,13 @@ async def info(ctx: lightbulb.Context) -> None:
|
||||||
embed.add_field("Version", git_status['commit_id'], inline=True)
|
embed.add_field("Version", git_status['commit_id'], inline=True)
|
||||||
embed.add_field("Branch", git_status['branch'], inline=True)
|
embed.add_field("Branch", git_status['branch'], inline=True)
|
||||||
embed.add_field("Remote", git_status['remote'], inline=True)
|
embed.add_field("Remote", git_status['remote'], inline=True)
|
||||||
embed.add_field("In Dev-Env?", git_status['dev'])
|
|
||||||
embed.add_field("Needs Update?", git_status['update'])
|
embed.add_field("Needs Update?", git_status['update'])
|
||||||
await ctx.respond(embed)
|
await ctx.respond(embed)
|
||||||
|
|
||||||
|
|
||||||
def load(bot: lightbulb.BotApp):
|
def load(bot: lightbulb.BotApp):
|
||||||
bot.add_plugin(plugin)
|
bot.add_plugin(plugin)
|
||||||
|
|
||||||
|
|
||||||
def unload(bot: lightbulb.BotApp):
|
def unload(bot: lightbulb.BotApp):
|
||||||
bot.remove_plugin(plugin)
|
bot.remove_plugin(plugin)
|
||||||
|
|
Loading…
Reference in a new issue