Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
- Enable 3.14 base CI (#4804)
- Enhance GitHub Action `psf/black` to support the `required-version` major-version-only
"stability" format when using pyproject.toml (#4770)
- Add `output-file` input to GitHub Action `psf/black` to write formatter output to a
file for artifact capture and log cleanliness (#4824)
- Improve error message for vim plugin users. It now handles independently vim version
- Vim: Warn on unsupported Vim and Python versions independently (#4772)
- Vim: Print the import paths when importing black fails (#4675)
Expand Down
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ inputs:
description: "Whether to add the output to the workflow summary"
required: false
default: true
output-file:
description: >
Optional path to write Black output to a file in addition to stdout. Useful for
keeping GitHub Actions logs clean when using --diff or --check.
required: false
default: ""
branding:
color: "black"
icon: "check-circle"
Expand Down Expand Up @@ -75,5 +81,6 @@ runs:
INPUT_BLACK_ARGS: ${{ inputs.black_args }}
INPUT_VERSION: ${{ inputs.version }}
INPUT_USE_PYPROJECT: ${{ inputs.use_pyproject }}
OUTPUT_FILE: ${{ inputs.output-file }}
pythonioencoding: utf-8
shell: bash
12 changes: 12 additions & 0 deletions action/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
BLACK_ARGS = os.getenv("INPUT_BLACK_ARGS", default="")
VERSION = os.getenv("INPUT_VERSION", default="")
USE_PYPROJECT = os.getenv("INPUT_USE_PYPROJECT") == "true"
OUTPUT_FILE = os.getenv("OUTPUT_FILE", default="")

BLACK_VERSION_RE = re.compile(r"^black([^A-Z0-9._-]+.*)$", re.IGNORECASE)
EXTRAS_RE = re.compile(r"\[.*\]")
Expand Down Expand Up @@ -182,5 +183,16 @@ def find_black_version_in_array(array: object) -> Union[str, None]:
encoding="utf-8",
)
shutil.rmtree(ENV_PATH, ignore_errors=True)

# Write output to file if specified
if OUTPUT_FILE:
try:
with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
f.write(proc.stdout)
print(f"Black output written to {OUTPUT_FILE}")
except Exception as e:
print(f"::error::Failed to write output to {OUTPUT_FILE}: {e}", file=sys.stderr)
sys.exit(1)

print(proc.stdout)
sys.exit(proc.returncode)