|
| 1 | +name: Tree-shake test |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + workflow_dispatch: |
| 6 | + inputs: |
| 7 | + target_branch: |
| 8 | + description: 'Target branch to compare against' |
| 9 | + required: true |
| 10 | + default: 'main' |
| 11 | + |
| 12 | +jobs: |
| 13 | + treeshake-test: |
| 14 | + permissions: |
| 15 | + contents: read |
| 16 | + issues: write |
| 17 | + pull-requests: write |
| 18 | + runs-on: ubuntu-latest |
| 19 | + steps: |
| 20 | + - name: Install pnpm |
| 21 | + uses: pnpm/action-setup@v4 |
| 22 | + with: |
| 23 | + version: 10.27.0 |
| 24 | + run_install: false |
| 25 | + |
| 26 | + - name: Checkout PR branch |
| 27 | + uses: actions/checkout@v4 |
| 28 | + with: |
| 29 | + path: pr-branch |
| 30 | + |
| 31 | + - name: Checkout target branch |
| 32 | + uses: actions/checkout@v4 |
| 33 | + with: |
| 34 | + path: target-branch |
| 35 | + ref: ${{ github.event_name == 'workflow_dispatch' && inputs.target_branch || github.base_ref }} |
| 36 | + |
| 37 | + - name: Install dependencies (PR branch) |
| 38 | + working-directory: pr-branch |
| 39 | + run: pnpm install |
| 40 | + |
| 41 | + - name: Install dependencies (target branch) |
| 42 | + working-directory: target-branch |
| 43 | + run: pnpm install |
| 44 | + |
| 45 | + - name: Set up Node.js |
| 46 | + uses: actions/setup-node@v4 |
| 47 | + with: |
| 48 | + node-version: 22.x |
| 49 | + cache: 'pnpm' |
| 50 | + cache-dependency-path: | |
| 51 | + pr-branch/pnpm-lock.yaml |
| 52 | + target-branch/pnpm-lock.yaml |
| 53 | +
|
| 54 | + - name: Generate import tests on PR branch |
| 55 | + working-directory: pr-branch |
| 56 | + run: pnpm --filter treeshake-test generate-tests |
| 57 | + |
| 58 | + - name: Generate import tests on target branch |
| 59 | + working-directory: target-branch |
| 60 | + run: pnpm --filter treeshake-test generate-tests |
| 61 | + |
| 62 | + - name: Run tree-shake test on PR branch |
| 63 | + working-directory: pr-branch |
| 64 | + run: pnpm --filter treeshake-test test --tsdown ${{ github.event_name == 'workflow_dispatch' && '--webpack' || '' }} |
| 65 | + |
| 66 | + - name: Run tree-shake test on target branch |
| 67 | + working-directory: target-branch |
| 68 | + run: pnpm --filter treeshake-test test --tsdown ${{ github.event_name == 'workflow_dispatch' && '--webpack' || '' }} |
| 69 | + |
| 70 | + - name: Compare results |
| 71 | + run: | |
| 72 | + node pr-branch/apps/treeshake-test/compareResults.ts \ |
| 73 | + pr-branch/apps/treeshake-test/results.json \ |
| 74 | + target-branch/apps/treeshake-test/results.json \ |
| 75 | + > comparison.md |
| 76 | +
|
| 77 | + - name: Comment PR with results |
| 78 | + uses: actions/github-script@v7 |
| 79 | + with: |
| 80 | + script: | |
| 81 | + const fs = require('fs'); |
| 82 | + const comparison = fs.readFileSync('comparison.md', 'utf8'); |
| 83 | +
|
| 84 | + const botCommentIdentifier = '<!-- treeshake-test-bot-comment -->'; |
| 85 | +
|
| 86 | + async function findBotComment(issueNumber) { |
| 87 | + if (!issueNumber) return null; |
| 88 | + const comments = await github.rest.issues.listComments({ |
| 89 | + owner: context.repo.owner, |
| 90 | + repo: context.repo.repo, |
| 91 | + issue_number: issueNumber, |
| 92 | + }); |
| 93 | + return comments.data.find((comment) => |
| 94 | + comment.body.includes(botCommentIdentifier) |
| 95 | + ); |
| 96 | + } |
| 97 | +
|
| 98 | + async function createOrUpdateComment(issueNumber) { |
| 99 | + if (!issueNumber) { |
| 100 | + console.log('No issue number provided. Cannot post or update comment.'); |
| 101 | + return; |
| 102 | + } |
| 103 | +
|
| 104 | + const existingComment = await findBotComment(issueNumber); |
| 105 | + if (existingComment) { |
| 106 | + await github.rest.issues.updateComment({ |
| 107 | + ...context.repo, |
| 108 | + comment_id: existingComment.id, |
| 109 | + body: botCommentIdentifier + '\n' + comparison, |
| 110 | + }); |
| 111 | + } else { |
| 112 | + await github.rest.issues.createComment({ |
| 113 | + ...context.repo, |
| 114 | + issue_number: issueNumber, |
| 115 | + body: botCommentIdentifier + '\n' + comparison, |
| 116 | + }); |
| 117 | + } |
| 118 | + } |
| 119 | +
|
| 120 | + const issueNumber = context.issue.number; |
| 121 | + if (!issueNumber) { |
| 122 | + console.log('No issue number found in context. Skipping comment.'); |
| 123 | + } else { |
| 124 | + await createOrUpdateComment(issueNumber); |
| 125 | + } |
| 126 | + await core.summary |
| 127 | + .addRaw(comparison) |
| 128 | + .write(); |
0 commit comments