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

action, templates: switch to a template #24

Merged
merged 3 commits into from Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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>