ZedThree / clang-tidy-review

Create a pull request review based on clang-tidy warnings
MIT License
88 stars 44 forks source link

Running clang-tidy-review outside of container #122

Open bwrsandman opened 4 months ago

bwrsandman commented 4 months ago

I wanted to post this in discussions but it's not enabled for the repo.

Here's how I am able to run the clang-tidy review script directly on the github runner environments without needing the ubuntu container. Why would you do this?

  1. Saves time spent each run fetching the docker and installing packages with apt
  2. You can take better advantage of github caches
  3. You can run this on any of the runners that github provides or that you host yourself.
  4. This works on different CI workflows than github actions
  5. You can use any version of clang-tidy that is available to you in this environment.
  6. Overall seems to run much faster
name: Clang Tidy
on:
  pull_request_target:  # Required to post comments on PR from forks
jobs:
  clang-tidy-review:
    runs-on: ubuntu-latest
    if: startsWith(github.event_name, 'pull_request')  # Posting a review only makes sense on PRs
    steps:
      # Repo config for pull_request_target
      - uses: actions/checkout@v3
        with:
          ref: ${{ github.event.pull_request.head.ref }}
          repository: ${{ github.event.pull_request.head.repo.full_name }}
          submodules: recursive

      # Generate compilation database and make sure to have the source and all third party headers available

      # Checkout the review source for the python package
      - name: Checkout clang-tidy-review
        uses: actions/checkout@v4
        with:
          repository: ZedThree/clang-tidy-review
          ref: master
          path: clang-tidy-review

      - name: Install LLVM and Clang
        uses: KyleMayes/install-llvm-action@v2
        with:
          version: '17'

     - uses: actions/setup-python@v5
       with:
         python-version: '3.12'
         cache: 'pip'

      # Install the python package and run review
      - name: Non-docker clang-tidy-check
        run: |
          clang-tidy --version
          pip install ${{github.workspace}}/clang-tidy-review/post/clang_tidy_review
          review --token=${{ secrets.GITHUB_TOKEN }} --repo=${{ github.event.pull_request.head.repo.full_name }} --pr=${{ github.event.pull_request.number }} --clang_tidy_binary=clang-tidy --build_dir=cmake-build-presets/ninja-multi-vcpkg --config_file=.clang-tidy --split_workflow=True
        env:
          USER: ${{ github.event.pull_request.user.login }}

     # Upload the review for the next job
      - uses: actions/upload-artifact@v3
        with:
          name: ClangTidyReviewOutputs-${{matrix.os}}
          path: |
            clang-tidy-review-metadata.json
            clang-tidy-review-output.json
            clang_tidy_review.yaml

  # Second part of the split job
  clang-tidy-comments:
    runs-on: ubuntu-latest
    needs: clang-tidy-review
    steps:
      - name: Checkout clang-tidy-review
        uses: actions/checkout@v4
        with:
          repository: ZedThree/clang-tidy-review
          ref: master
          path: clang-tidy-review

     # Download all the reviews
      - uses: actions/download-artifact@v3
        with:
          name: ClangTidyReviewOutputs-ubuntu-latest
          path: ubuntu-latest
      - uses: actions/download-artifact@v3
        with:
          name: ClangTidyReviewOutputs-macos-latest
          path: macos-latest
      - uses: actions/download-artifact@v3
        with:
          name: ClangTidyReviewOutputs-windows-latest
          path: windows-latest

     - uses: actions/setup-python@v5
       with:
         python-version: '3.12'
         cache: 'pip'

      # Again install and run the python package to post the review
      - name: Non-docker clang-tidy-check
        run: |
          clang-tidy --version
          pip install ${{github.workspace}}/clang-tidy-review/post/clang_tidy_review
          cp ubuntu-latest/clang-tidy-review-metadata.json .
          post --token=${{ secrets.GITHUB_TOKEN }} --repo=${{ github.event.pull_request.head.repo.full_name }} ubuntu-latest/clang-tidy-review-output.json macos-latest/clang-tidy-review-output.json windows-latest/clang-tidy-review-output.json
ZehMatt commented 4 months ago

I initially used also clang-tidy-review but ended up creating my own that works almost the same as this one for the reasons you just pointed out, you can check it out here https://github.com/ZehMatt/clang-tidy-annotations

I hope this doesn't come over as self advertisement but I figured this is exactly what this issue is about.

bwrsandman commented 4 months ago

Thanks for sharing! Correct me if I'm wrong but annotations don't give you the option to add fixes to a batch in the PR's web interface, is that right?

ZedThree commented 4 months ago

This is really nice, thanks @bwrsandman! I need to investigate properly, but I think it's possible to have composite actions (e.g. https://docs.github.com/en/actions/creating-actions/creating-a-composite-action). That looks like we could then use your workflow here and export it as a single action for users?