Skip to content

Commit

Permalink
Fix --gist argument failure
Browse files Browse the repository at this point in the history
This change fixes --gist argument which was raising a
traceback. Also adds a unit test of the condition where
--gist is specified but GITHUB_TOKEN is undefined.

Signed-off-by: Eric Brown <eric.brown@securesauce.dev>
  • Loading branch information
ericwb committed Nov 1, 2024
1 parent 6351796 commit b6e80c1
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 9 deletions.
17 changes: 8 additions & 9 deletions precli/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions precli/renderers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions precli/renderers/detailed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions precli/renderers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions precli/renderers/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions precli/renderers/plain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/cli/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit b6e80c1

Please sign in to comment.