@@ -28,73 +28,102 @@ jobs:
2828 - name : Install dependencies
2929 run : npm ci
3030
31- - name : Fetch .gas-snapshot from Base Branch
31+ - name : Fetch Base Snapshot
3232 run : |
3333 BASE_BRANCH="origin/${{ github.base_ref }}"
34- echo "Fetching snapshot from $BASE_BRANCH..."
34+ git fetch origin ${{ github.base_ref }} --depth=1 || true
3535
36- if git show "$BASE_BRANCH:.gas-snapshot" > .gas-snapshot 2>/dev/null; then
37- echo "Successfully fetched .gas- snapshot from base branch ."
36+ if git show "$BASE_BRANCH:.gas-snapshot" > .gas-snapshot.base 2>/dev/null; then
37+ echo "✅ Base snapshot found ."
3838 else
39- echo "⚠️ .gas- snapshot not found in base branch ."
39+ echo "⚠️ Base snapshot not found."
4040 exit 0
4141 fi
4242
43- - name : Run Snapshot Diff
44- id : run_diff
45- run : |
46- npm run snapshot:diff | sed 's/\x1b\[[0-9;]*m//g' | grep -v "^>" > gas_diff.txt
47- cat gas_diff.txt
43+ - name : Generate Current Snapshot
44+ run : npm run snapshot
4845
49- - name : Comment Gas Diff on PR
46+ - name : Compare and Comment
5047 uses : actions/github-script@v7
5148 with :
5249 github-token : ${{ secrets.GITHUB_TOKEN }}
5350 script : |
5451 const fs = require('fs');
55- const diffOutput = fs.readFileSync('gas_diff.txt', 'utf8');
5652
57- if (diffOutput.trim() === '') {
58- console.log("No gas changes detected.");
59- return;
53+ function parseSnapshot(content) {
54+ const lines = content.split('\n');
55+ const data = {};
56+ const regex = /(.*)\s+\((\d+)\)/; // 例: "Test:method() (12345)"
57+
58+ for (const line of lines) {
59+ const match = line.match(regex);
60+ if (match) {
61+ data[match[1].trim()] = parseInt(match[2]);
62+ }
63+ }
64+ return data;
6065 }
6166
62- const body = `### ⛽ Gas Usage Changes
67+ let baseData = {};
68+ let currentData = {};
6369
64- Comparison against \`${{ github.base_ref }}\` branch:
65-
66- <details>
67- <summary>Click to view gas diff</summary>
70+ try { baseData = parseSnapshot(fs.readFileSync('.gas-snapshot.base', 'utf8')); } catch (e) {}
71+ try { currentData = parseSnapshot(fs.readFileSync('.gas-snapshot', 'utf8')); } catch (e) {}
72+
73+ let rows = [];
6874
69- \`\`\`diff
70- ${diffOutput}
71- \`\`\`
75+ for (const [testName, currentGas] of Object.entries(currentData)) {
76+ const baseGas = baseData[testName];
77+
78+ if (baseGas === undefined) {
79+ rows.push({ name: testName, base: '-', current: currentGas, diff: 0, type: 'NEW' });
80+ continue;
81+ }
82+
83+ if (baseGas !== currentGas) {
84+ const diff = currentGas - baseGas;
85+ rows.push({ name: testName, base: baseGas, current: currentGas, diff: diff, type: 'MOD' });
86+ }
87+ }
88+
89+ if (rows.length === 0) {
90+ console.log("No gas changes.");
91+ return;
92+ }
93+
94+ rows.sort((a, b) => Math.abs(b.diff) - Math.abs(a.diff));
95+
96+ let markdownTable = `### ⛽ Gas Usage Changes
7297
73- </details>
98+ Comparison against \`${{ github.base_ref }}\`:
7499
75- *Calculated by Foundry Gas Snapshot Action*`;
100+ | Test Name | Before | After | Diff | Status |
101+ | :--- | :---: | :---: | :---: | :---: |
102+ `;
103+
104+ for (const row of rows) {
105+ const diffSign = row.diff > 0 ? '+' : '';
106+ const diffStr = row.type === 'NEW' ? '-' : `${diffSign}${row.diff}`;
107+ const icon = row.diff > 0 ? '❌' : (row.diff < 0 ? '✅' : '🆕');
108+
109+ markdownTable += `| \`${row.name}\` | ${row.base} | ${row.current} | **${diffStr}** | ${icon} |\n`;
110+ }
111+
112+ markdownTable += `\n*Calculated by Foundry Gas Snapshot Action*`;
76113
77114 const { owner, repo, number } = context.issue;
78115 const comments = await github.rest.issues.listComments({
79- owner,
80- repo,
81- issue_number: number,
116+ owner, repo, issue_number: number,
82117 });
83118
84119 const botComment = comments.data.find(c => c.body.includes('### ⛽ Gas Usage Changes'));
85120
86121 if (botComment) {
87122 await github.rest.issues.updateComment({
88- owner,
89- repo,
90- comment_id: botComment.id,
91- body: body
123+ owner, repo, comment_id: botComment.id, body: markdownTable
92124 });
93125 } else {
94126 await github.rest.issues.createComment({
95- owner,
96- repo,
97- issue_number: number,
98- body: body
127+ owner, repo, issue_number: number, body: markdownTable
99128 });
100129 }
0 commit comments