simplify dev environment checks

This commit is contained in:
Riley Housden 2022-08-19 22:21:34 -04:00
parent 9049b5e637
commit bae52e7d7e
Signed by: InValidFire
GPG key ID: 0D6208F6DF56B4D8

View file

@ -17,26 +17,66 @@ async def get_git_status() -> dict:
stdout=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE stderr=asyncio.subprocess.PIPE
) )
br_proc = await asyncio.create_subprocess_exec("git", "rev-parse", "--abbrev-ref", "HEAD",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
dv_proc = await asyncio.create_subprocess_exec("git", "diff", "HEAD",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, _ = await id_proc.communicate() stdout, _ = await id_proc.communicate()
output["commit_id"] = stdout.decode("utf-8") output["commit_id"] = stdout.decode("utf-8")
stdout, _ = await br_proc.communicate() br_proc = await asyncio.create_subprocess_exec("git", "rev-parse", "--abbrev-ref", "HEAD",
output["branch"] = stdout.decode("utf-8") stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, _ = await dv_proc.communicate() stdout, _ = await br_proc.communicate()
if stdout.decode("utf-8") == "": output["branch"] = stdout.decode("utf-8").strip()
output["dev"] = False
else: rm_proc = await asyncio.create_subprocess_exec("git", "branch", "-r",
output["dev"] = True stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, _ = await rm_proc.communicate()
output['remote'] = "remotes/"+stdout.decode("utf-8").lstrip().split("\n")[0]
# diff HEAD to remote
hd_proc = await asyncio.create_subprocess_exec("git", "diff", "HEAD", output['remote'],
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
# diff index to remote
ix_proc = await asyncio.create_subprocess_exec("git", "diff", output['remote'],
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, _ = await ix_proc.communicate()
ix_diff = stdout.decode("utf-8")
# commits ahead/behind remote
ab_proc = await asyncio.create_subprocess_exec("git", "rev-list", "--left-right", "--count", output['branch']+"..."+output["remote"],
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, _ = await ab_proc.communicate()
commits_ahead = stdout.decode("utf-8").split()[0]
commits_behind = stdout.decode("utf-8").split()[1]
stdout, _ = await hd_proc.communicate()
hd_diff = stdout.decode("utf-8")
output['dev'] = False
# dev environment tests
if hd_diff != "": # difference between HEAD and remote
output['dev'] = True
elif config.branch != output["branch"]: # branch has been changed
output['dev'] = True
elif ix_diff != "": # difference between index and remote
output['dev'] = True
elif int(commits_ahead) > 0: # ahead in commits
output['dev'] = True
output['update'] = False
if not output['dev'] and int(commits_behind) > 0:
output['update'] = True
except subprocess.SubprocessError as err: except subprocess.SubprocessError as err:
print(err) print(err)
return output return output
@ -56,10 +96,12 @@ async def ping(ctx: lightbulb.Context) -> None:
async def info(ctx: lightbulb.Context) -> None: async def info(ctx: lightbulb.Context) -> None:
git_status = await get_git_status() git_status = await get_git_status()
embed = hikari.Embed(title="About Me!") embed = hikari.Embed(title="About Me!")
embed.add_field("GitHub", config.git_url) embed.add_field("Git Repository", config.git_url)
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("In Dev-Env?", git_status['dev']) embed.add_field("In Dev-Env?", git_status['dev'])
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):