Skip to content

Commit af89de5

Browse files
authored
Merge pull request #48 from datachainlab/add-gas-snapshot-workflow
feat: add gas snapshot diff report workflow
2 parents fab94ef + 2c50862 commit af89de5

File tree

3 files changed

+150
-1
lines changed

3 files changed

+150
-1
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Update Gas Snapshot on Merge
2+
3+
on:
4+
push:
5+
branches:
6+
- develop # ToDo: change develop to main when main branch is ready
7+
paths:
8+
- 'src/**'
9+
- 'test/**'
10+
- 'foundry.toml'
11+
12+
permissions:
13+
contents: write
14+
15+
jobs:
16+
update-snapshot:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v4
21+
with:
22+
ref: ${{ github.ref }}
23+
24+
- name: Setup Node.js
25+
uses: actions/setup-node@v4
26+
with:
27+
node-version: '25'
28+
29+
- name: Install dependencies
30+
run: npm ci
31+
32+
- name: Generate Snapshot
33+
run: npm run snapshot
34+
35+
- name: Commit and Push changes
36+
uses: stefanzweifel/git-auto-commit-action@v5
37+
with:
38+
commit_message: "chore(gas): update .gas-snapshot [skip ci]"
39+
file_pattern: .gas-snapshot
40+
branch: ${{ github.ref }}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"fmt:sol": "forge fmt",
1212
"coverage:sol": "forge coverage --report lcov --ir-minimum",
1313
"snapshot": "forge snapshot",
14-
"snapshot:diff": "forge snapshot --diff"
14+
"snapshot:diff": "forge snapshot --diff --diff-sort absolute-desc"
1515
},
1616
"dependencies": {
1717
"@hyperledger-labs/yui-ibc-solidity": "git+https://github.com/hyperledger-labs/yui-ibc-solidity.git#v0.3.40",

0 commit comments

Comments
 (0)