From 849c7d3e7d687ecec8dead5ab8f393d5fbdda1ef Mon Sep 17 00:00:00 2001 From: Jani Mikkonen Date: Fri, 27 Jan 2023 11:54:49 +0200 Subject: [PATCH] Github action now reports state of execution #3531 --- CHANGES.md | 2 ++ action.yml | 7 +++++ action/main.py | 25 ++++++++++++++++-- docs/integrations/github_actions.md | 40 +++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 2acb31d6ac4..595f3aae184 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -84,6 +84,8 @@ +- Github Actions: adds 2 variables as github action outputs to report changes that can + be used in other steps. (#3531) - Move 3.11 CI to normal flow now all dependencies support 3.11 (#3446) - Docker: Add new `latest_prerelease` tag automation to follow latest black alpha release on docker images (#3465) diff --git a/action.yml b/action.yml index 35705e99414..a4a5c7e5207 100644 --- a/action.yml +++ b/action.yml @@ -27,6 +27,13 @@ inputs: description: 'Python Version specifier (PEP440) - e.g. "21.5b1"' required: false default: "" +outputs: + is_formatted: + description: "Whether the files were formatted using the black formatter." + value: steps + changed_files: + description: "no. of files black changed" + value: steps branding: color: "black" icon: "check-circle" diff --git a/action/main.py b/action/main.py index ff9d4112aed..61e3b083a14 100644 --- a/action/main.py +++ b/action/main.py @@ -2,9 +2,11 @@ import shlex import sys from pathlib import Path +from re import MULTILINE, search from subprocess import PIPE, STDOUT, run ACTION_PATH = Path(os.environ["GITHUB_ACTION_PATH"]) +GITHUB_OUTPUT = Path(os.environ["GITHUB_OUTPUT"]) ENV_PATH = ACTION_PATH / ".black-env" ENV_BIN = ENV_PATH / ("Scripts" if sys.platform == "win32" else "bin") OPTIONS = os.getenv("INPUT_OPTIONS", default="") @@ -13,6 +15,10 @@ BLACK_ARGS = os.getenv("INPUT_BLACK_ARGS", default="") VERSION = os.getenv("INPUT_VERSION", default="") +_is_formatted_re = r"\s?(?P[0-9]+)\sfiles?\sreformatted(\.|,)\s?" + +_outputs = {"is_formatted": "0", "changed_files": "0"} + run([sys.executable, "-m", "venv", str(ENV_PATH)], check=True) version_specifier = VERSION @@ -38,8 +44,23 @@ base_cmd = [str(ENV_BIN / "black")] if BLACK_ARGS: # TODO: remove after a while since this is deprecated in favour of SRC + OPTIONS. - proc = run([*base_cmd, *shlex.split(BLACK_ARGS)]) + proc = run([*base_cmd, *shlex.split(BLACK_ARGS)], stdout=PIPE, stderr=STDOUT) else: - proc = run([*base_cmd, *shlex.split(OPTIONS), *shlex.split(SRC)]) + proc = run( + [*base_cmd, *shlex.split(OPTIONS), *shlex.split(SRC)], + stdout=PIPE, + stderr=STDOUT, + ) + +_output = proc.stdout.decode("utf-8") + +matches = search(_is_formatted_re, _output, MULTILINE) +if matches: + _outputs["is_formatted"] = "1" + _outputs["changed_files"] = str(matches.group("changed_files")) + +with GITHUB_OUTPUT.open("a+", encoding="utf-8") as f: + for k, v in _outputs.items(): + f.write(f"{k}={v}\n") sys.exit(proc.returncode) diff --git a/docs/integrations/github_actions.md b/docs/integrations/github_actions.md index ebfcc2d95a2..8f806933e34 100644 --- a/docs/integrations/github_actions.md +++ b/docs/integrations/github_actions.md @@ -70,3 +70,43 @@ If you want to match versions covered by Black's src: "./src" version: "~= 22.0" ``` + +## Outputs + +This action will output two variables for further processing of changes black has made +against `src` set. + +### `is_formatted` + +Defaults to `"0"`, set to `"1"` if black changed any files. + +### `changed_files` + +Defaults to `"0"`, set to string representation of integer value of how maney files +black modified. + +### Usage + +One could use either of these output variables to further have conditional steps within +the same pipeline, like creating a pull request after black has done changes to the code +base. + +```yaml +- uses: psf/black@stable + with: + options: "--verbose" + src: "./src" + id: "action_black" + +- name: Create Pull Request + if: steps.action_black.outputs.is_formatted == '1' + uses: peter-evans/create-pull-request@v3 + with: + token: ${{ secrets.GITHUB_TOKEN }} + title: "Format Python code with psf/black push" + commit-message: ":art: Format Python code with psf/black" + body: | + There appear to be some python formatting errors in ${{ github.sha }}. This pull request + uses the [psf/black](https://github.com/psf/black) formatter to fix these issues. + base: ${{ github.head_ref }} # Creates pull request onto pull request or commit branch +```