Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#63 add command to generate and update GitHub issue templates #132

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions exasol/toolbox/tools/issue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import difflib
import io
from contextlib import ExitStack
from pathlib import Path
from typing import (
Any,
Mapping,
Union,
)

import importlib_resources as resources
import typer
from rich.columns import Columns
from rich.console import Console
from rich.syntax import Syntax

stdout = Console()
stderr = Console(stderr=True)

CLI = typer.Typer()


def _issues() -> Mapping[str, Any]:
pkg = "exasol.toolbox.templates.github.ISSUE_TEMPLATE"

def _normalize(name: str) -> str:
name, ext = name.split(".")
return name

return {_normalize(w.name): w for w in resources.files(pkg).iterdir()} # type: ignore


@CLI.command(name="list")
def list_issues(
columns: bool = typer.Option(
False, "--columns", "-c", help="use column style presentation like `ls`"
)
) -> None:
"""List all available issues."""

class List:
def __init__(self, items: Any):
self.items = items

def __rich__(self) -> str:
return "\n".join(self.items)

output = List(_issues()) if not columns else Columns(_issues(), expand=True)
stdout.print(output)


def _install_issue(
src: Union[str, Path], dest: Union[str, Path], exists_ok: bool = False
) -> None:
src, dest = Path(src), Path(dest)

if dest.exists() and not exists_ok:
raise FileExistsError("Issue already exists")

with ExitStack() as stack:
input_file = stack.enter_context(open(src, "rb"))
output_file = stack.enter_context(open(dest, "wb"))
output_file.write(input_file.read())


def _select_issues(issue: str) -> Mapping[str, Any]:
issues = _issues()
if issue != "all" and issue not in issues:
raise Exception(f"Issue <{issue}> is unknown")
issues = (
issues
if issue == "all"
else {name: path for name, path in issues.items() if issue == name}
)
return issues


@CLI.command(name="install")
def install_issue(
issue: str = typer.Argument("all", help="name of the issue to install."),
dest: Path = typer.Argument(
Path("./.github/ISSUE_TEMPLATE"), help="target directory to install the issue to."
),
) -> None:
"""
Installs the requested issue into the target directory.

Attention: If there is an existing issue with the same name it will be overwritten!
"""
if not dest.exists():
dest.mkdir()

try:
issues = _select_issues(issue)
except Exception as ex:
stderr.print(f"[red]{ex}[/red]")
raise typer.Exit(-1)

for issue, path in issues.items():
destination = dest / f"{issue}.md"
_install_issue(path, destination, exists_ok=True)
stderr.print(f"Installed {issue} in {destination}")
Jannis-Mittenzwei marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 2 additions & 1 deletion exasol/toolbox/tools/tbx.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
from exasol.toolbox.tools import (
security,
workflow,
issue,
)

CLI = typer.Typer()
CLI.add_typer(workflow.CLI, name="workflow", help="Manage github workflows")
CLI.add_typer(security.CLI, name="security", help="Security related helpers")

CLI.add_typer(issue.CLI, name="issue", help="issue templates")
Jannis-Mittenzwei marked this conversation as resolved.
Show resolved Hide resolved

if __name__ == "__main__":
CLI()
4 changes: 2 additions & 2 deletions exasol/toolbox/tools/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ def _install_workflow(
raise FileExistsError("Workflow already exists")

with ExitStack() as stack:
input_file = stack.enter_context(open(src))
output_file = stack.enter_context(open(dest, "w"))
input_file = stack.enter_context(open(src, "rb"))
output_file = stack.enter_context(open(dest, "wb"))
output_file.write(input_file.read())


Expand Down
Loading
Loading