|
| 1 | +# This workflow is intended to check if PR conforms with coding standards used |
| 2 | +# in the project. |
| 3 | +# |
| 4 | +# Documentation for GitHub Actions: |
| 5 | +# [workflow-syntax]: https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions |
| 6 | +# [context-and-expression-syntax]: https://docs.github.com/en/free-pro-team@latest/actions/reference/context-and-expression-syntax-for-github-actions |
| 7 | +# [workflow-commands]: https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions |
| 8 | + |
| 9 | +name: Check code style |
| 10 | + |
| 11 | +on: |
| 12 | + pull_request: |
| 13 | + branches: |
| 14 | + - master |
| 15 | + - llvm_release_* |
| 16 | + paths-ignore: # no need to check formatting for documentation and tests |
| 17 | + - 'docs/**' |
| 18 | + - 'test/**' |
| 19 | + |
| 20 | +env: |
| 21 | + # We need compile command database in order to perform clang-tidy check. So, |
| 22 | + # in order to perform configure step we need to setup llvm-dev package. This |
| 23 | + # env variable used to specify desired version of it |
| 24 | + LLVM_VERSION: 12 |
| 25 | + |
| 26 | +jobs: |
| 27 | + clang-format-and-tidy: |
| 28 | + name: Run clang-format & clang-tidy |
| 29 | + runs-on: ubuntu-latest |
| 30 | + steps: |
| 31 | + - name: Checkout sources |
| 32 | + uses: actions/checkout@v2 |
| 33 | + with: |
| 34 | + # In order to gather diff from PR we need to fetch not only the latest |
| 35 | + # commit. Depth of 2 is enough, because GitHub Actions supply us with |
| 36 | + # merge commit as {{ github.sha }}, i.e. the second commit is a merge |
| 37 | + # base between target branch and PR |
| 38 | + fetch-depth: 2 |
| 39 | + |
| 40 | + - name: Gather list of changes |
| 41 | + id: gather-list-of-changes |
| 42 | + run: | |
| 43 | + git diff -U0 --no-color ${{ github.sha }}^ > diff-to-inspect.txt |
| 44 | + if [ -s diff-to-inspect.txt ]; then |
| 45 | + # Here we set an output of our step, which is used later to either |
| 46 | + # perform or skip further steps, i.e. there is no sense to install |
| 47 | + # clang-format if PR hasn't changed .cpp files at all |
| 48 | + # See [workflow-commands] for reference |
| 49 | + echo '::set-output name=HAS_CHANGES::true' |
| 50 | + fi |
| 51 | +
|
| 52 | + - name: Install dependencies |
| 53 | + if: ${{ steps.gather-list-of-changes.outputs.HAS_CHANGES }} |
| 54 | + run: | |
| 55 | + # clang-tidy requires compile command database in order to be properly |
| 56 | + # launched, so, we need to setup llvm package to perform cmake |
| 57 | + # configuration step to generate that database |
| 58 | + curl -L "https://apt.llvm.org/llvm-snapshot.gpg.key" | sudo apt-key add - |
| 59 | + echo "deb https://apt.llvm.org/bionic/ llvm-toolchain-bionic main" | sudo tee -a /etc/apt/sources.list |
| 60 | + sudo apt-get update |
| 61 | + sudo apt-get install -yqq clang-format-9 clang-tidy-9 llvm-${{ env.LLVM_VERSION }}-dev |
| 62 | +
|
| 63 | + - name: Generate compile command database |
| 64 | + if: ${{ steps.gather-list-of-changes.outputs.HAS_CHANGES }} |
| 65 | + run: | |
| 66 | + mkdir build && cd build |
| 67 | + cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release -G "Unix Makefiles" ${{ github.workspace }} |
| 68 | +
|
| 69 | + - name: Run clang-format |
| 70 | + if: ${{ steps.gather-list-of-changes.outputs.HAS_CHANGES }} |
| 71 | + id: run-clang-format |
| 72 | + run: | |
| 73 | + cat diff-to-inspect.txt | /usr/share/clang/clang-format-9/clang-format-diff.py -p1 > clang-format.patch |
| 74 | + if [ -s clang-format.patch ]; then |
| 75 | + echo "clang-format found incorrectly formatted code:" |
| 76 | + cat clang-format.patch; |
| 77 | + exit 1; |
| 78 | + else |
| 79 | + rm clang-format.patch # to avoid uploading empty file |
| 80 | + fi |
| 81 | +
|
| 82 | + - name: Run clang-tidy |
| 83 | + # By some reason, GitHub Actions automatically include "success()" |
| 84 | + # expression into an "if" statement if it doesn't contain any of job |
| 85 | + # status check functions. This is why this and following steps has |
| 86 | + # "always()" and "failure()" in "if" conditions. |
| 87 | + # See "Job status check functions" in [context-and-expression-syntax] |
| 88 | + if: ${{ always() && steps.gather-list-of-changes.outputs.HAS_CHANGES }} |
| 89 | + id: run-clang-tidy |
| 90 | + run: | |
| 91 | + cat diff-to-inspect.txt | /usr/lib/llvm-9/share/clang/clang-tidy-diff.py \ |
| 92 | + -p1 -clang-tidy-binary clang-tidy-9 -quiet \ |
| 93 | + -path ${{ github.workspace}}/build > clang-tidy.log 2>/dev/null |
| 94 | + # By some reason, clang-tidy log contains tons of extra empty lines, |
| 95 | + # that confuse the check below |
| 96 | + sed -i '/^$/d' clang-tidy.log |
| 97 | + if [ -s clang-tidy.log ]; then |
| 98 | + if ! grep -q "No relevant changes found." clang-tidy.log; then |
| 99 | + echo "clang-tidy found incorrectly written code:" |
| 100 | + cat clang-tidy.log |
| 101 | + exit 1 |
| 102 | + else |
| 103 | + rm clang-tidy.log # to avoid uploading empty file |
| 104 | + fi |
| 105 | + fi |
| 106 | +
|
| 107 | + - name: Upload patch with clang-format fixes |
| 108 | + uses: actions/upload-artifact@v2 |
| 109 | + if: ${{ failure() && steps.run-clang-format.outcome == 'failure' }} |
| 110 | + with: |
| 111 | + name: clang-format.patch |
| 112 | + path: clang-format.patch |
| 113 | + if-no-files-found: ignore |
| 114 | + |
| 115 | + - name: Upload clang-tidy log |
| 116 | + uses: actions/upload-artifact@v2 |
| 117 | + if: ${{ failure() && steps.run-clang-tidy.outcome == 'failure' }} |
| 118 | + with: |
| 119 | + name: clang-tidy.log |
| 120 | + path: clang-tidy.log |
| 121 | + if-no-files-found: ignore |
0 commit comments