Skip to content

Commit e1c2c0f

Browse files
committed
Github action now reports state of execution #3531
1 parent b0d1fba commit e1c2c0f

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242

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

45+
- GitHub Action: adds 2 action outputs to report changes that can be used in other
46+
steps. Outputs: `is-formatted` and `changed-files` (#3531)
47+
4548
### Documentation
4649

4750
<!-- Major changes to documentation and policies. Small docs changes

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: "Files checked are formatted to the current black formatter style."
33+
value: steps
34+
changed-files:
35+
description: "Number of files black changed"
36+
value: steps
3037
branding:
3138
color: "black"
3239
icon: "check-circle"

action/main.py

+25-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": "false", "changed-files": "0"}
21+
1622
run([sys.executable, "-m", "venv", str(ENV_PATH)], check=True)
1723

1824
version_specifier = VERSION
@@ -38,8 +44,25 @@
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)], stderr=PIPE)
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+
stderr=PIPE
52+
)
53+
# Re-emit stderr back to console so that action output is visible to pipeline
54+
# Do note, click will strip terminal control codes if the output is not TTY
55+
# and thus, this will not show colors anymore.
56+
print(proc.stderr, file=sys.stderr, flush=True)
57+
58+
_output = proc.stderr.decode("utf-8")
59+
matches = search(_is_formatted_re, _output, MULTILINE)
60+
if matches:
61+
_outputs["is-formatted"] = "true"
62+
_outputs["changed-files"] = str(matches.group("changed-files"))
63+
64+
with GITHUB_OUTPUT.open("a+", encoding="utf-8") as f:
65+
for k, v in _outputs.items():
66+
f.write(f"{k}={v}\n")
4467

4568
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 `"false"`, set to `"true"` if black changed any files.
82+
83+
### `changed-files`
84+
85+
Defaults to `"0"`, set to string representation of integer value of how many files black
86+
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 == 'true'
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)