Skip to content

Commit 849c7d3

Browse files
committed
Github action now reports state of execution #3531
1 parent 196b1f3 commit 849c7d3

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484

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

87+
- Github Actions: adds 2 variables as github action outputs to report changes that can
88+
be used in other steps. (#3531)
8789
- Move 3.11 CI to normal flow now all dependencies support 3.11 (#3446)
8890
- Docker: Add new `latest_prerelease` tag automation to follow latest black alpha
8991
release on docker images (#3465)

action.yml

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ inputs:
2727
description: 'Python Version specifier (PEP440) - e.g. "21.5b1"'
2828
required: false
2929
default: ""
30+
outputs:
31+
is_formatted:
32+
description: "Whether the files were formatted using the black formatter."
33+
value: steps
34+
changed_files:
35+
description: "no. of files black changed"
36+
value: steps
3037
branding:
3138
color: "black"
3239
icon: "check-circle"

action/main.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
import shlex
33
import sys
44
from pathlib import Path
5+
from re import MULTILINE, search
56
from subprocess import PIPE, STDOUT, run
67

78
ACTION_PATH = Path(os.environ["GITHUB_ACTION_PATH"])
9+
GITHUB_OUTPUT = Path(os.environ["GITHUB_OUTPUT"])
810
ENV_PATH = ACTION_PATH / ".black-env"
911
ENV_BIN = ENV_PATH / ("Scripts" if sys.platform == "win32" else "bin")
1012
OPTIONS = os.getenv("INPUT_OPTIONS", default="")
@@ -13,6 +15,10 @@
1315
BLACK_ARGS = os.getenv("INPUT_BLACK_ARGS", default="")
1416
VERSION = os.getenv("INPUT_VERSION", default="")
1517

18+
_is_formatted_re = r"\s?(?P<changed_files>[0-9]+)\sfiles?\sreformatted(\.|,)\s?"
19+
20+
_outputs = {"is_formatted": "0", "changed_files": "0"}
21+
1622
run([sys.executable, "-m", "venv", str(ENV_PATH)], check=True)
1723

1824
version_specifier = VERSION
@@ -38,8 +44,23 @@
3844
base_cmd = [str(ENV_BIN / "black")]
3945
if BLACK_ARGS:
4046
# TODO: remove after a while since this is deprecated in favour of SRC + OPTIONS.
41-
proc = run([*base_cmd, *shlex.split(BLACK_ARGS)])
47+
proc = run([*base_cmd, *shlex.split(BLACK_ARGS)], stdout=PIPE, stderr=STDOUT)
4248
else:
43-
proc = run([*base_cmd, *shlex.split(OPTIONS), *shlex.split(SRC)])
49+
proc = run(
50+
[*base_cmd, *shlex.split(OPTIONS), *shlex.split(SRC)],
51+
stdout=PIPE,
52+
stderr=STDOUT,
53+
)
54+
55+
_output = proc.stdout.decode("utf-8")
56+
57+
matches = search(_is_formatted_re, _output, MULTILINE)
58+
if matches:
59+
_outputs["is_formatted"] = "1"
60+
_outputs["changed_files"] = str(matches.group("changed_files"))
61+
62+
with GITHUB_OUTPUT.open("a+", encoding="utf-8") as f:
63+
for k, v in _outputs.items():
64+
f.write(f"{k}={v}\n")
4465

4566
sys.exit(proc.returncode)

docs/integrations/github_actions.md

+40
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,43 @@ If you want to match versions covered by Black's
7070
src: "./src"
7171
version: "~= 22.0"
7272
```
73+
74+
## Outputs
75+
76+
This action will output two variables for further processing of changes black has made
77+
against `src` set.
78+
79+
### `is_formatted`
80+
81+
Defaults to `"0"`, set to `"1"` if black changed any files.
82+
83+
### `changed_files`
84+
85+
Defaults to `"0"`, set to string representation of integer value of how maney files
86+
black modified.
87+
88+
### Usage
89+
90+
One could use either of these output variables to further have conditional steps within
91+
the same pipeline, like creating a pull request after black has done changes to the code
92+
base.
93+
94+
```yaml
95+
- uses: psf/black@stable
96+
with:
97+
options: "--verbose"
98+
src: "./src"
99+
id: "action_black"
100+
101+
- name: Create Pull Request
102+
if: steps.action_black.outputs.is_formatted == '1'
103+
uses: peter-evans/create-pull-request@v3
104+
with:
105+
token: ${{ secrets.GITHUB_TOKEN }}
106+
title: "Format Python code with psf/black push"
107+
commit-message: ":art: Format Python code with psf/black"
108+
body: |
109+
There appear to be some python formatting errors in ${{ github.sha }}. This pull request
110+
uses the [psf/black](https://github.com/psf/black) formatter to fix these issues.
111+
base: ${{ github.head_ref }} # Creates pull request onto pull request or commit branch
112+
```

0 commit comments

Comments
 (0)