PR Comment on Git LFS Check #519
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: PR Comment on Git LFS Check | |
| on: | |
| workflow_run: | |
| workflows: ["Test Git LFS"] | |
| types: [completed] | |
| permissions: | |
| pull-requests: write | |
| actions: read | |
| jobs: | |
| comment: | |
| runs-on: ubuntu-latest | |
| if: >- | |
| github.event.workflow_run.conclusion == 'failure' && | |
| (github.event.workflow_run.pull_requests[0] || startsWith(github.event.workflow_run.head_branch, 'pull-request/')) | |
| steps: | |
| - name: Download PR comment data | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: pr-comment-data | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| run-id: ${{ github.event.workflow_run.id }} | |
| - name: Post comment on PR | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| const pullRequests = context.payload.workflow_run.pull_requests; | |
| let prNumber; | |
| if (pullRequests && pullRequests.length > 0) { | |
| prNumber = pullRequests[0].number; | |
| } else { | |
| try { | |
| prNumber = parseInt(fs.readFileSync('pr_number', 'utf8').trim()); | |
| } catch (e) { | |
| console.log('No PR number found, skipping comment.'); | |
| return; | |
| } | |
| } | |
| if (!prNumber || isNaN(prNumber)) { | |
| console.log('No valid PR number found, skipping comment.'); | |
| return; | |
| } | |
| const errorType = fs.readFileSync('error_type', 'utf8').trim(); | |
| const errorFiles = fs.readFileSync('error_files', 'utf8').trim(); | |
| if (!errorType) { | |
| console.log('No error type found, skipping comment.'); | |
| return; | |
| } | |
| const runUrl = `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${{ github.event.workflow_run.id }}`; | |
| let commentBody; | |
| if (errorType === 'subdir_gitattributes') { | |
| const fileList = errorFiles.split('\n').map(f => f.trim()).filter(f => f).map(f => '- `' + f + '`').join('\n'); | |
| commentBody = ` | |
| ## ❌ Git LFS Check Failed | |
| 🔗 [View workflow run logs](${runUrl}) | |
| **Found \`.gitattributes\` file(s) in subdirectories.** Only the top-level \`.gitattributes\` should be used for LFS configuration. | |
| <details> | |
| <summary>Files to remove</summary> | |
| ${fileList} | |
| </details> | |
| ### How to fix: | |
| \`\`\`bash | |
| git rm <path/to/.gitattributes> | |
| git commit -m "Remove subdirectory .gitattributes" | |
| \`\`\` | |
| 📚 [Git LFS documentation](https://git-lfs.com/) | |
| `.replace(/^ {14}/gm, '').trim(); | |
| } else if (errorType === 'not_lfs_tracked') { | |
| commentBody = ` | |
| ## ❌ Git LFS Check Failed | |
| 🔗 [View workflow run logs](${runUrl}) | |
| **The following files should be tracked by Git LFS but are not:** | |
| <details> | |
| <summary>Files not tracked by LFS</summary> | |
| ${errorFiles} | |
| </details> | |
| ### How to fix: | |
| \`\`\`bash | |
| # For each file listed above: | |
| git rm --cached <file> | |
| git add <file> | |
| git commit -m "Track file with Git LFS" | |
| \`\`\` | |
| Make sure Git LFS is installed locally (\`git lfs install\`) before running these commands. | |
| 📚 [Git LFS documentation](https://git-lfs.com/) | |
| `.replace(/^ {14}/gm, '').trim(); | |
| } else { | |
| console.log(`Unknown error type: ${errorType}, skipping comment.`); | |
| return; | |
| } | |
| const { data: comments } = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber | |
| }); | |
| const botComment = comments.find(comment => | |
| comment.user.type === 'Bot' && | |
| comment.body.includes('Git LFS Check Failed') | |
| ); | |
| if (botComment) { | |
| // Update existing comment | |
| await github.rest.issues.updateComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: botComment.id, | |
| body: commentBody | |
| }); | |
| console.log(`Updated existing comment on PR #${prNumber}`); | |
| } else { | |
| // Create new comment | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| body: commentBody | |
| }); | |
| console.log(`Created comment on PR #${prNumber}`); | |
| } |