|
| 1 | +name: Check trailing whitespaces |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [master] |
| 6 | + paths-ignore: |
| 7 | + - '**.png' |
| 8 | + pull_request: |
| 9 | + branches: [master] |
| 10 | + paths-ignore: |
| 11 | + - '**.png' |
| 12 | + |
| 13 | + |
| 14 | +jobs: |
| 15 | + check-trailing-whitespace: |
| 16 | + name: Check trailing whitespaces |
| 17 | + runs-on: [self-hosted] |
| 18 | + steps: |
| 19 | + - uses: actions/checkout@v2 |
| 20 | + |
| 21 | + # Choose the git commit to diff against for the purposes of diff-check. |
| 22 | + # Since this workflow is triggered on both pushes and pull requests, we |
| 23 | + # have to determine if the pull request target branch is set (which it |
| 24 | + # will only be on the PR triggered flow). If it's not, then compare |
| 25 | + # against the last commit. |
| 26 | + - name: choose-commit |
| 27 | + if: ${{ always() }} |
| 28 | + env: |
| 29 | + # Base ref is the target branch, in text form (not hash) |
| 30 | + PR_BASE: ${{ github.base_ref }} |
| 31 | + run: | |
| 32 | + # Determine diff commit name. |
| 33 | + if [[ -z "$PR_BASE" ]]; then |
| 34 | + DIFF_COMMIT_NAME="HEAD^" |
| 35 | + else |
| 36 | + DIFF_COMMIT_NAME="$PR_BASE" |
| 37 | + fi |
| 38 | + echo "DIFF_COMMIT_NAME=$DIFF_COMMIT_NAME" >> $GITHUB_ENV |
| 39 | +
|
| 40 | + # Since we did a shallow fetch for this repo, we must fetch the commit |
| 41 | + # upon which we be diff'ing. The last step set the ref name in the |
| 42 | + # $DIFF_COMMIT_NAME environment variable. When running the fetch, resolve |
| 43 | + # it to the commit hash and pass that hash along to subsequent steps. |
| 44 | + - name: git fetch base commit |
| 45 | + continue-on-error: true |
| 46 | + run: | |
| 47 | + if [[ ! "$DIFF_COMMIT_NAME" == *"HEAD"* ]]; then |
| 48 | + git fetch --recurse-submodules=no origin $DIFF_COMMIT_NAME |
| 49 | + DIFF_COMMIT_SHA=$( git rev-parse origin/$DIFF_COMMIT_NAME ) |
| 50 | + else |
| 51 | + DIFF_COMMIT_SHA=$( git rev-parse $DIFF_COMMIT_NAME ) |
| 52 | + fi |
| 53 | + echo "DIFF_COMMIT=$DIFF_COMMIT_SHA" >> $GITHUB_ENV |
| 54 | +
|
| 55 | + # Run 'git diff --check', comparing against the target commit hash. If |
| 56 | + # diff check fixed anything, fail and output a patch. |
| 57 | + - name: diff-check |
| 58 | + shell: bash |
| 59 | + working-directory: ${{github.workspace}} |
| 60 | + run: | |
| 61 | + git diff --ignore-submodules --check $DIFF_COMMIT |
| 62 | + echo "git diff --check found no whitespace problems" |
| 63 | + exit 0 |
| 64 | +
|
| 65 | +# Cancel workflows when new commit is pushed to a branch other than master. |
| 66 | +concurrency: |
| 67 | + # Use github.run_id on master branch. |
| 68 | + # Use github.event.pull_request.number on pull requests, so it's unique per pull request. |
| 69 | + # Use github.ref on other branches, so it's unique per branch. |
| 70 | + group: ${{ github.workflow }}-${{ github.ref == 'refs/heads/master' && github.run_id || github.event.pull_request.number || github.ref }} |
| 71 | + cancel-in-progress: true |
0 commit comments