Skip to content

Commit

Permalink
Github action now reports state of execution #3531
Browse files Browse the repository at this point in the history
  • Loading branch information
rasjani committed Jan 30, 2023
1 parent 196b1f3 commit 849c7d3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@

<!-- For example, Docker, GitHub Actions, pre-commit, editors -->

- 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)
Expand Down
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
25 changes: 23 additions & 2 deletions action/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="")
Expand All @@ -13,6 +15,10 @@
BLACK_ARGS = os.getenv("INPUT_BLACK_ARGS", default="")
VERSION = os.getenv("INPUT_VERSION", default="")

_is_formatted_re = r"\s?(?P<changed_files>[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
Expand All @@ -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)
40 changes: 40 additions & 0 deletions docs/integrations/github_actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

0 comments on commit 849c7d3

Please sign in to comment.