diff --git a/CHANGES.md b/CHANGES.md index 921fc3efdd8..b30f88074fc 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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) diff --git a/action.yml b/action.yml index d49dc4ac3a0..9d0d7939cd6 100644 --- a/action.yml +++ b/action.yml @@ -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" @@ -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 diff --git a/action/main.py b/action/main.py index 2cabc9772ba..b67428285f6 100644 --- a/action/main.py +++ b/action/main.py @@ -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"\[.*\]") @@ -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)