|
| 1 | +name: Gas Snapshot Diff Report |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - 'src/**' |
| 7 | + - 'test/**' |
| 8 | + - 'foundry.toml' |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + pull-requests: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + gas-diff: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - name: Checkout repository |
| 19 | + uses: actions/checkout@v4 |
| 20 | + with: |
| 21 | + fetch-depth: 0 |
| 22 | + |
| 23 | + - name: Setup Node.js |
| 24 | + uses: actions/setup-node@v4 |
| 25 | + with: |
| 26 | + node-version: '25' |
| 27 | + |
| 28 | + - name: Install dependencies |
| 29 | + run: npm ci |
| 30 | + |
| 31 | + - name: Fetch .gas-snapshot from Base Branch |
| 32 | + run: | |
| 33 | + BASE_BRANCH="origin/${{ github.base_ref }}" |
| 34 | + echo "Fetching snapshot from $BASE_BRANCH..." |
| 35 | + |
| 36 | + if git show "$BASE_BRANCH:.gas-snapshot" > .gas-snapshot 2>/dev/null; then |
| 37 | + echo "Successfully fetched .gas-snapshot from base branch." |
| 38 | + else |
| 39 | + echo "⚠️ .gas-snapshot not found in base branch." |
| 40 | + exit 0 |
| 41 | + fi |
| 42 | +
|
| 43 | + - name: Run Snapshot Diff |
| 44 | + id: run_diff |
| 45 | + shell: bash |
| 46 | + run: | |
| 47 | + npm run snapshot:diff | \ |
| 48 | + sed 's/\x1b\[[0-9;]*m//g' | \ |
| 49 | + grep -v "^>" | \ |
| 50 | + grep -vE "^\[PASS\]|^Ran |^Suite result" | \ |
| 51 | + grep -v "gas: 0 (0.000%)" | \ |
| 52 | + grep -v "compilation skipped" | \ |
| 53 | + sed '/^$/d' | \ |
| 54 | + sed -E 's/\(gas: ([0-9])/(gas: +\1/g' > gas_diff.txt |
| 55 | + |
| 56 | + cat gas_diff.txt |
| 57 | +
|
| 58 | + - name: Comment Gas Diff on PR |
| 59 | + uses: actions/github-script@v7 |
| 60 | + with: |
| 61 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 62 | + script: | |
| 63 | + const fs = require('fs'); |
| 64 | + const diffOutput = fs.readFileSync('gas_diff.txt', 'utf8'); |
| 65 | +
|
| 66 | + if (diffOutput.trim() === '') { |
| 67 | + console.log("No gas changes detected."); |
| 68 | + return; |
| 69 | + } |
| 70 | +
|
| 71 | + const body = `### ⛽ Gas Usage Changes |
| 72 | + |
| 73 | + Comparison against \`${{ github.base_ref }}\` branch: |
| 74 | + |
| 75 | + <details> |
| 76 | + <summary>Click to view gas diff</summary> |
| 77 | + |
| 78 | + \`\`\`diff |
| 79 | + ${diffOutput} |
| 80 | + \`\`\` |
| 81 | + |
| 82 | + </details> |
| 83 | + |
| 84 | + *Calculated by Foundry Gas Snapshot Action*`; |
| 85 | +
|
| 86 | + const { owner, repo, number } = context.issue; |
| 87 | + const comments = await github.rest.issues.listComments({ |
| 88 | + owner, |
| 89 | + repo, |
| 90 | + issue_number: number, |
| 91 | + }); |
| 92 | +
|
| 93 | + const botComment = comments.data.find(c => c.body.includes('### ⛽ Gas Usage Changes')); |
| 94 | +
|
| 95 | + if (botComment) { |
| 96 | + await github.rest.issues.updateComment({ |
| 97 | + owner, |
| 98 | + repo, |
| 99 | + comment_id: botComment.id, |
| 100 | + body: body |
| 101 | + }); |
| 102 | + } else { |
| 103 | + await github.rest.issues.createComment({ |
| 104 | + owner, |
| 105 | + repo, |
| 106 | + issue_number: number, |
| 107 | + body: body |
| 108 | + }); |
| 109 | + } |
0 commit comments