Skip to content

Commit cc6f704

Browse files
committed
feat: add gas snapshot diff report workflow
1 parent bef092e commit cc6f704

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
run: |
46+
npm run snapshot:diff | sed 's/\x1b\[[0-9;]*m//g' | grep -v "^>" > gas_diff.txt
47+
cat gas_diff.txt
48+
49+
- name: Comment Gas Diff on PR
50+
uses: actions/github-script@v7
51+
with:
52+
github-token: ${{ secrets.GITHUB_TOKEN }}
53+
script: |
54+
const fs = require('fs');
55+
const diffOutput = fs.readFileSync('gas_diff.txt', 'utf8');
56+
57+
if (diffOutput.trim() === '') {
58+
console.log("No gas changes detected.");
59+
return;
60+
}
61+
62+
const body = `### ⛽ Gas Usage Changes
63+
64+
Comparison against \`${{ github.base_ref }}\` branch:
65+
66+
<details>
67+
<summary>Click to view gas diff</summary>
68+
69+
\`\`\`diff
70+
${diffOutput}
71+
\`\`\`
72+
73+
</details>
74+
75+
*Calculated by Foundry Gas Snapshot Action*`;
76+
77+
const { owner, repo, number } = context.issue;
78+
const comments = await github.rest.issues.listComments({
79+
owner,
80+
repo,
81+
issue_number: number,
82+
});
83+
84+
const botComment = comments.data.find(c => c.body.includes('### ⛽ Gas Usage Changes'));
85+
86+
if (botComment) {
87+
await github.rest.issues.updateComment({
88+
owner,
89+
repo,
90+
comment_id: botComment.id,
91+
body: body
92+
});
93+
} else {
94+
await github.rest.issues.createComment({
95+
owner,
96+
repo,
97+
issue_number: number,
98+
body: body
99+
});
100+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Update Gas Snapshot on Push
2+
3+
on:
4+
push:
5+
paths:
6+
- 'src/**'
7+
- 'test/**'
8+
- 'foundry.toml'
9+
10+
permissions:
11+
contents: write
12+
13+
jobs:
14+
update-snapshot:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout repository
18+
uses: actions/checkout@v4
19+
with:
20+
ref: ${{ github.head_ref }}
21+
22+
- name: Setup Node.js
23+
uses: actions/setup-node@v4
24+
with:
25+
node-version: '25'
26+
27+
- name: Install dependencies
28+
run: npm ci
29+
30+
- name: Generate Snapshot
31+
run: npm run snapshot
32+
33+
- name: Commit and Push changes
34+
uses: stefanzweifel/git-auto-commit-action@v5
35+
with:
36+
commit_message: "chore(gas): update .gas-snapshot [skip ci]"
37+
file_pattern: .gas-snapshot

0 commit comments

Comments
 (0)