|
| 1 | +name: Pre-commit Bot |
| 2 | + |
| 3 | +run-name: Pre-commit bot for PR #${{ github.event.issue.number }} |
| 4 | + |
| 5 | +on: |
| 6 | + issue_comment: |
| 7 | + types: [created] |
| 8 | + |
| 9 | +jobs: |
| 10 | + pre-commit: |
| 11 | + # Only run on pull request comments |
| 12 | + if: github.event.issue.pull_request && contains(github.event.comment.body, '@github-actions run precommit') |
| 13 | + runs-on: ubuntu-latest |
| 14 | + permissions: |
| 15 | + contents: write |
| 16 | + pull-requests: write |
| 17 | + |
| 18 | + steps: |
| 19 | + - name: Check comment author and get PR details |
| 20 | + id: check_author |
| 21 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 22 | + with: |
| 23 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 24 | + script: | |
| 25 | + // Get PR details |
| 26 | + const pr = await github.rest.pulls.get({ |
| 27 | + owner: context.repo.owner, |
| 28 | + repo: context.repo.repo, |
| 29 | + pull_number: context.issue.number |
| 30 | + }); |
| 31 | +
|
| 32 | + // Check if commenter has write access or is the PR author |
| 33 | + const commenter = context.payload.comment.user.login; |
| 34 | + const prAuthor = pr.data.user.login; |
| 35 | +
|
| 36 | + let hasPermission = false; |
| 37 | +
|
| 38 | + // Check if commenter is PR author |
| 39 | + if (commenter === prAuthor) { |
| 40 | + hasPermission = true; |
| 41 | + console.log(`Comment author ${commenter} is the PR author`); |
| 42 | + } else { |
| 43 | + // Check if commenter has write/admin access |
| 44 | + try { |
| 45 | + const permission = await github.rest.repos.getCollaboratorPermissionLevel({ |
| 46 | + owner: context.repo.owner, |
| 47 | + repo: context.repo.repo, |
| 48 | + username: commenter |
| 49 | + }); |
| 50 | +
|
| 51 | + const level = permission.data.permission; |
| 52 | + hasPermission = ['write', 'admin', 'maintain'].includes(level); |
| 53 | + console.log(`Comment author ${commenter} has permission: ${level}`); |
| 54 | + } catch (error) { |
| 55 | + console.log(`Could not check permissions for ${commenter}: ${error.message}`); |
| 56 | + } |
| 57 | + } |
| 58 | +
|
| 59 | + if (!hasPermission) { |
| 60 | + await github.rest.issues.createComment({ |
| 61 | + owner: context.repo.owner, |
| 62 | + repo: context.repo.repo, |
| 63 | + issue_number: context.issue.number, |
| 64 | + body: `❌ @${commenter} You don't have permission to trigger pre-commit. Only PR authors or repository collaborators can run this command.` |
| 65 | + }); |
| 66 | + core.setFailed(`User ${commenter} does not have permission`); |
| 67 | + return; |
| 68 | + } |
| 69 | +
|
| 70 | + // Save PR info for later steps |
| 71 | + core.setOutput('pr_number', context.issue.number); |
| 72 | + core.setOutput('pr_head_ref', pr.data.head.ref); |
| 73 | + core.setOutput('pr_head_sha', pr.data.head.sha); |
| 74 | + core.setOutput('pr_head_repo', pr.data.head.repo.full_name); |
| 75 | + core.setOutput('pr_base_ref', pr.data.base.ref); |
| 76 | + core.setOutput('is_fork', pr.data.head.repo.full_name !== context.payload.repository.full_name); |
| 77 | + core.setOutput('authorized', 'true'); |
| 78 | +
|
| 79 | + - name: React to comment |
| 80 | + if: steps.check_author.outputs.authorized == 'true' |
| 81 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 82 | + with: |
| 83 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 84 | + script: | |
| 85 | + await github.rest.reactions.createForIssueComment({ |
| 86 | + owner: context.repo.owner, |
| 87 | + repo: context.repo.repo, |
| 88 | + comment_id: context.payload.comment.id, |
| 89 | + content: 'rocket' |
| 90 | + }); |
| 91 | +
|
| 92 | + - name: Comment starting |
| 93 | + if: steps.check_author.outputs.authorized == 'true' |
| 94 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 95 | + with: |
| 96 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 97 | + script: | |
| 98 | + await github.rest.issues.createComment({ |
| 99 | + owner: context.repo.owner, |
| 100 | + repo: context.repo.repo, |
| 101 | + issue_number: ${{ steps.check_author.outputs.pr_number }}, |
| 102 | + body: `⏳ Running pre-commit hooks on PR #${{ steps.check_author.outputs.pr_number }}...` |
| 103 | + }); |
| 104 | +
|
| 105 | + - name: Checkout PR branch (same-repo) |
| 106 | + if: steps.check_author.outputs.authorized == 'true' && steps.check_author.outputs.is_fork == 'false' |
| 107 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 108 | + with: |
| 109 | + ref: ${{ steps.check_author.outputs.pr_head_ref }} |
| 110 | + fetch-depth: 0 |
| 111 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 112 | + |
| 113 | + - name: Checkout PR branch (fork) |
| 114 | + if: steps.check_author.outputs.authorized == 'true' && steps.check_author.outputs.is_fork == 'true' |
| 115 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 116 | + with: |
| 117 | + repository: ${{ steps.check_author.outputs.pr_head_repo }} |
| 118 | + ref: ${{ steps.check_author.outputs.pr_head_ref }} |
| 119 | + fetch-depth: 0 |
| 120 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 121 | + |
| 122 | + - name: Verify checkout |
| 123 | + if: steps.check_author.outputs.authorized == 'true' |
| 124 | + run: | |
| 125 | + echo "Current SHA: $(git rev-parse HEAD)" |
| 126 | + echo "Expected SHA: ${{ steps.check_author.outputs.pr_head_sha }}" |
| 127 | + if [[ "$(git rev-parse HEAD)" != "${{ steps.check_author.outputs.pr_head_sha }}" ]]; then |
| 128 | + echo "::error::Checked out SHA does not match expected SHA" |
| 129 | + exit 1 |
| 130 | + fi |
| 131 | +
|
| 132 | + - name: Set up Python |
| 133 | + if: steps.check_author.outputs.authorized == 'true' |
| 134 | + uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0 |
| 135 | + with: |
| 136 | + python-version: '3.12' |
| 137 | + cache: pip |
| 138 | + cache-dependency-path: | |
| 139 | + **/requirements*.txt |
| 140 | + .pre-commit-config.yaml |
| 141 | +
|
| 142 | + - name: Set up Node.js |
| 143 | + if: steps.check_author.outputs.authorized == 'true' |
| 144 | + uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0 |
| 145 | + with: |
| 146 | + node-version: '20' |
| 147 | + cache: 'npm' |
| 148 | + cache-dependency-path: 'llama_stack/ui/' |
| 149 | + |
| 150 | + - name: Install npm dependencies |
| 151 | + if: steps.check_author.outputs.authorized == 'true' |
| 152 | + run: npm ci |
| 153 | + working-directory: llama_stack/ui |
| 154 | + |
| 155 | + - name: Run pre-commit |
| 156 | + if: steps.check_author.outputs.authorized == 'true' |
| 157 | + id: precommit |
| 158 | + uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1 |
| 159 | + continue-on-error: true |
| 160 | + env: |
| 161 | + SKIP: no-commit-to-branch |
| 162 | + RUFF_OUTPUT_FORMAT: github |
| 163 | + |
| 164 | + - name: Check for changes |
| 165 | + if: steps.check_author.outputs.authorized == 'true' |
| 166 | + id: changes |
| 167 | + run: | |
| 168 | + if ! git diff --exit-code || [ -n "$(git ls-files --others --exclude-standard)" ]; then |
| 169 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 170 | + echo "Changes detected after pre-commit" |
| 171 | + else |
| 172 | + echo "has_changes=false" >> $GITHUB_OUTPUT |
| 173 | + echo "No changes after pre-commit" |
| 174 | + fi |
| 175 | +
|
| 176 | + - name: Commit and push changes |
| 177 | + if: steps.check_author.outputs.authorized == 'true' && steps.changes.outputs.has_changes == 'true' |
| 178 | + run: | |
| 179 | + git config --local user.email "github-actions[bot]@users.noreply.github.com" |
| 180 | + git config --local user.name "github-actions[bot]" |
| 181 | +
|
| 182 | + git add -A |
| 183 | + git commit -m "style: apply pre-commit fixes |
| 184 | +
|
| 185 | + 🤖 Applied by @github-actions bot via pre-commit workflow" |
| 186 | +
|
| 187 | + # Push changes |
| 188 | + git push origin HEAD:${{ steps.check_author.outputs.pr_head_ref }} |
| 189 | +
|
| 190 | + - name: Comment success with changes |
| 191 | + if: steps.check_author.outputs.authorized == 'true' && steps.changes.outputs.has_changes == 'true' |
| 192 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 193 | + with: |
| 194 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 195 | + script: | |
| 196 | + await github.rest.issues.createComment({ |
| 197 | + owner: context.repo.owner, |
| 198 | + repo: context.repo.repo, |
| 199 | + issue_number: ${{ steps.check_author.outputs.pr_number }}, |
| 200 | + body: `✅ Pre-commit hooks completed successfully!\n\n🔧 Changes have been committed and pushed to the PR branch.` |
| 201 | + }); |
| 202 | +
|
| 203 | + - name: Comment success without changes |
| 204 | + if: steps.check_author.outputs.authorized == 'true' && steps.changes.outputs.has_changes == 'false' && steps.precommit.outcome == 'success' |
| 205 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 206 | + with: |
| 207 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 208 | + script: | |
| 209 | + await github.rest.issues.createComment({ |
| 210 | + owner: context.repo.owner, |
| 211 | + repo: context.repo.repo, |
| 212 | + issue_number: ${{ steps.check_author.outputs.pr_number }}, |
| 213 | + body: `✅ Pre-commit hooks passed!\n\n✨ No changes needed - your code is already formatted correctly.` |
| 214 | + }); |
| 215 | +
|
| 216 | + - name: Comment failure |
| 217 | + if: failure() |
| 218 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 219 | + with: |
| 220 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 221 | + script: | |
| 222 | + await github.rest.issues.createComment({ |
| 223 | + owner: context.repo.owner, |
| 224 | + repo: context.repo.repo, |
| 225 | + issue_number: ${{ steps.check_author.outputs.pr_number }}, |
| 226 | + 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.` |
| 227 | + }); |
0 commit comments