From be80a14c5d2e20fbc3397dd7a9c836fb3934f55c Mon Sep 17 00:00:00 2001 From: Shankar Singh C <83439957+ShankarSinghC@users.noreply.github.com> Date: Fri, 13 Oct 2023 17:18:58 +0530 Subject: [PATCH] ci: auto format code when formatting step fails and update Cargo.lock if changed (#2567) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .github/workflows/CI-pr.yml | 53 ++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/.github/workflows/CI-pr.yml b/.github/workflows/CI-pr.yml index 96d5898ec7..c79ffa6370 100644 --- a/.github/workflows/CI-pr.yml +++ b/.github/workflows/CI-pr.yml @@ -41,19 +41,43 @@ jobs: name: Check formatting runs-on: ubuntu-latest steps: - - name: Checkout repository + - name: Checkout repository with token + if: ${{ github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name }} uses: actions/checkout@v3 - + with: + fetch-depth: 0 + ref: ${{ github.event.pull_request.head.ref }} + token: ${{ secrets.AUTO_FILE_UPDATE_PAT }} + + - name: Checkout repository for fork + if: ${{ github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name }} + uses: actions/checkout@v3 + - name: Install Rust uses: dtolnay/rust-toolchain@master with: toolchain: nightly components: rustfmt - - name: Check formatting + - name: Check formatting for forked pull requests + if: ${{ github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name }} shell: bash run: cargo +nightly fmt --all --check + - name: Run formatter + if: ${{ github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name }} + shell: bash + run: | + cargo +nightly fmt --all + if ! git diff --exit-code --quiet -- crates; then + echo "::notice::Formatting check failed" + git config --local user.name 'github-actions[bot]' + git config --local user.email '41898282+github-actions[bot]@users.noreply.github.com' + git add crates + git commit --message 'chore: run formatter' + git push + fi + check-msrv: name: Check compilation on MSRV toolchain runs-on: ${{ matrix.os }} @@ -275,9 +299,18 @@ jobs: # - windows-latest steps: - - name: Checkout repository + - name: Checkout repository for fork + if: ${{ (github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) }} uses: actions/checkout@v3 + - name: Checkout repository with token + if: ${{ (github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name) }} + uses: actions/checkout@v3 + with: + fetch-depth: 0 + ref: ${{ github.event.pull_request.head.ref }} + token: ${{ secrets.AUTO_FILE_UPDATE_PAT }} + - name: "Fetch base branch" shell: bash run: git fetch origin $GITHUB_BASE_REF --depth 1 @@ -322,6 +355,18 @@ jobs: shell: bash run: cargo clippy --all-features --all-targets + - name: Check Cargo.lock changed + if: ${{ (github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name) }} + shell: bash + run: | + if ! git diff --quiet --exit-code -- Cargo.lock ; then + git config --local user.name 'github-actions[bot]' + git config --local user.email '41898282+github-actions[bot]@users.noreply.github.com' + git add Cargo.lock + git commit --message 'chore: update Cargo.lock' + git push + fi + - name: Check files changed shell: bash run: |