Skip to content

Commit c0a5d09

Browse files
committed
🐞 Fix issue when branchName is same
1 parent 3ea6cb9 commit c0a5d09

File tree

7 files changed

+94
-64
lines changed

7 files changed

+94
-64
lines changed

.gitignore

100644100755
File mode changed.

LICENSE

100644100755
File mode changed.

README.md

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
run: forge install
5353

5454
- name: Generate gas diff
55-
uses: atarpara/foundry-snapshot-diff@v0.7
55+
uses: atarpara/foundry-snapshot-diff@v0.8
5656
with:
5757
# Optionally configure to run only for changes in specific files. For example:
5858
# token: ${{ secrets.GITHUB_TOKEN }}

action.yml

100644100755
File mode changed.

dist/index.js

100644100755
Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32345,9 +32345,9 @@ const fs = __nccwpck_require__(7147);
3234532345

3234632346
async function run() {
3234732347
try {
32348-
const freshSnapshot = core.getInput("fresh-shapshots")==='true';
32349-
const includeFuzzTests = core.getInput('include-fuzz-tests')==='true';
32350-
const includeNewContracts = core.getInput('include-new-contracts')==='true';
32348+
const freshSnapshot = core.getInput("fresh-shapshots") === 'true';
32349+
const includeFuzzTests = core.getInput('include-fuzz-tests') === 'true';
32350+
const includeNewContracts = core.getInput('include-new-contracts') === 'true';
3235132351
const token = process.env.GITHUB_TOKEN || core.getInput("token");
3235232352
const octokit = getOctokit(token);
3235332353
const repo = context.repo.repo;
@@ -32377,7 +32377,7 @@ async function run() {
3237732377
if (prSnapshot === null) {
3237832378
throw new Error(`prSnapshot is null`);
3237932379
}
32380-
32380+
3238132381
fs.writeFileSync('.gas-snapshot.pr', prSnapshot);
3238232382
core.endGroup()
3238332383

@@ -32425,14 +32425,29 @@ async function getGitFileContent(octokit, owner, repo, ref, filePath) {
3242532425
}
3242632426

3242732427
async function generateGasSnapshot(repoFullName, branchName, fileName) {
32428-
await exec.exec('git', ['fetch', `https://github.com/${repoFullName}`, `${branchName}`]);
32429-
await exec.exec('git', ['checkout', '-b', branchName, `FETCH_HEAD`]);
32428+
const isFork = repoFullName !== `${context.repo.owner}/${context.repo.repo}`;
32429+
32430+
core.info(`Fetching branch: ${branchName} from repo: ${repoFullName}`);
32431+
32432+
// First, ensure we're on a clean branch
32433+
await exec.exec('git', ['checkout', '-B', `temp-${branchName}`]);
32434+
32435+
if (isFork) {
32436+
// If it's a fork, add a new remote and fetch from it
32437+
await exec.exec('git', ['remote', 'add', 'fork', `https://github.com/${repoFullName}.git`], { ignoreReturnCode: true });
32438+
await exec.exec('git', ['fetch', 'fork', branchName]);
32439+
await exec.exec('git', ['reset', '--hard', `fork/${branchName}`]);
32440+
} else {
32441+
// If it's the same repo, fetch as usual
32442+
await exec.exec('git', ['fetch', 'origin', branchName]);
32443+
await exec.exec('git', ['reset', '--hard', `origin/${branchName}`]);
32444+
}
3243032445

3243132446
const options = {
3243232447
ignoreReturnCode: true,
3243332448
silent: true
3243432449
};
32435-
await exec.exec('forge', ['snapshot', '--snap', fileName] , options);
32450+
await exec.exec('forge', ['snapshot', '--snap', fileName], options);
3243632451
}
3243732452

3243832453
async function getDiffFileContent() {
@@ -32453,7 +32468,7 @@ async function getDiffFileContent() {
3245332468

3245432469
function generateReport(diffSnapshot, genCommit, comCommit, includeFuzzTests, includeNewContracts) {
3245532470
if (!diffSnapshot || diffSnapshot.trim() === "") return ""; // Return if diffSnapshot is blank
32456-
32471+
3245732472
const mainTests = [];
3245832473
const prTests = [];
3245932474

@@ -32502,28 +32517,28 @@ function generateReport(diffSnapshot, genCommit, comCommit, includeFuzzTests, in
3250232517
]));
3250332518

3250432519

32505-
const testData = uniqueTests.map(testName => {
32506-
const [contractName, simpleTestName] = testName.split(':');
32507-
const mainTest = mainTests.find(t => t.testName === testName) || { gasValue: '-' };
32508-
const prTest = prTests.find(t => t.testName === testName) || { gasValue: '-' };
32509-
const diff = (mainTest.gasValue !== '-' && prTest.gasValue !== '-')
32510-
? (parseInt(prTest.gasValue) - parseInt(mainTest.gasValue))
32511-
: '-';
32512-
32513-
return {
32514-
contractName,
32515-
testName: simpleTestName,
32516-
mainGas: mainTest.gasValue,
32517-
prGas: prTest.gasValue,
32518-
diff
32519-
};
32520-
}).filter(entry =>
32521-
(includeNewContracts || entry.diff !== '-') && entry.diff !== 0); // Include new contracts or existing ones with valid gas values
32522-
32523-
// sort testData for correct rowSpan in report
32524-
testData.sort((a, b) => a.contractName.localeCompare(b.contractName));
32520+
const testData = uniqueTests.map(testName => {
32521+
const [contractName, simpleTestName] = testName.split(':');
32522+
const mainTest = mainTests.find(t => t.testName === testName) || { gasValue: '-' };
32523+
const prTest = prTests.find(t => t.testName === testName) || { gasValue: '-' };
32524+
const diff = (mainTest.gasValue !== '-' && prTest.gasValue !== '-')
32525+
? (parseInt(prTest.gasValue) - parseInt(mainTest.gasValue))
32526+
: '-';
32527+
32528+
return {
32529+
contractName,
32530+
testName: simpleTestName,
32531+
mainGas: mainTest.gasValue,
32532+
prGas: prTest.gasValue,
32533+
diff
32534+
};
32535+
}).filter(entry =>
32536+
(includeNewContracts || entry.diff !== '-') && entry.diff !== 0); // Include new contracts or existing ones with valid gas values
3252532537

32526-
let report = `
32538+
// sort testData for correct rowSpan in report
32539+
testData.sort((a, b) => a.contractName.localeCompare(b.contractName));
32540+
32541+
let report = `
3252732542
### Gas Snapshot Comparison Report
3252832543

3252932544
> Generated at commit : ${genCommit}, Compared to commit : ${comCommit}
@@ -32536,15 +32551,15 @@ function generateReport(diffSnapshot, genCommit, comCommit, includeFuzzTests, in
3253632551
<th>PR Gas</th>
3253732552
<th>Diff</th>
3253832553
</tr>`;
32539-
32554+
3254032555
let lastContractName = '';
3254132556

3254232557
// Process each test entry to generate report rows
3254332558
testData.forEach(entry => {
3254432559
if (entry.contractName !== lastContractName) {
3254532560
// Calculate rowSpan for the current contract
3254632561
const rowSpan = testData.filter(t => t.contractName === entry.contractName).length;
32547-
32562+
3254832563
report += `
3254932564
<tr>
3255032565
<td rowspan="${rowSpan}">${entry.contractName}</td>`;

package.json

100644100755
File mode changed.

src/index.js

100644100755
Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ const fs = require('fs');
55

66
async function run() {
77
try {
8-
const freshSnapshot = core.getInput("fresh-shapshots")==='true';
9-
const includeFuzzTests = core.getInput('include-fuzz-tests')==='true';
10-
const includeNewContracts = core.getInput('include-new-contracts')==='true';
8+
const freshSnapshot = core.getInput("fresh-shapshots") === 'true';
9+
const includeFuzzTests = core.getInput('include-fuzz-tests') === 'true';
10+
const includeNewContracts = core.getInput('include-new-contracts') === 'true';
1111
const token = process.env.GITHUB_TOKEN || core.getInput("token");
1212
const octokit = getOctokit(token);
1313
const repo = context.repo.repo;
@@ -37,7 +37,7 @@ async function run() {
3737
if (prSnapshot === null) {
3838
throw new Error(`prSnapshot is null`);
3939
}
40-
40+
4141
fs.writeFileSync('.gas-snapshot.pr', prSnapshot);
4242
core.endGroup()
4343

@@ -85,14 +85,29 @@ async function getGitFileContent(octokit, owner, repo, ref, filePath) {
8585
}
8686

8787
async function generateGasSnapshot(repoFullName, branchName, fileName) {
88-
await exec.exec('git', ['fetch', `https://github.com/${repoFullName}`, `${branchName}`]);
89-
await exec.exec('git', ['checkout', '-b', branchName, `FETCH_HEAD`]);
88+
const isFork = repoFullName !== `${context.repo.owner}/${context.repo.repo}`;
89+
90+
core.info(`Fetching branch: ${branchName} from repo: ${repoFullName}`);
91+
92+
// First, ensure we're on a clean branch
93+
await exec.exec('git', ['checkout', '-B', `temp-${branchName}`]);
94+
95+
if (isFork) {
96+
// If it's a fork, add a new remote and fetch from it
97+
await exec.exec('git', ['remote', 'add', 'fork', `https://github.com/${repoFullName}.git`], { ignoreReturnCode: true });
98+
await exec.exec('git', ['fetch', 'fork', branchName]);
99+
await exec.exec('git', ['reset', '--hard', `fork/${branchName}`]);
100+
} else {
101+
// If it's the same repo, fetch as usual
102+
await exec.exec('git', ['fetch', 'origin', branchName]);
103+
await exec.exec('git', ['reset', '--hard', `origin/${branchName}`]);
104+
}
90105

91106
const options = {
92107
ignoreReturnCode: true,
93108
silent: true
94109
};
95-
await exec.exec('forge', ['snapshot', '--snap', fileName] , options);
110+
await exec.exec('forge', ['snapshot', '--snap', fileName], options);
96111
}
97112

98113
async function getDiffFileContent() {
@@ -113,7 +128,7 @@ async function getDiffFileContent() {
113128

114129
function generateReport(diffSnapshot, genCommit, comCommit, includeFuzzTests, includeNewContracts) {
115130
if (!diffSnapshot || diffSnapshot.trim() === "") return ""; // Return if diffSnapshot is blank
116-
131+
117132
const mainTests = [];
118133
const prTests = [];
119134

@@ -162,28 +177,28 @@ function generateReport(diffSnapshot, genCommit, comCommit, includeFuzzTests, in
162177
]));
163178

164179

165-
const testData = uniqueTests.map(testName => {
166-
const [contractName, simpleTestName] = testName.split(':');
167-
const mainTest = mainTests.find(t => t.testName === testName) || { gasValue: '-' };
168-
const prTest = prTests.find(t => t.testName === testName) || { gasValue: '-' };
169-
const diff = (mainTest.gasValue !== '-' && prTest.gasValue !== '-')
170-
? (parseInt(prTest.gasValue) - parseInt(mainTest.gasValue))
171-
: '-';
172-
173-
return {
174-
contractName,
175-
testName: simpleTestName,
176-
mainGas: mainTest.gasValue,
177-
prGas: prTest.gasValue,
178-
diff
179-
};
180-
}).filter(entry =>
181-
(includeNewContracts || entry.diff !== '-') && entry.diff !== 0); // Include new contracts or existing ones with valid gas values
182-
183-
// sort testData for correct rowSpan in report
184-
testData.sort((a, b) => a.contractName.localeCompare(b.contractName));
185-
186-
let report = `
180+
const testData = uniqueTests.map(testName => {
181+
const [contractName, simpleTestName] = testName.split(':');
182+
const mainTest = mainTests.find(t => t.testName === testName) || { gasValue: '-' };
183+
const prTest = prTests.find(t => t.testName === testName) || { gasValue: '-' };
184+
const diff = (mainTest.gasValue !== '-' && prTest.gasValue !== '-')
185+
? (parseInt(prTest.gasValue) - parseInt(mainTest.gasValue))
186+
: '-';
187+
188+
return {
189+
contractName,
190+
testName: simpleTestName,
191+
mainGas: mainTest.gasValue,
192+
prGas: prTest.gasValue,
193+
diff
194+
};
195+
}).filter(entry =>
196+
(includeNewContracts || entry.diff !== '-') && entry.diff !== 0); // Include new contracts or existing ones with valid gas values
197+
198+
// sort testData for correct rowSpan in report
199+
testData.sort((a, b) => a.contractName.localeCompare(b.contractName));
200+
201+
let report = `
187202
### Gas Snapshot Comparison Report
188203
189204
> Generated at commit : ${genCommit}, Compared to commit : ${comCommit}
@@ -196,15 +211,15 @@ function generateReport(diffSnapshot, genCommit, comCommit, includeFuzzTests, in
196211
<th>PR Gas</th>
197212
<th>Diff</th>
198213
</tr>`;
199-
214+
200215
let lastContractName = '';
201216

202217
// Process each test entry to generate report rows
203218
testData.forEach(entry => {
204219
if (entry.contractName !== lastContractName) {
205220
// Calculate rowSpan for the current contract
206221
const rowSpan = testData.filter(t => t.contractName === entry.contractName).length;
207-
222+
208223
report += `
209224
<tr>
210225
<td rowspan="${rowSpan}">${entry.contractName}</td>`;

0 commit comments

Comments
 (0)