from __future__ import annotations
import os
import subprocess
import sys
import tempfile
import yaml
SUPPRESSED = "SC2086,SC2046,SC2016"
def main() -> int:
with open("action.yml", encoding="utf-8") as handle:
action = yaml.safe_load(handle)
failed = False
checked = 0
for step in action["runs"]["steps"]:
if step.get("shell") != "bash" or "run" not in step:
continue
checked += 1
script = "#!/usr/bin/env bash\n" + step["run"]
handle = tempfile.NamedTemporaryFile(
"w", suffix=".sh", delete=False, encoding="utf-8"
)
try:
handle.write(script)
handle.close()
result = subprocess.run(
["shellcheck", "--severity=warning", "-e", SUPPRESSED, handle.name],
check=False,
)
if result.returncode != 0:
print(f"::error file=action.yml::shellcheck failed for step {step.get('name', '?')!r}")
failed = True
finally:
os.unlink(handle.name)
if checked == 0:
print("::error file=action.yml::no bash steps found; did the action change shape?")
return 1
print(f"shellcheck OK across {checked} step(s)")
return 1 if failed else 0
if __name__ == "__main__":
sys.exit(main())