|
| 1 | +name: ERB Format Check |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - 'app/views/**/*.erb' |
| 7 | + |
| 8 | +jobs: |
| 9 | + erb_format: |
| 10 | + runs-on: ubuntu-24.04 |
| 11 | + |
| 12 | + steps: |
| 13 | + # Checkout the repo |
| 14 | + - uses: actions/checkout@v2 |
| 15 | + with: |
| 16 | + fetch-depth: 0 |
| 17 | + |
| 18 | + # Install Ruby and run bundler |
| 19 | + - uses: ruby/setup-ruby@v1 |
| 20 | + with: |
| 21 | + ruby-version: '3.0' |
| 22 | + bundler-cache: true |
| 23 | + |
| 24 | + # Fetch base branch so we can get diff of changed 'app/views/**/*.erb' files |
| 25 | + - name: Fetch base branch |
| 26 | + run: git fetch origin ${{ github.base_ref }} |
| 27 | + |
| 28 | + # Gather names of all files within 'app/views/**/*.erb' that have been changed within this PR |
| 29 | + # Store those changed files in the `FILES` ENV variable |
| 30 | + - name: Get list of changed `app/views/**/*.erb` files |
| 31 | + run: | |
| 32 | + FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD -- 'app/views/**/*.erb') |
| 33 | +
|
| 34 | + echo "FILES<<EOF" >> $GITHUB_ENV |
| 35 | + echo "$FILES" >> $GITHUB_ENV |
| 36 | + echo "EOF" >> $GITHUB_ENV |
| 37 | +
|
| 38 | + # Check formatting of all changed `app/views/**/*.erb` files using erb-format. |
| 39 | + # - If any files encounter errors or require formatting, |
| 40 | + # then output these file names and exit with code 1 |
| 41 | + # - Else output that all files are correctly formatted |
| 42 | + - name: Execute erb-format against all changed `app/views/**/*.erb` files |
| 43 | + run: | |
| 44 | + unformatted_files="" |
| 45 | + error_files="" |
| 46 | +
|
| 47 | + IFS=$'\n' |
| 48 | + for file in $FILES; do |
| 49 | + if ! bundle exec erb-format "$file" > /dev/null 2>&1; then |
| 50 | + error_files="${error_files}${file}"$'\n' |
| 51 | + else |
| 52 | + formatted=$(bundle exec erb-format "$file") |
| 53 | + if ! diff -q <(echo "$formatted") "$file" > /dev/null; then |
| 54 | + unformatted_files="${unformatted_files}${file}"$'\n' |
| 55 | + fi |
| 56 | + fi |
| 57 | + done |
| 58 | + unset IFS |
| 59 | +
|
| 60 | + if [ -n "$error_files" ] || [ -n "$unformatted_files" ]; then |
| 61 | + if [ -n "$error_files" ]; then |
| 62 | + echo "⚠️ erb-format encountered errors." |
| 63 | + printf "Please fix the following files so formatting can be validated:\n%s\n" "$error_files" |
| 64 | + fi |
| 65 | + if [ -n "$unformatted_files" ]; then |
| 66 | + echo "❌ Some files are not correctly formatted." |
| 67 | + printf "Please run \`erb-format --write\` on the following files:\n%s\n" "$unformatted_files" |
| 68 | + fi |
| 69 | + exit 1 |
| 70 | + else |
| 71 | + echo "✅ All changed .erb files are correctly formatted." |
| 72 | + fi |
0 commit comments