pre-commit-cron #11
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| name: pre-commit-cron | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| repositories: | |
| description: 'Override the target repositories, use a comma separated list. Leave as All to run on all repositories.' | |
| default: 'All' | |
| type: string | |
| inactive_days: | |
| description: 'Number of days with no PR activity' | |
| default: 7 | |
| type: number | |
| first_run: | |
| description: 'Whether to run in first run mode' | |
| default: false | |
| type: boolean | |
| # schedule: | |
| # - cron: '34 4 * * 0,3,5' | |
| permissions: | |
| id-token: write | |
| contents: read | |
| env: | |
| ACTIVITY_DAYS_AGO: 7 | |
| jobs: | |
| generate-matrix: | |
| name: Generate Matrix | |
| runs-on: ubuntu-latest | |
| environment: avm | |
| outputs: | |
| matrix: ${{ steps.matrix.outputs.matrix }} | |
| matrixParallel: ${{ steps.matrix.outputs.matrixParallel }} | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
| - name: Generate Matrix | |
| uses: ./.github/actions/avm-repos | |
| id: matrix | |
| with: | |
| repositories: ${{ inputs.repositories }} | |
| first_run: ${{ inputs.first_run }} | |
| clientId: ${{ secrets.AVM_APP_CLIENT_ID }} | |
| privateKey: ${{ secrets.AVM_APP_PRIVATE_KEY }} | |
| run-precommit: | |
| name: ${{ matrix.repoId }} (${{ matrix.repoUrl }}) | |
| runs-on: ubuntu-latest | |
| environment: avm | |
| needs: generate-matrix | |
| strategy: | |
| fail-fast: false | |
| max-parallel: ${{ fromJson(needs.generate-matrix.outputs.matrixParallel) }} | |
| matrix: | |
| include: ${{ fromJson(needs.generate-matrix.outputs.matrix) }} | |
| steps: | |
| - name: Create GitHub App Token | |
| id: app-token | |
| uses: actions/create-github-app-token@5d869da34e18e7287c1daad50e0b8ea0f506ce69 # v1.11.0 | |
| with: | |
| app-id: ${{ secrets.AVM_APP_CLIENT_ID }} | |
| private-key: ${{ secrets.AVM_APP_PRIVATE_KEY }} | |
| owner: ${{ github.repository_owner }} | |
| - name: Check for recent PR activity | |
| id: check | |
| run: | | |
| # Check if there are any open PRs | |
| PR_COUNT=$(gh pr list --repo ${{ matrix.repoFullName }} --base main --json number | jq 'length') | |
| if [[ "$PR_COUNT" -eq 0 ]]; then | |
| echo "No open PRs found, proceeding with pre-commit run." | |
| echo "continue=true" >> $GITHUB_OUTPUT | |
| exit 0 | |
| fi | |
| INACTIVE_DAYS=${{ inputs.inactive_days != '' && inputs.inactive_days || env.ACTIVITY_DAYS_AGO }} | |
| echo "Found $PR_COUNT open PR(s), checking for recent activity (checking last $INACTIVE_DAYS days)..." | |
| # Read PR numbers into an array | |
| readarray -t NOS < <(gh pr list --repo ${{ matrix.repoFullName }} --base main --json number | jq -r '.[].number') | |
| for N in "${NOS[@]}"; do | |
| LAST_COMMIT=$(gh pr view $N --json commits | jq -r '[.commits[].authoredDate] | sort | last') | |
| if [ -n "$LAST_COMMIT" ]; then | |
| LAST_COMMIT_EPOCH=$(date -d "$LAST_COMMIT" +%s) | |
| DAYS_AGO_EPOCH=$(date -d "$INACTIVE_DAYS days ago" +%s) | |
| if [[ "$LAST_COMMIT_EPOCH" -gt "$DAYS_AGO_EPOCH" ]]; then | |
| echo "Skipping repository ${{ matrix.repoId }} as PR #$N has activity within the last $INACTIVE_DAYS days (last commit: $LAST_COMMIT)." | |
| exit 0 | |
| fi | |
| fi | |
| done | |
| echo "No recent PR activity found, proceeding with pre-commit run." | |
| echo "continue=true" >> $GITHUB_OUTPUT | |
| env: | |
| GITHUB_TOKEN: ${{ steps.app-token.outputs.token }} | |
| GH_TOKEN: ${{ steps.app-token.outputs.token }} | |
| GH_REPO: ${{ matrix.repoFullName }} | |
| - name: Checkout repository | |
| if: steps.check.outputs.continue | |
| uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2.7.0 | |
| with: | |
| repository: ${{ matrix.repoFullName }} | |
| - name: Run pre-commit | |
| if: steps.check.outputs.continue | |
| run: ./avm pre-commit | |
| shell: bash | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Commit and push changes | |
| if: steps.check.outputs.continue | |
| id: changes | |
| run: | | |
| # Check if there are ANY changes (including untracked files) | |
| if [ -z "$(git status --porcelain)" ]; then | |
| echo "No changes detected" | |
| exit 0 | |
| fi | |
| git config --global credential.helper '!f() { echo "username=token"; echo "password=$GITHUB_TOKEN"; }; f' | |
| git config --global user.name "${{ vars.AVM_APP_GIT_USER_NAME }}" | |
| git config --global user.email "${{ vars.AVM_APP_GIT_USER_EMAIL }}" | |
| git add . | |
| git commit -m "chore: pre-commit updates" | |
| git push | |
| shell: bash | |
| env: | |
| GITHUB_TOKEN: ${{ steps.app-token.outputs.token }} |