Skip to content

Commit

Permalink
action, templates: switch to a template (#24)
Browse files Browse the repository at this point in the history
* action, templates: switch to a template

Closes #23.

Signed-off-by: William Woodruff <william@trailofbits.com>

* action: tweak logging

Signed-off-by: William Woodruff <william@trailofbits.com>

* action: include Markdown table in summary

Signed-off-by: William Woodruff <william@trailofbits.com>
  • Loading branch information
woodruffw committed Aug 3, 2022
1 parent cce8844 commit 4a89a38
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
36 changes: 13 additions & 23 deletions action.py
Expand Up @@ -6,18 +6,23 @@
# is a whitespace-separated list of inputs

import os
import string
import subprocess
import sys
from base64 import b64encode
from pathlib import Path

_OUTPUTS = [sys.stderr]
_HERE = Path(__file__).parent.resolve()
_TEMPLATES = _HERE / "templates"

_SUMMARY = Path(os.getenv("GITHUB_STEP_SUMMARY")).open("a")
_RENDER_SUMMARY = os.getenv("GHA_PIP_AUDIT_SUMMARY", "true") == "true"
_DEBUG = os.getenv("GHA_PIP_AUDIT_INTERNAL_BE_CAREFUL_DEBUG", "false") != "false"

if _RENDER_SUMMARY:
_OUTPUTS.append(_SUMMARY)

def _template(name):
path = _TEMPLATES / f"{name}.md"
return string.Template(path.read_text())


def _summary(msg):
Expand All @@ -31,8 +36,7 @@ def _debug(msg):


def _log(msg):
for output in _OUTPUTS:
print(msg, file=output)
print(msg, file=sys.stderr)


def _pip_audit(*args):
Expand Down Expand Up @@ -127,9 +131,9 @@ def _fatal_help(msg):
_debug(status.stdout)

if status.returncode == 0:
_log("🎉 pip-audit exited successfully")
_summary("🎉 pip-audit exited successfully")
else:
_log("❌ pip-audit found one or more problems")
_summary("❌ pip-audit found one or more problems")

with open("/tmp/pip-audit-output.txt", "r") as io:
output = io.read()
Expand All @@ -140,25 +144,11 @@ def _fatal_help(msg):
print(f"::set-output name=output::{b64encode(output.encode()).decode()}")

_log(output)
_summary(output)


_summary(
"""
<details>
<summary>
Raw `pip-audit` output
</summary>
```
"""
)
_log(status.stdout)
_summary(
"""
```
</details>
"""
)
_summary(_template("pip-audit").substitute(output=status.stdout))

# Normally, we exit with the same code as `pip-audit`, but the user can
# explicitly configure the CI to always pass.
Expand Down
11 changes: 11 additions & 0 deletions templates/pip-audit.md
@@ -0,0 +1,11 @@
<details>

<summary>
Raw <code>pip-audit</code> output
</summary>

```
$output
```

</details>

0 comments on commit 4a89a38

Please sign in to comment.