Skip to content

Commit 666b1b8

Browse files
authored
Check that output file exists before opening (#33)
* action.py: check that output exists before opening * action: log exception, extra flags setting Signed-off-by: Andrew Pan <a@tny.town> * workflows/selftest: regression test for no output Signed-off-by: Andrew Pan <a@tny.town> * issue/32: doc `internal-be-careful-extra-flags` --------- Signed-off-by: Andrew Pan <a@tny.town>
1 parent 9075e93 commit 666b1b8

File tree

5 files changed

+50
-9
lines changed

5 files changed

+50
-9
lines changed

.github/workflows/selftest.yml

+18
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,21 @@ jobs:
8888
PIP_AUDIT_OUTPUT: "${{ steps.pip-audit.outputs.internal-be-careful-output }}"
8989
run: |
9090
grep -E 'pyyaml\s+\|\s+5.1' <<< $(base64 -d <<< "${PIP_AUDIT_OUTPUT}")
91+
selftest-pipaudit-fail:
92+
runs-on: ubuntu-latest
93+
steps:
94+
- uses: actions/checkout@v3
95+
- uses: ./
96+
id: pip-audit
97+
with:
98+
# we do not care about pip-audit's actual output in this test, we just need a file to pass
99+
# in so as to not exercise `pip list` mode.
100+
inputs: ./test/empty.txt
101+
# pass in a fake flag here to reliably trigger the failure we're looking for.
102+
internal-be-careful-extra-flags: --not-a-real-pip-audit-flag
103+
internal-be-careful-allow-failure: true
104+
- name: assert expected output
105+
env:
106+
PIP_AUDIT_OUTPUT: "${{ steps.pip-audit.outputs.internal-be-careful-output }}"
107+
run: |
108+
grep 'pip-audit did not return any output' <<< $(base64 -d <<< "${PIP_AUDIT_OUTPUT}")

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,20 @@ Example
300300
internal-be-careful-debug: true
301301
```
302302

303+
#### `internal-be-careful-extra-flags`
304+
**Default**: `""`
305+
306+
The `internal-be-careful-extra-flags` setting passes the specified flags
307+
to `pip-audit`.
308+
309+
Example:
310+
311+
```yaml
312+
- uses: pypa/gh-action-pip-audit@v1.0.5
313+
with:
314+
internal-be-careful-extra-flags: --not-a-real-pip-audit-flag
315+
```
316+
303317
</details>
304318

305319
## Troubleshooting

action.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
_GITHUB_STEP_SUMMARY = Path(os.getenv("GITHUB_STEP_SUMMARY")).open("a")
1919
_GITHUB_OUTPUT = Path(os.getenv("GITHUB_OUTPUT")).open("a")
2020
_RENDER_SUMMARY = os.getenv("GHA_PIP_AUDIT_SUMMARY", "true") == "true"
21-
_DEBUG = os.getenv("GHA_PIP_AUDIT_INTERNAL_BE_CAREFUL_DEBUG", "false") != "false"
21+
_DEBUG = str(os.getenv("GHA_PIP_AUDIT_INTERNAL_BE_CAREFUL_DEBUG", "false")) != "false"
2222

2323

2424
def _template(name):
@@ -64,7 +64,7 @@ def _fatal_help(msg):
6464
"--desc",
6565
# Write the output to this logfile, which we'll turn into the step summary (if configured).
6666
"--output=/tmp/pip-audit-output.txt",
67-
]
67+
] + os.getenv("GHA_PIP_AUDIT_INTERNAL_BE_CAREFUL_EXTRA_FLAGS").split()
6868

6969
if _DEBUG:
7070
pip_audit_args.append("--verbose")
@@ -135,15 +135,19 @@ def _fatal_help(msg):
135135
else:
136136
_summary("❌ pip-audit found one or more problems")
137137

138-
with open("/tmp/pip-audit-output.txt", "r") as io:
139-
output = io.read()
138+
output = "⚠️ pip-audit did not return any output"
139+
try:
140+
with open("/tmp/pip-audit-output.txt", "r") as io:
141+
output = io.read()
142+
except OSError as ex:
143+
_log(ex)
140144

141-
# This is really nasty: our output contains multiple lines,
142-
# so we can't naively stuff it into an output.
143-
print(f"output={b64encode(output.encode()).decode()}", file=_GITHUB_OUTPUT)
145+
# This is really nasty: our output contains multiple lines,
146+
# so we can't naively stuff it into an output.
147+
print(f"output={b64encode(output.encode()).decode()}", file=_GITHUB_OUTPUT)
144148

145-
_log(output)
146-
_summary(output)
149+
_log(output)
150+
_summary(output)
147151

148152

149153
_log(status.stdout)

action.yml

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ inputs:
5050
description: "run with debug logs (default false)"
5151
required: false
5252
default: false
53+
internal-be-careful-extra-flags:
54+
description: "extra flags to be passed in to pip-audit"
55+
required: false
56+
default: ""
5357
outputs:
5458
internal-be-careful-output:
5559
description: "the column-formatted output from pip-audit, wrapped as base64"
@@ -84,4 +88,5 @@ runs:
8488
GHA_PIP_AUDIT_IGNORE_VULNS: "${{ inputs.ignore-vulns }}"
8589
GHA_PIP_AUDIT_INTERNAL_BE_CAREFUL_ALLOW_FAILURE: "${{ inputs.internal-be-careful-allow-failure }}"
8690
GHA_PIP_AUDIT_INTERNAL_BE_CAREFUL_DEBUG: "${{ inputs.internal-be-careful-debug }}"
91+
GHA_PIP_AUDIT_INTERNAL_BE_CAREFUL_EXTRA_FLAGS: "${{ inputs.internal-be-careful-extra-flags }}"
8792
shell: bash

test/empty.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)