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

Write output to workflow job summary #105

Merged
merged 5 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all 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: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ repos:
- id: mixed-line-ending
- id: check-added-large-files
- repo: https://github.com/scop/pre-commit-shfmt
rev: v3.4.2-1
rev: v3.5.1-1
hooks:
- id: shfmt
17 changes: 17 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,20 @@ runs:
- run: |
"${{ github.action_path }}/check.sh" "${{ inputs.clang-format-version }}" "${{ inputs.check-path }}" "${{ inputs.fallback-style }}" "${{ inputs.exclude-regex }}"
shell: bash
- name: Save PR head commit SHA
if: failure() && github.event_name == 'pull_request'
shell: bash
run: |
SHA="${{ github.event.pull_request.head.sha }}"
echo "SHA=$SHA" >> $GITHUB_ENV
- name: Save latest commit SHA if not PR
if: failure() && github.event_name != 'pull_request'
shell: bash
run: echo "SHA=${{ github.sha }}" >> $GITHUB_ENV

- name: Report failure in job summary
if: failure()
run: |
DEEPLINK="https://github.com/${{ github.repository }}/commit/${{ env.SHA }}"
echo -e "Format check failed on commit [${GITHUB_SHA:0:8}]($DEEPLINK) with files:\n$(<$GITHUB_WORKSPACE/failing-files.txt)" >> $GITHUB_STEP_SUMMARY
shell: bash
22 changes: 14 additions & 8 deletions check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@
format_diff() {
local filepath="$1"
# Invoke clang-format with dry run and formatting error output
if [[ "$CLANG_FORMAT_VERSION" -gt "9" ]]; then
if [[ $CLANG_FORMAT_VERSION -gt "9" ]]; then
local_format="$(docker run -i -v "$(pwd)":"$(pwd)" -w "$(pwd)" ghcr.io/jidicula/clang-format:"$CLANG_FORMAT_VERSION" -n --Werror --style=file --fallback-style="$FALLBACK_STYLE" "${filepath}")"
else
else # Versions below 9 don't have dry run
formatted="$(docker run -i -v "$(pwd)":"$(pwd)" -w "$(pwd)" ghcr.io/jidicula/clang-format:"$CLANG_FORMAT_VERSION" --style=file --fallback-style="$FALLBACK_STYLE" "${filepath}")"
local_format="$(diff -q <(cat "${filepath}") <(echo "${formatted}"))"
fi

local format_status="$?"
if [[ "${format_status}" -ne 0 ]]; then
echo "Failed on file: $filepath"
if [[ ${format_status} -ne 0 ]]; then
# Append Markdown-bulleted monospaced filepath of failing file to
# summary file.
echo "* \`$filepath\`" >>failing-files.txt

echo "Failed on file: $filepath" >&2
echo "$local_format" >&2
exit_code=1
exit_code=1 # flip the global exit code
return "${format_status}"
fi
return 0
Expand All @@ -49,8 +53,8 @@ fi

cd "$GITHUB_WORKSPACE" || exit 2

if [[ ! -d "$CHECK_PATH" ]]; then
echo "Not a directory in the workspace, fallback to all files."
if [[ ! -d $CHECK_PATH ]]; then
echo "Not a directory in the workspace, fallback to all files." >&2
CHECK_PATH="."
fi

Expand All @@ -68,9 +72,11 @@ src_files=$(find "$CHECK_PATH" -name .git -prune -o -regextype posix-egrep -rege
# check formatting in each source file
for file in $src_files; do
# Only check formatting if the path doesn't match the regex
if ! [[ "${file}" =~ $EXCLUDE_REGEX ]]; then
if ! [[ ${file} =~ $EXCLUDE_REGEX ]]; then
format_diff "${file}"
fi
done

# global exit code is flipped to nonzero if any invocation of `format_diff` has
# a formatting difference.
exit "$exit_code"