Skip to content

Commit

Permalink
feat(inputs): Add custom file regex Action input (#125)
Browse files Browse the repository at this point in the history
This change factors out the existing C/C++/Protobuf/CUDA regex into a variable
that's exposed to a new input in action.yml.

Resolves: #79
  • Loading branch information
jidicula committed Dec 27, 2022
1 parent 8d36a2e commit 134f658
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ You can sponsor me [here](https://github.com/sponsors/jidicula)!
* Available values: `LLVM`, `Google`, `Chromium`, `Mozilla`, `WebKit` and others listed in the `clang-format` [docs for BasedOnStyle](https://clang.llvm.org/docs/ClangFormatStyleOptions.html#configurable-format-style-options).
* `exclude-regex` [optional]: A regex to exclude files or directories that should not be checked.
* Default: `^$`
* Pattern matching is done with a real regex, not a glob expression. You can exclude multiple patterns like this: `(hello|world)`.
* Pattern matching is done with a POSIX `grep -E` extended regex, **not** a glob expression. You can exclude multiple patterns like this: `(hello|world)`. Build and verify your regex at https://regex101.com .
* `include-regex` [optional]: A regex to include files or directories that should be checked.
* Default: `^.*\.((((c|C)(c|pp|xx|\+\+)?$)|((h|H)h?(pp|xx|\+\+)?$))|(ino|pde|proto|cu))$`
* Pattern matching is done with a POSIX `grep -E` extended regex, **not** a glob expression. You can exclude multiple patterns like this: `(hello|world)`. Build and verify your regex at https://regex101.com .

This action checks all C/C++/Protobuf (including Arduino `.ino` and `.pde`) files in the provided directory in the GitHub workspace are formatted correctly using `clang-format`. If no directory is provided or the provided path is not a directory in the GitHub workspace, all C/C++/Protobuf files are checked.

The following file extensions are checked:
The following file extensions are checked by default:
* Header files:
* `.h`
* `.H`
Expand Down Expand Up @@ -71,7 +74,7 @@ The following file extensions are checked:

# Usage

⚠️This action does not run on `windows` GitHub Actions runners!
⚠️This action does not run on `windows` GitHub Actions runners!

## Single Path

Expand Down
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ inputs:
description: 'The fallback style for clang-format if no .clang-format file exists in your repository.'
required: false
default: 'llvm'
include-regex:
description: 'A regex to override the C/C++/Protobuf/CUDA filetype regex. that should be checked. Default results in the regex defined in `check.sh`.'
required: false
default: ''

runs:
using: "composite"
steps:
- run: |
"${{ github.action_path }}/check.sh" "${{ inputs.clang-format-version }}" "${{ inputs.check-path }}" "${{ inputs.fallback-style }}" "${{ inputs.exclude-regex }}"
"${{ github.action_path }}/check.sh" "${{ inputs.clang-format-version }}" "${{ inputs.check-path }}" "${{ inputs.fallback-style }}" "${{ inputs.exclude-regex }} "${{ inputs.include-regex }}""

This comment has been minimized.

Copy link
@scosman

scosman Dec 28, 2022

Looks like quoting issue here? The close quote for exclude regex is missing, and extra quote at the end.

I'm getting an error when I run it:

Run "/home/runner/work/_actions/jidicula/clang-format-action/v4.10.0/check.sh" "13" "ios/voicebox" "WebKit" " "^.*\.((((c|C)(c|pp|xx|\+\+)?$)|((h|H)h?(pp|xx|\+\+)?$))|(ino|pde|proto|cu|m))$""
(https://github.com/scosman/voicebox/actions/runs/3794978966/jobs/6453615308#step:3:9)
  "/home/runner/work/_actions/jidicula/clang-format-action/v4.10.0/check.sh" "13" "ios/voicebox" "WebKit" " "^.*\.((((c|C)(c|pp|xx|\+\+)?$)|((h|H)h?(pp|xx|\+\+)?$))|(ino|pde|proto|cu|m))$""
(https://github.com/scosman/voicebox/actions/runs/3794978966/jobs/6453615308#step:3:10)
  shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
[10](https://github.com/scosman/voicebox/actions/runs/3794978966/jobs/6453615308#step:3:12)
/home/runner/work/_temp/da47c41e-2fa6-4813-b279-8ebb7608ab19.sh: line 1: syntax error near unexpected token `('
[11](https://github.com/scosman/voicebox/actions/runs/3794978966/jobs/6453615308#step:3:13)
Error: Process completed with exit code 2.

This comment has been minimized.

Copy link
@jidicula

jidicula Dec 28, 2022

Author Owner

oops 😬

I'll get a fix out right away

This comment has been minimized.

Copy link
@scosman

scosman Dec 28, 2022

🎉 🙏

shell: bash
- name: Save PR head commit SHA
if: failure() && github.event_name == 'pull_request'
Expand Down
22 changes: 14 additions & 8 deletions check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,24 @@ CLANG_FORMAT_VERSION="$1"
CHECK_PATH="$2"
FALLBACK_STYLE="$3"
EXCLUDE_REGEX="$4"
INCLUDE_REGEX="$5"

# Set the regex to an empty string regex if nothing was provided
if [ -z "$EXCLUDE_REGEX" ]; then
if [[ -z $EXCLUDE_REGEX ]]; then
EXCLUDE_REGEX="^$"
fi

# Set the filetype regex if nothing was provided.
# Find all C/C++/Protobuf/CUDA files:
# h, H, hpp, hh, h++, hxx
# c, C, cpp, cc, c++, cxx
# ino, pde
# proto
# cu
if [[ -z $INCLUDE_REGEX ]]; then
INCLUDE_REGEX='^.*\.((((c|C)(c|pp|xx|\+\+)?$)|((h|H)h?(pp|xx|\+\+)?$))|(ino|pde|proto|cu))$'
fi

cd "$GITHUB_WORKSPACE" || exit 2

if [[ ! -d $CHECK_PATH ]]; then
Expand All @@ -62,13 +74,7 @@ fi
exit_code=0

# All files improperly formatted will be printed to the output.
# find all C/C++/Protobuf/CUDA files:
# h, H, hpp, hh, h++, hxx
# c, C, cpp, cc, c++, cxx
# ino, pde
# proto
# cu
src_files=$(find "$CHECK_PATH" -name .git -prune -o -regextype posix-egrep -regex '^.*\.((((c|C)(c|pp|xx|\+\+)?$)|((h|H)h?(pp|xx|\+\+)?$))|(ino|pde|proto|cu))$' -print)
src_files=$(find "$CHECK_PATH" -name .git -prune -o -regextype posix-egrep -regex "$INCLUDE_REGEX" -print)

# check formatting in each source file
for file in $src_files; do
Expand Down
30 changes: 28 additions & 2 deletions test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,44 @@ FALLBACK_STYLE="llvm"
EXCLUDE_REGEX="capital"
CLANG_FORMAT_VERSION="$1"

###############################################################################
# Default C/C++/Protobuf/CUDA regex #
###############################################################################

# should succeed
"$GITHUB_WORKSPACE"/check.sh "$CLANG_FORMAT_VERSION" "$GITHUB_WORKSPACE/test/known_pass" "$FALLBACK_STYLE" "$EXCLUDE_REGEX"
docker_status="$?"
if [[ "$docker_status" != "0" ]]; then
if [[ $docker_status != "0" ]]; then
echo "files that should succeed have failed!"
exit 1
fi

# should fail
"$GITHUB_WORKSPACE"/check.sh "$CLANG_FORMAT_VERSION" "$GITHUB_WORKSPACE/test/known_fail" "$FALLBACK_STYLE" "$EXCLUDE_REGEX"
docker_status="$?"
if [[ "$docker_status" == "0" ]]; then
if [[ $docker_status == "0" ]]; then
echo "files that should fail have succeeded!"
exit 2
fi

###############################################################################
# Custom filetype regex #
###############################################################################

INCLUDE_REGEX='^.*\.(c|C)'

# should succeed
"$GITHUB_WORKSPACE"/check.sh "$CLANG_FORMAT_VERSION" "$GITHUB_WORKSPACE/test/known_pass" "$FALLBACK_STYLE" "$EXCLUDE_REGEX" "$INCLUDE_REGEX"
docker_status="$?"
if [[ $docker_status != "0" ]]; then
echo "files that should succeed have failed!"
exit 1
fi

# should fail
"$GITHUB_WORKSPACE"/check.sh "$CLANG_FORMAT_VERSION" "$GITHUB_WORKSPACE/test/known_fail" "$FALLBACK_STYLE" "$EXCLUDE_REGEX" "$INCLUDE_REGEX"
docker_status="$?"
if [[ $docker_status == "0" ]]; then
echo "files that should fail have succeeded!"
exit 2
fi

0 comments on commit 134f658

Please sign in to comment.