|
| 1 | +name: Pre-commit Bot - Execute |
| 2 | + |
| 3 | +run-name: Pre-commit bot execution for PR #${{ inputs.pr_number }} |
| 4 | + |
| 5 | +on: |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + pr_number: |
| 9 | + description: 'Pull request number' |
| 10 | + required: true |
| 11 | + type: string |
| 12 | + pr_head_ref: |
| 13 | + description: 'PR head ref' |
| 14 | + required: true |
| 15 | + type: string |
| 16 | + pr_head_sha: |
| 17 | + description: 'PR head SHA' |
| 18 | + required: true |
| 19 | + type: string |
| 20 | + pr_head_repo: |
| 21 | + description: 'PR head repository' |
| 22 | + required: true |
| 23 | + type: string |
| 24 | + pr_base_ref: |
| 25 | + description: 'PR base ref' |
| 26 | + required: true |
| 27 | + type: string |
| 28 | + |
| 29 | +jobs: |
| 30 | + pre-commit: |
| 31 | + runs-on: ubuntu-latest |
| 32 | + permissions: |
| 33 | + contents: write |
| 34 | + pull-requests: write |
| 35 | + |
| 36 | + steps: |
| 37 | + - name: Comment starting |
| 38 | + uses: actions/github-script@b72609b8d3f6598eef55e8f8010b7cba8b9ff9c5 # v7.0.1 |
| 39 | + with: |
| 40 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 41 | + script: | |
| 42 | + await github.rest.issues.createComment({ |
| 43 | + owner: context.repo.owner, |
| 44 | + repo: context.repo.repo, |
| 45 | + issue_number: ${{ inputs.pr_number }}, |
| 46 | + body: `⏳ Running pre-commit hooks on PR #${{ inputs.pr_number }}...` |
| 47 | + }); |
| 48 | +
|
| 49 | + - name: Determine checkout strategy |
| 50 | + id: checkout_strategy |
| 51 | + run: | |
| 52 | + # Check if this is a fork PR |
| 53 | + if [[ "${{ inputs.pr_head_repo }}" != "${{ github.repository }}" ]]; then |
| 54 | + echo "is_fork=true" >> $GITHUB_OUTPUT |
| 55 | + echo "This is a fork PR from ${{ inputs.pr_head_repo }}" |
| 56 | + else |
| 57 | + echo "is_fork=false" >> $GITHUB_OUTPUT |
| 58 | + echo "This is a same-repo PR" |
| 59 | + fi |
| 60 | +
|
| 61 | + - name: Checkout PR branch (same-repo) |
| 62 | + if: steps.checkout_strategy.outputs.is_fork == 'false' |
| 63 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 64 | + with: |
| 65 | + ref: ${{ inputs.pr_head_ref }} |
| 66 | + fetch-depth: 0 |
| 67 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 68 | + |
| 69 | + - name: Checkout PR branch (fork) |
| 70 | + if: steps.checkout_strategy.outputs.is_fork == 'true' |
| 71 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 72 | + with: |
| 73 | + repository: ${{ inputs.pr_head_repo }} |
| 74 | + ref: ${{ inputs.pr_head_ref }} |
| 75 | + fetch-depth: 0 |
| 76 | + # For forks, we need a token with write access to push |
| 77 | + # This will only work if the fork has granted workflow permissions |
| 78 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 79 | + |
| 80 | + - name: Verify checkout |
| 81 | + run: | |
| 82 | + echo "Current SHA: $(git rev-parse HEAD)" |
| 83 | + echo "Expected SHA: ${{ inputs.pr_head_sha }}" |
| 84 | + if [[ "$(git rev-parse HEAD)" != "${{ inputs.pr_head_sha }}" ]]; then |
| 85 | + echo "::error::Checked out SHA does not match expected SHA" |
| 86 | + exit 1 |
| 87 | + fi |
| 88 | +
|
| 89 | + - name: Set up Python |
| 90 | + uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0 |
| 91 | + with: |
| 92 | + python-version: '3.12' |
| 93 | + cache: pip |
| 94 | + cache-dependency-path: | |
| 95 | + **/requirements*.txt |
| 96 | + .pre-commit-config.yaml |
| 97 | +
|
| 98 | + - name: Set up Node.js |
| 99 | + uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0 |
| 100 | + with: |
| 101 | + node-version: '20' |
| 102 | + cache: 'npm' |
| 103 | + cache-dependency-path: 'llama_stack/ui/' |
| 104 | + |
| 105 | + - name: Install npm dependencies |
| 106 | + run: npm ci |
| 107 | + working-directory: llama_stack/ui |
| 108 | + |
| 109 | + - name: Run pre-commit |
| 110 | + id: precommit |
| 111 | + uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1 |
| 112 | + continue-on-error: true |
| 113 | + env: |
| 114 | + SKIP: no-commit-to-branch |
| 115 | + RUFF_OUTPUT_FORMAT: github |
| 116 | + |
| 117 | + - name: Check for changes |
| 118 | + id: changes |
| 119 | + run: | |
| 120 | + if ! git diff --exit-code || [ -n "$(git ls-files --others --exclude-standard)" ]; then |
| 121 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 122 | + echo "Changes detected after pre-commit" |
| 123 | + else |
| 124 | + echo "has_changes=false" >> $GITHUB_OUTPUT |
| 125 | + echo "No changes after pre-commit" |
| 126 | + fi |
| 127 | +
|
| 128 | + - name: Commit and push changes |
| 129 | + if: steps.changes.outputs.has_changes == 'true' |
| 130 | + run: | |
| 131 | + git config --local user.email "github-actions[bot]@users.noreply.github.com" |
| 132 | + git config --local user.name "github-actions[bot]" |
| 133 | +
|
| 134 | + git add -A |
| 135 | + git commit -m "style: apply pre-commit fixes |
| 136 | +
|
| 137 | + 🤖 Applied by @github-actions bot via pre-commit workflow" |
| 138 | +
|
| 139 | + # Push changes |
| 140 | + git push origin HEAD:${{ inputs.pr_head_ref }} |
| 141 | +
|
| 142 | + - name: Comment success with changes |
| 143 | + if: steps.changes.outputs.has_changes == 'true' |
| 144 | + uses: actions/github-script@b72609b8d3f6598eef55e8f8010b7cba8b9ff9c5 # v7.0.1 |
| 145 | + with: |
| 146 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 147 | + script: | |
| 148 | + await github.rest.issues.createComment({ |
| 149 | + owner: context.repo.owner, |
| 150 | + repo: context.repo.repo, |
| 151 | + issue_number: ${{ inputs.pr_number }}, |
| 152 | + body: `✅ Pre-commit hooks completed successfully!\n\n🔧 Changes have been committed and pushed to the PR branch.` |
| 153 | + }); |
| 154 | +
|
| 155 | + - name: Comment success without changes |
| 156 | + if: steps.changes.outputs.has_changes == 'false' && steps.precommit.outcome == 'success' |
| 157 | + uses: actions/github-script@b72609b8d3f6598eef55e8f8010b7cba8b9ff9c5 # v7.0.1 |
| 158 | + with: |
| 159 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 160 | + script: | |
| 161 | + await github.rest.issues.createComment({ |
| 162 | + owner: context.repo.owner, |
| 163 | + repo: context.repo.repo, |
| 164 | + issue_number: ${{ inputs.pr_number }}, |
| 165 | + body: `✅ Pre-commit hooks passed!\n\n✨ No changes needed - your code is already formatted correctly.` |
| 166 | + }); |
| 167 | +
|
| 168 | + - name: Comment failure |
| 169 | + if: failure() |
| 170 | + uses: actions/github-script@b72609b8d3f6598eef55e8f8010b7cba8b9ff9c5 # v7.0.1 |
| 171 | + with: |
| 172 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 173 | + script: | |
| 174 | + await github.rest.issues.createComment({ |
| 175 | + owner: context.repo.owner, |
| 176 | + repo: context.repo.repo, |
| 177 | + issue_number: ${{ inputs.pr_number }}, |
| 178 | + body: `❌ Pre-commit workflow failed!\n\nPlease check the [workflow logs](https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}) for details.` |
| 179 | + }); |
0 commit comments