|
| 1 | +name: Pre-commit comment |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ["Pre-commit"] |
| 6 | + types: |
| 7 | + - completed |
| 8 | + |
| 9 | +jobs: |
| 10 | + comment: |
| 11 | + name: Comment on PR |
| 12 | + runs-on: ubuntu-latest |
| 13 | + if: github.event.workflow_run.event == 'pull_request' |
| 14 | + permissions: |
| 15 | + pull-requests: write |
| 16 | + |
| 17 | + steps: |
| 18 | + |
| 19 | + - name: Get the PR number from the workflow run |
| 20 | + id: pr-number |
| 21 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 |
| 22 | + with: |
| 23 | + script: | |
| 24 | + const prs = context.payload.workflow_run.pull_requests; |
| 25 | + if (prs.length > 0) { |
| 26 | + const num = prs[0].number; |
| 27 | + if (Number.isInteger(num)) { |
| 28 | + core.setOutput('number', num); |
| 29 | + } else { |
| 30 | + core.setFailed(`Invalid PR number detected: ${num}`); |
| 31 | + } |
| 32 | + } |
| 33 | +
|
| 34 | + - name: Delete previous pre-commit failure comments |
| 35 | + if: steps.pr-number.outputs.number |
| 36 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 |
| 37 | + env: |
| 38 | + PR_NUMBER: ${{ steps.pr-number.outputs.number }} |
| 39 | + with: |
| 40 | + script: | |
| 41 | + const prNumber = parseInt(process.env.PR_NUMBER, 10); |
| 42 | +
|
| 43 | + // Get all comments on the PR |
| 44 | + const { data: comments } = await github.rest.issues.listComments({ |
| 45 | + owner: context.repo.owner, |
| 46 | + repo: context.repo.repo, |
| 47 | + issue_number: prNumber, |
| 48 | + }); |
| 49 | +
|
| 50 | + console.log(`Found ${comments.length} total comments on PR #${prNumber}`); |
| 51 | +
|
| 52 | + const botComments = comments.filter(comment => { |
| 53 | + return comment.user.login === 'github-actions[bot]' && |
| 54 | + comment.body && |
| 55 | + comment.body.includes('⚠️ Pre-commit hook failures'); |
| 56 | + }); |
| 57 | +
|
| 58 | + for (const comment of botComments) { |
| 59 | + try { |
| 60 | + await github.rest.issues.deleteComment({ |
| 61 | + owner: context.repo.owner, |
| 62 | + repo: context.repo.repo, |
| 63 | + comment_id: comment.id, |
| 64 | + }); |
| 65 | + } catch (error) { |
| 66 | + console.warn(`Failed to delete comment ${comment.id}:`, error.message); |
| 67 | + } |
| 68 | + } |
| 69 | +
|
| 70 | + - name: Post comment |
| 71 | + if: steps.pr-number.outputs.number && github.event.workflow_run.conclusion == 'failure' |
| 72 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 |
| 73 | + env: |
| 74 | + PR_NUMBER: ${{ steps.pr-number.outputs.number }} |
| 75 | + with: |
| 76 | + script: | |
| 77 | + const prNumber = parseInt(process.env.PR_NUMBER, 10); |
| 78 | +
|
| 79 | + const body = ` |
| 80 | + ## ⚠️ Pre-commit hook failures |
| 81 | +
|
| 82 | + The pre-commit hooks detected issues that need to be fixed. |
| 83 | +
|
| 84 | + **To fix these issues:** |
| 85 | +
|
| 86 | + 1. Install the required dependencies: |
| 87 | + - [pre-commit](https://pre-commit.com/#install) |
| 88 | + - [helm-docs](https://github.com/norwoodj/helm-docs#installation) |
| 89 | +
|
| 90 | + 2. Run pre-commit on all files: |
| 91 | + \`\`\`bash |
| 92 | + pre-commit run --all-files |
| 93 | + \`\`\` |
| 94 | +
|
| 95 | + 3. Commit and push any resulting changes |
| 96 | + `.trim().replace(/^ {12}/gm, ''); |
| 97 | +
|
| 98 | + await github.rest.issues.createComment({ |
| 99 | + issue_number: prNumber, |
| 100 | + owner: context.repo.owner, |
| 101 | + repo: context.repo.repo, |
| 102 | + body: body |
| 103 | + }); |
0 commit comments