From aea53325adc10e03ead81225e7fe1934c85c452b Mon Sep 17 00:00:00 2001 From: Luca Beltrame Date: Tue, 8 Feb 2022 23:38:26 +0100 Subject: [PATCH] Initial support for aliases maubot's mandatory positional arguments make things a little difficult. --- oscbot/__init__.py | 121 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 119 insertions(+), 2 deletions(-) diff --git a/oscbot/__init__.py b/oscbot/__init__.py index 979fe56..02ff417 100644 --- a/oscbot/__init__.py +++ b/oscbot/__init__.py @@ -36,6 +36,21 @@ STATUS_TEMPLATE = """ {% endfor %} """ +ALIAS_TEMPLATE = """ +### Configured aliases + +{% for alias, contents in repo_aliases.items() %} +#### {{ alias }} + +- project: {{ contents.project }} +- repository: {{ contents.repository }} +- package(s): {{ contents.package }} +- architecture(s): {{ contents.arch }} +- state: {{ contents.state }} + +{% endfor %} +""" + @dataclass class BuildResult: @@ -200,6 +215,103 @@ class OSCBot(Plugin): async def osc(self) -> None: pass + @osc.subcommand("alias", help="Manage aliases") + async def alias(self) -> None: + pass + + @alias.subcommand("list", help="List configured aliases") + async def list_aliases(self, evt: MessageEvent) -> None: + + body = self.template.from_string(ALIAS_TEMPLATE) + + if self.config.get("repo_aliases", None) is None: + await evt.reply("No aliases defined.") + return + + message = body.render(repo_aliases=self.config["repo_aliases"]) + await evt.respond(message, markdown=True) + + @alias.subcommand("edit", help="Edit configured aliases") + @command.argument("alias", "alias name") + @command.argument("project", "project name") + @command.argument("package", "package name", required=False) + @command.argument("repository", "repository", required=False) + @command.argument("arch", "architecture", required=False) + @command.argument("state", "state", required=False) + async def edit_alias(self, evt: MessageEvent, + alias: str, + project: Optional[str] = None, + package: Optional[str] = None, + repository: Optional[str] = None, + arch: Optional[str] = None, + state: Optional[str] = None) -> None: + + if alias not in self.config["repo_aliases"]: + await evt.respond(f"Unknown alias {alias}") + return + + alias_data = self.config["repo_aliases"][alias] + + if project: + alias_data["project"] = project + + if repository: + alias_data["repository"] = repository + + if package: + alias_data["package"] = package + + if arch: + alias_data["arch"] = alias + + if state: + alias_data["state"] = state + + self.config["repo_aliases"][alias] = alias_data + self.config.save() + await evt.reply(f"Alias {alias} successfully changed.") + + @alias.subcommand("create", help="Create a new alias") + @command.argument("alias", "alias name") + @command.argument("project", "project name") + @command.argument("package", "package name", required=False) + @command.argument("repository", "repository", required=False) + @command.argument("arch", "architecture", required=False) + @command.argument("state", "state", required=False) + async def create_alias(self, evt: MessageEvent, + alias: str, + project: Optional[str] = None, + package: Optional[str] = None, + repository: Optional[str] = None, + arch: Optional[str] = None, + state: Optional[str] = None) -> None: + + repository = "all" if not repository else repository + package = "all" if not package else package + arch = "all" if not arch else arch + state = "all" if not state else state + alias_data = { + "project": project, + "package": package, + "repository": repository, + "arch": arch, + "state": state + } + self.config["repo_aliases"][alias] = alias_data + self.config.save() + await evt.reply(f"Alias {alias} successfully created.") + + @alias.subcommand("delete", help="Delete an alias", aliases=("rm", )) + @command.argument("alias", "alias name") + async def delete_alias(self, evt: MessageEvent, alias: str): + if alias not in self.config["repo_aliases"]: + await evt.respond(f"Unknown alias {alias}") + return + + del self.config["repo_aliases"][alias] + self.config.save() + await evt.reply(f"Alias {alias} successfully deleted.") + @osc.subcommand( "rebuildpac", aliases=("rb",), help="Rebuild a package or all packages in the repositories") @@ -213,9 +325,14 @@ class OSCBot(Plugin): repository: Optional[str] = None, arch: Optional[str] = None) -> None: + if project in self.config["repo_aliases"]: + # We're not interested in state and we query package explicitly + project, _, repository, _, arch = self.get_alias(project) + else: + repository = None if repository == "all" else repository + arch = None if arch == "all" else arch + package = None if package == "all" else package - repository = None if repository == "all" else repository - arch = None if arch == "all" else arch result, status = await self.parse_rebuilpac(project, package, repository,