Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[github action] display black result in job summary #3688

Merged
merged 5 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
### Integrations

<!-- For example, Docker, GitHub Actions, pre-commit, editors -->
- Update GitHub Action to display black output in the job summary


### Documentation

Expand Down
7 changes: 4 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ branding:
runs:
using: composite
steps:
- run: |
- name: black
run: |
if [ "$RUNNER_OS" == "Windows" ]; then
python $GITHUB_ACTION_PATH/action/main.py
python $GITHUB_ACTION_PATH/action/main.py | tee -a $GITHUB_STEP_SUMMARY
else
python3 $GITHUB_ACTION_PATH/action/main.py
python3 $GITHUB_ACTION_PATH/action/main.py | tee -a $GITHUB_STEP_SUMMARY
fi
env:
# TODO: Remove once https://github.com/actions/runner/issues/665 is fixed.
Expand Down
20 changes: 15 additions & 5 deletions action/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
describe_name = line[len("describe-name: ") :].rstrip()
break
if not describe_name:
print("::error::Failed to detect action version.", flush=True)
sys.stderr.write("::error::Failed to detect action version.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we care about the newline here you've removed? If so I recommend sticking with print cause I believe it will send correct newline in windows ...

Suggested change
sys.stderr.write("::error::Failed to detect action version.")
print("::error::Failed to detect action version.", file=sys.stderr, flush=True)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does need to be written to stderr so as errors do not appear in the job summary part (which won't parse it correctly) but the annotation one, like this:
screenshot-2023-05-13-1815
I've updated the PR with your feedback

sys.exit(1)
# expected format is one of:
# - 23.1.0
Expand All @@ -53,15 +53,25 @@
)
if pip_proc.returncode:
print(pip_proc.stdout)
print("::error::Failed to install Black.", flush=True)
sys.stderr.write("::error::Failed to install Black.")
tieum marked this conversation as resolved.
Show resolved Hide resolved
sys.exit(pip_proc.returncode)


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,
encoding="utf-8",
)
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,
encoding="utf-8",
)
print(proc.stdout)
sys.exit(proc.returncode)