|
| 1 | +name: PR Package.json Comment |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + paths: |
| 7 | + - 'package.json' |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + comment-on-package-json-changes: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - name: Checkout code |
| 18 | + uses: actions/checkout@v4 |
| 19 | + with: |
| 20 | + fetch-depth: 0 |
| 21 | + |
| 22 | + - name: Get package.json changes |
| 23 | + id: package-changes |
| 24 | + run: | |
| 25 | + # Get the base branch (usually main) |
| 26 | + BASE_BRANCH="${{ github.event.pull_request.base.ref }}" |
| 27 | + HEAD_BRANCH="${{ github.event.pull_request.head.sha }}" |
| 28 | +
|
| 29 | + # Get the diff for package.json |
| 30 | + git diff origin/${BASE_BRANCH}...${HEAD_BRANCH} -- package.json > package_diff.txt |
| 31 | +
|
| 32 | + # Check if there are actual changes |
| 33 | + if [ -s package_diff.txt ]; then |
| 34 | + echo "changes_detected=true" >> $GITHUB_OUTPUT |
| 35 | + echo "Package.json has been modified in this PR" |
| 36 | + else |
| 37 | + echo "changes_detected=false" >> $GITHUB_OUTPUT |
| 38 | + echo "No changes detected in package.json" |
| 39 | + fi |
| 40 | +
|
| 41 | + - name: Comment on PR |
| 42 | + if: steps.package-changes.outputs.changes_detected == 'true' |
| 43 | + uses: actions/github-script@v7 |
| 44 | + with: |
| 45 | + script: | |
| 46 | + const fs = require('fs'); |
| 47 | +
|
| 48 | + // Read the package.json diff |
| 49 | + let diffContent = ''; |
| 50 | + try { |
| 51 | + diffContent = fs.readFileSync('package_diff.txt', 'utf8'); |
| 52 | + } catch (error) { |
| 53 | + console.log('Could not read diff file:', error); |
| 54 | + diffContent = 'Unable to read package.json diff'; |
| 55 | + } |
| 56 | +
|
| 57 | + // Create the comment body |
| 58 | + const commentBody = `## 📦 Package.json Changes Detected |
| 59 | +
|
| 60 | + This PR modifies \`package.json\`. Please review the following changes carefully, and tick the following checklist boxes: |
| 61 | +
|
| 62 | + ### Checklist for Reviewers |
| 63 | + - [ ] Verify that new and/or updated dependencies are necessary and from trusted sources |
| 64 | + - [ ] Review any script changes for (security) implications |
| 65 | + - [ ] Verify whether a new version should be released after merging the PR |
| 66 | +
|
| 67 | + ### Package.json Diff |
| 68 | + \`\`\`diff |
| 69 | + ${diffContent} |
| 70 | + \`\`\` |
| 71 | +
|
| 72 | + --- |
| 73 | + _This comment was automatically generated by the PR Package.json Comment workflow._`; |
| 74 | +
|
| 75 | + // Check if we already commented on this PR |
| 76 | + const comments = await github.rest.issues.listComments({ |
| 77 | + owner: context.repo.owner, |
| 78 | + repo: context.repo.repo, |
| 79 | + issue_number: context.issue.number, |
| 80 | + }); |
| 81 | +
|
| 82 | + const existingComment = comments.data.find(comment => |
| 83 | + comment.body.includes('📦 Package.json Changes Detected') && |
| 84 | + comment.user.type === 'Bot' |
| 85 | + ); |
| 86 | +
|
| 87 | + if (existingComment) { |
| 88 | + // Update existing comment |
| 89 | + await github.rest.issues.updateComment({ |
| 90 | + owner: context.repo.owner, |
| 91 | + repo: context.repo.repo, |
| 92 | + comment_id: existingComment.id, |
| 93 | + body: commentBody |
| 94 | + }); |
| 95 | + console.log('Updated existing comment'); |
| 96 | + } else { |
| 97 | + // Create new comment |
| 98 | + await github.rest.issues.createComment({ |
| 99 | + owner: context.repo.owner, |
| 100 | + repo: context.repo.repo, |
| 101 | + issue_number: context.issue.number, |
| 102 | + body: commentBody |
| 103 | + }); |
| 104 | + console.log('Created new comment'); |
| 105 | + } |
0 commit comments