diff --git a/precli/cli/main.py b/precli/cli/main.py index 2b245a4..527ae2b 100644 --- a/precli/cli/main.py +++ b/precli/cli/main.py @@ -22,6 +22,7 @@ from precli.core import loader from precli.core.artifact import Artifact from precli.core.run import Run +from precli.renderers import Renderer BUSL_URL = "https://spdx.org/licenses/BUSL-1.1.html" @@ -174,6 +175,11 @@ def setup_arg_parser(): f"file or directory: '{target}'" ) + if args.gist and not os.getenv("GITHUB_TOKEN"): + parser.error( + f"argument --gist: environment variable GITHUB_TOKEN undefined" + ) + return args @@ -214,15 +220,8 @@ def discover_files(targets: list[str], recursive: bool) -> list[Artifact]: return artifacts -def create_gist(file, renderer: str): - if renderer == "json": - filename = "results.json" - elif renderer == "plain": - filename = "results.txt" - elif renderer == "markdown": - filename = "results.md" - elif renderer == "detailed": - filename = "results.txt" +def create_gist(file, renderer: Renderer): + filename = f"results.{renderer.file_extension()}" with open(file.name, encoding="utf-8") as f: file_content = f.read() diff --git a/precli/renderers/__init__.py b/precli/renderers/__init__.py index 88c949a..a1a25f7 100644 --- a/precli/renderers/__init__.py +++ b/precli/renderers/__init__.py @@ -12,6 +12,10 @@ def __init__(self, console: Console, quiet: bool): self.console = console self.quiet = quiet + @abstractmethod + def file_extension(self) -> str: + pass + @abstractmethod def render(self, run: Run): pass diff --git a/precli/renderers/detailed.py b/precli/renderers/detailed.py index 4e2b34e..a38eb08 100644 --- a/precli/renderers/detailed.py +++ b/precli/renderers/detailed.py @@ -12,6 +12,9 @@ class Detailed(Renderer): + def file_extension(self) -> str: + return ".txt" + def render(self, run: Run): for result in run.results: if result.level == Level.ERROR: diff --git a/precli/renderers/json.py b/precli/renderers/json.py index f4bd1dc..37e971e 100644 --- a/precli/renderers/json.py +++ b/precli/renderers/json.py @@ -131,6 +131,9 @@ def get_extensions(self) -> list: ) return precli_exts + def file_extension(self) -> str: + return ".json" + def render(self, run: Run): log = sarif_om.SarifLog( schema_uri=SCHEMA_URI, diff --git a/precli/renderers/markdown.py b/precli/renderers/markdown.py index 107651c..3f59641 100644 --- a/precli/renderers/markdown.py +++ b/precli/renderers/markdown.py @@ -16,6 +16,9 @@ class Markdown(Renderer): + def file_extension(self) -> str: + return ".md" + def render(self, run: Run): output = "" for result in run.results: diff --git a/precli/renderers/plain.py b/precli/renderers/plain.py index 1a838b3..9581903 100644 --- a/precli/renderers/plain.py +++ b/precli/renderers/plain.py @@ -9,6 +9,9 @@ class Plain(Renderer): + def file_extension(self) -> str: + return ".txt" + def render(self, run: Run): for result in run.results: rule = Rule.get_by_id(result.rule_id) diff --git a/tests/unit/cli/test_main.py b/tests/unit/cli/test_main.py index ff82892..2e0edfd 100644 --- a/tests/unit/cli/test_main.py +++ b/tests/unit/cli/test_main.py @@ -86,6 +86,14 @@ def test_main_more_than_one_renderer(self, monkeypatch, capsys): captured = capsys.readouterr() assert "not allowed with argument" in captured.err + def test_main_gist_no_github_token(self, monkeypatch, capsys): + monkeypatch.setattr("sys.argv", ["precli", ".", "--gist"]) + with pytest.raises(SystemExit) as excinfo: + main.main() + assert excinfo.value.code == 2 + captured = capsys.readouterr() + assert "environment variable GITHUB_TOKEN undefined" in captured.err + def test_main_version(self, monkeypatch, capsys): monkeypatch.setattr("sys.argv", ["precli", "--version"]) with pytest.raises(SystemExit) as excinfo: