Skip to content

Auto-pull-request for cargo fixes #185

Auto-pull-request for cargo fixes

Auto-pull-request for cargo fixes #185

name: Auto-pull-request for cargo fixes
on:
# Run on push to main
push:
branches:
- main
paths:
- .github/workflows/auto-pr-cargo-fixes.yaml
- '*.rs'
- '**/*.rs'
# Run weekly
schedule:
- cron: '0 0 * * Sun'
# Needed so we can run it manually
workflow_dispatch:
permissions:
contents: read
defaults:
run:
shell: bash
env:
# So cargo doesn't complain about unstable features
RUSTC_BOOTSTRAP: 1
# Which branch to use to make the pull request
PR_BRANCH: auto/cargo_fix
# Whether or not to enable auto-merge
PR_AUTO_MERGE: false
jobs:
check-pull-request:
name: Check if the pull request exists
runs-on: ubuntu-latest
if: github.repository_owner == 'XaF'
permissions:
contents: read
pull-requests: read
outputs:
pull_request_state: ${{ env.PR_STATE }}
should_run: ${{ github.event_name != 'push' || env.PR_STATE == 'OPEN' }}
steps:
- name: Checkout commit
uses: actions/checkout@v4
- name: Check if pull request already exists
env:
GH_TOKEN: ${{ github.token }}
run: |
STATE=$(gh pr view "${{ env.PR_BRANCH }}" \
--repo "${{ github.repository }}" \
--json state --jq '.state' || \
echo "NOT FOUND")
echo "PR_STATE=${STATE}" | tee -a "$GITHUB_ENV"
run-cargo-fix:
name: Run different cargo fixes
runs-on: ubuntu-latest
needs:
- check-pull-request
if: needs.check-pull-request.outputs.should_run == 'true'
outputs:
any_update: ${{ env.UPDATES }}
steps:
- name: Checkout commit
uses: actions/checkout@v4
- name: Get rust stable
uses: dtolnay/rust-toolchain@stable
with:
components: clippy, rustfmt
- name: Set up cargo cache
uses: Swatinem/rust-cache@v2
with:
shared-key: cargo-fixes
- name: Run cargo fix
env:
__CARGO_FIX_YOLO: 1 # So that we offer all the fixes
run: |
cargo fix --all-features --color always --allow-dirty
- name: Run clippy --fix
env:
__CARGO_FIX_YOLO: 1 # So that we offer all the fixes
run: |
cargo clippy --fix --all-features --color always --allow-dirty
- name: Run rustfmt
run: |
cargo fmt --verbose
- name: Check if there is any update
run : |
git diff --name-only
git diff --quiet && \
UPDATES=false || \
UPDATES=true
echo "UPDATES=${UPDATES}" | tee -a "$GITHUB_ENV"
- name: Create archive of modified files
if: ${{ env.UPDATES == 'true' }}
run: |
git stash
git diff 'stash@{0}' -z --name-only | \
xargs -0 git archive 'stash@{0}' -o cargo-fix.zip --
- name: upload cargo-fix.zip artifact for use in PR
if: ${{ env.UPDATES == 'true' }}
uses: actions/upload-artifact@v4
with:
name: cargo-fix
path: cargo-fix.zip
retention-days: 1
create-or-update-pr:
name: Create or update pull-request
runs-on: ubuntu-latest
needs:
- check-pull-request
- run-cargo-fix
if: needs.check-pull-request.outputs.should_run == 'true' && needs.run-cargo-fix.outputs.any_update == 'true'
env:
PR_TITLE: "chore(lints): 💅 cargo fixes"
steps:
- name: Create application token
uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ secrets.OMNICLI_APP_ID }}
private-key: ${{ secrets.OMNICLI_PRIVATE_KEY }}
- name: Checkout commit
uses: actions/checkout@v4
with:
token: ${{ steps.app-token.outputs.token }}
- name: Download cargo-fix.zip from update job
uses: actions/download-artifact@v4
with:
name: cargo-fix
- name: Unzip cargo-fix.zip
run: |
unzip -o cargo-fix.zip -d .
rm cargo-fix.zip
- name: Craft commit message
run: |
echo "${{ env.PR_TITLE }}" | tee commit.txt
- name: Craft pull-request body
run: |
echo 'Fixing lint for rust files' | tee body.md
echo | tee -a body.md
echo "The following files have been updated:" | tee -a body.md
echo | tee -a body.md
git diff --name-only | while read file; do echo "- \`${file}\`"; done | tee -a body.md
- name: git commit
run: |
git config user.name github-actions
git config user.email [email protected]
git switch --force-create "${{ env.PR_BRANCH }}"
git commit --all --no-verify --file=commit.txt
- name: git push
run: |
git push --no-verify --force --set-upstream origin "${{ env.PR_BRANCH }}"
- name: Create or update pull-request
env:
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
PR_STATE: ${{ needs.check-pull-request.outputs.pull_request_state }}
run: |
if [[ "$PR_STATE" != "OPEN" ]]; then
gh pr create \
--title "${{ env.PR_TITLE }}" \
--body-file body.md \
--repo "${{ github.repository }}" \
--label rust
else
gh pr edit "${{ env.PR_BRANCH }}" \
--title "${{ env.PR_TITLE }}" \
--body-file body.md \
--repo "${{ github.repository }}" \
--add-label rust
fi
- name: Enable auto-merge
if: ${{ env.PR_AUTO_MERGE == 'true' }}
env:
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
run: |
NUM=$(gh pr view "${{ env.PR_BRANCH }}" \
--repo "${{ github.repository }}" \
--json number --jq '.number' || \
echo "")
if [[ -z "$NUM" ]]; then
echo "Failed to get PR number"
exit 1
fi
gh pr merge --squash --auto --body "" "$NUM"
close-pr:
name: Close pull-request
runs-on: ubuntu-latest
needs:
- check-pull-request
- run-cargo-fix
if: needs.check-pull-request.outputs.should_run == 'true' && needs.run-cargo-fix.outputs.any_update == 'false' && needs.check-pull-request.outputs.pull_request_state == 'OPEN'
env:
PR_CLOSING_COMMENT: "Superseded. No more lints to be fixed for now."
steps:
- name: Create application token
uses: actions/create-github-app-token@v1
id: app-token
env:
APP_ID: ${{ secrets.OMNICLI_APP_ID }}
PRIVATE_KEY: ${{ secrets.OMNICLI_PRIVATE_KEY }}
with:
app-id: ${{ env.APP_ID }}
private-key: ${{ env.PRIVATE_KEY }}
- name: Close pull-request
env:
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
run: |
gh pr close \
--title "${{ env.PR_TITLE }}" \
--comment "${{ env.PR_CLOSING_COMMENT }}" \
--delete-branch