diff --git a/.github/workflows/pre-commit-comment.yaml b/.github/workflows/pre-commit-comment.yaml index 751c6c6..d2640d9 100644 --- a/.github/workflows/pre-commit-comment.yaml +++ b/.github/workflows/pre-commit-comment.yaml @@ -10,134 +10,92 @@ jobs: comment: name: Comment on PR runs-on: ubuntu-latest - # Only run if the pre-commit workflow completed (not necessarily succeeded) if: github.event.workflow_run.event == 'pull_request' permissions: pull-requests: write steps: - - name: Download artifact - id: download - uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v4 - continue-on-error: true - with: - name: pre-commit-diff - github-token: ${{ secrets.GITHUB_TOKEN }} - run-id: ${{ github.event.workflow_run.id }} - - - name: Check artifact and extract PR number - id: extract - if: steps.download.outcome == 'success' - run: | - if [ -f pr-number.txt ]; then - echo "PR_NUMBER=$(cat pr-number.txt)" >> $GITHUB_OUTPUT - echo "HAS_DIFF=true" >> $GITHUB_OUTPUT - else - echo "HAS_DIFF=false" >> $GITHUB_OUTPUT - fi - - name: Get PR number from workflow run id: pr-number uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 with: script: | - // Try to get PR number from artifact first, otherwise from the workflow run - let prNumber = ${{ steps.extract.outputs.PR_NUMBER || 'null' }}; - - if (!prNumber) { - // Get PR number from the workflow run event - const headRepo = context.payload.workflow_run.head_repository.owner.login; - const headBranch = context.payload.workflow_run.head_branch; - - console.log(`Looking for PR with head: ${headRepo}:${headBranch}`); - - const { data: pullRequests } = await github.rest.pulls.list({ - owner: context.repo.owner, - repo: context.repo.repo, - state: 'open', - head: `${headRepo}:${headBranch}`, - }); - - if (pullRequests.length > 0) { - prNumber = pullRequests[0].number; - console.log(`Found PR #${prNumber} for branch ${headBranch}`); - } else { - console.log('No open PR found for this workflow run'); - console.log('This might be a push to a non-PR branch, skipping comment workflow'); - return; - } - } - - core.setOutput('number', prNumber); + // Get PR number from the workflow run event + const headRepo = context.payload.workflow_run.head_repository.owner.login; + const headBranch = context.payload.workflow_run.head_branch; + console.log(`Looking for PR with head: ${headRepo}:${headBranch}`); + + const { data: pullRequests } = await github.rest.pulls.list({ + owner: context.repo.owner, + repo: context.repo.repo, + state: 'open', + head: `${headRepo}:${headBranch}`, + }); + + if (pullRequests.length > 0) { + core.setOutput('number', pullRequests[0].number); + } - name: Delete previous pre-commit failure comments if: steps.pr-number.outputs.number uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 + env: + PR_NUMBER: ${{ steps.pr-number.outputs.number }} with: script: | - const prNumber = ${{ steps.pr-number.outputs.number }}; - + const prNumber = parseInt(process.env.PR_NUMBER, 10); + // Get all comments on the PR const { data: comments } = await github.rest.issues.listComments({ owner: context.repo.owner, repo: context.repo.repo, issue_number: prNumber, }); - + console.log(`Found ${comments.length} total comments on PR #${prNumber}`); - - // Filter comments created by github-actions bot that contain our marker + const botComments = comments.filter(comment => { - const isBot = comment.user.login === 'github-actions[bot]'; - const hasMarker = comment.body && comment.body.includes('⚠️ Pre-commit Hook Failures'); - if (isBot && hasMarker) { - console.log(`Found matching comment ${comment.id} to delete`); - } - return isBot && hasMarker; + return comment.user.login === 'github-actions[bot]' && + comment.body && + comment.body.includes('⚠️ Pre-commit Hook Failures'); }); - - console.log(`Found ${botComments.length} pre-commit failure comment(s) to delete`); - - // Delete each matching comment + for (const comment of botComments) { - console.log(`Deleting comment ${comment.id}`); await github.rest.issues.deleteComment({ owner: context.repo.owner, repo: context.repo.repo, comment_id: comment.id, }); } - - console.log(`Successfully deleted ${botComments.length} previous pre-commit failure comment(s).`); - name: Post comment - if: steps.extract.outputs.HAS_DIFF == 'true' + if: steps.pr-number.outputs.number && github.event.workflow_run.conclusion == 'failure' uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 + env: + PR_NUMBER: ${{ steps.pr-number.outputs.number }} with: script: | - const fs = require('fs'); - const prNumber = ${{ steps.pr-number.outputs.number }}; - const diff = fs.readFileSync('diff.txt', 'utf8'); - + const prNumber = parseInt(process.env.PR_NUMBER, 10); + const body = `## ⚠️ Pre-commit Hook Failures - -
- View diff - - \`\`\`diff - ${diff} - \`\`\` - -
- - Please apply the above diff in your PR branch (or run \`pre-commit run --all-files\` locally) and push the changes. - - For more information on pre-commit, see the [pre-commit documentation](https://pre-commit.com/#install).`; - + + The pre-commit hooks detected issues that need to be fixed. + + **To fix these issues:** + + 1. Install required dependencies: + - [pre-commit](https://pre-commit.com/#install) + - [helm-docs](https://github.com/norwoodj/helm-docs#installation) + + 2. Run pre-commit on all files: + \`\`\`bash + pre-commit run --all-files + \`\`\` + + 3. Commit and push the changes`; + await github.rest.issues.createComment({ issue_number: prNumber, owner: context.repo.owner, repo: context.repo.repo, body: body }); - - console.log('Successfully posted PR comment with diff.'); diff --git a/.github/workflows/pre-commit.yaml b/.github/workflows/pre-commit.yaml index 8ce3600..481ba13 100644 --- a/.github/workflows/pre-commit.yaml +++ b/.github/workflows/pre-commit.yaml @@ -32,33 +32,5 @@ jobs: - name: Run pre-commit uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # pin@v3.0.1 - continue-on-error: true # Don't fail immediately; we'll handle it below with: extra_args: --verbose --all-files --show-diff-on-failure - - - name: Check for changes after pre-commit - id: diff-checker - run: | - echo "CHANGED=$(if git diff --quiet; then echo "false"; else echo "true"; fi)" >> $GITHUB_OUTPUT - - - name: Save diff and PR metadata - if: ${{ steps.diff-checker.outputs.CHANGED == 'true' }} - run: | - mkdir -p ./pr-comment - git diff > ./pr-comment/diff.txt - echo "${{ github.event.number }}" > ./pr-comment/pr-number.txt - echo "Pre-commit hooks made changes. See diff artifact." - - - name: Upload diff artifact - if: ${{ steps.diff-checker.outputs.CHANGED == 'true' }} - uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v4 - with: - name: pre-commit-diff - path: pr-comment/ - retention-days: 1 - - - name: Fail if changes were made - if: ${{ steps.diff-checker.outputs.CHANGED == 'true' }} - run: | - echo "::error::Pre-commit hooks made changes. Please review the diff in the PR comment and apply the fixes." - exit 1