Skip to content

Commit

Permalink
docs: update readme, readd ref
Browse files Browse the repository at this point in the history
  • Loading branch information
boredland committed Jan 29, 2025
1 parent 64bb0aa commit fd355a8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
# Github Action: foundry-gas-diff

Foundry gas diff is a Github Action that compares gas usage between two Solidity contracts.
The action is useful to compare gas usage from pull request to pullrequest so you can stop gas regressions before they are merged.
Foundry gas diff is a Github Action that compares gas usage between two Solidity contracts, assuming snapshots in a directory called `snapshots`.
The action is useful to compare gas usage from pull request to pull request so you can stop gas regressions before they are merged.

In it's current version foundry-gas-diff only performs the diffing, and leaves the reporting to the user.

We might consider changing this in a future version.

## Usage

```yaml
- uses: bgd-labs/action-foundry-gas-diff@main
with:
# The path to the first contract to compare
ROOT_REPORT_PATH: 'path/to/gas/on/main'
# The path to the second contract to compare
REPORT_PATH: 'path/to/gas'
```
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ inputs:
default: "Gas report"
token:
description: "The GitHub token to use for the API requests."
required: true
required: false
default: ${{ github.token }}
files:
description: "The files that should be compared. A glob is accepted."
required: false
default: |
snapshots/*.json
base_ref:
description: "The ref to compare against. Uses the base ref of the pull request if not provided."
required: false

outputs:
report:
Expand Down
13 changes: 7 additions & 6 deletions dist/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -25966,25 +25966,26 @@ var formatDiffMd = (heading2, input) => {
var import_node_path = __toESM(require("path"));
var heading = (0, import_core.getInput)("heading");
var octokit = (0, import_github.getOctokit)((0, import_core.getInput)("token"));
var getBaseFile = async (path2) => {
var getBaseFile = async (path2, ref) => {
const { data } = await octokit.rest.repos.getContent({
repo: import_github.context.repo.repo,
owner: import_github.context.repo.owner,
path: path2,
ref: import_github.context.payload.pull_request?.base.ref
ref
}).catch((e) => {
return e.response;
});
(0, import_core.debug)(`Base file ${path2}: ${JSON.stringify(data)}`);
if (!data || !("content" in data)) {
return {};
}
return JSON.parse(atob(data.content));
};
var run = async () => {
(0, import_core.debug)("Starting run");
if (!("pull_request" in import_github.context.payload)) {
throw new Error("This action can only be run on pull_request events");
const base_ref = (0, import_core.getInput)("base_ref") === "" ? void 0 : (0, import_core.getInput)("base_ref");
const ref = import_github.context.payload.pull_request?.base.ref;
if (!base_ref && !ref) {
throw new Error("No 'base_ref' provided and not a pull request");
}
const results = [];
const globber = await (0, import_glob.create)((0, import_core.getInput)("files"));
Expand All @@ -25993,7 +25994,7 @@ var run = async () => {
for (const filePath of files) {
const relativePath = import_node_path.default.relative(process.cwd(), filePath);
(0, import_core.debug)(`Comparing ${relativePath}`);
const before = await getBaseFile(relativePath);
const before = await getBaseFile(relativePath, base_ref ?? ref);
const after = JSON.parse(await (0, import_promises.readFile)(filePath, "utf8"));
const diff = snapshotDiff({
before,
Expand Down
13 changes: 7 additions & 6 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import path from "node:path";
const heading = getInput("heading");
const octokit = getOctokit(getInput("token"));

const getBaseFile = async (path: string) => {
const getBaseFile = async (path: string, ref: string) => {
const { data } = await octokit.rest.repos.getContent({
repo: context.repo.repo,
owner: context.repo.owner,
path,
ref: context.payload.pull_request?.base.ref,
ref,
}).catch(e => {
return e.response as {
status: number
Expand All @@ -23,7 +23,6 @@ const getBaseFile = async (path: string) => {
};
};
})
debug(`Base file ${path}: ${JSON.stringify(data)}`);

if (!data || !("content" in data)) {
return {};
Expand All @@ -35,8 +34,10 @@ const getBaseFile = async (path: string) => {

const run = async () => {
debug("Starting run");
if (!("pull_request" in context.payload)) {
throw new Error("This action can only be run on pull_request events");
const base_ref = getInput("base_ref") === "" ? undefined : getInput("base_ref");
const ref = context.payload.pull_request?.base.ref;
if (!base_ref && !ref) {
throw new Error("No 'base_ref' provided and not a pull request");
}
const results: Parameters<typeof formatDiffMd>[1] = [];
const globber = await createGlob(getInput("files"));
Expand All @@ -46,7 +47,7 @@ const run = async () => {
for (const filePath of files) {
const relativePath = path.relative(process.cwd(), filePath);
debug(`Comparing ${relativePath}`);
const before = await getBaseFile(relativePath);
const before = await getBaseFile(relativePath, base_ref ?? ref);
const after = JSON.parse(await readFile(filePath, "utf8"));
const diff = snapshotDiff({
before,
Expand Down

0 comments on commit fd355a8

Please sign in to comment.