From fd355a8aae57d07e1dfafdc6b6651d27e5f5448b Mon Sep 17 00:00:00 2001 From: Jonas Strassel Date: Wed, 29 Jan 2025 18:40:46 +0100 Subject: [PATCH] docs: update readme, readd ref --- README.md | 10 +++------- action.yml | 6 +++++- dist/action.js | 13 +++++++------ src/action.ts | 13 +++++++------ 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index a1eb7f2..44ea67e 100644 --- a/README.md +++ b/README.md @@ -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' ``` diff --git a/action.yml b/action.yml index ea48fb5..1c6c3bb 100644 --- a/action.yml +++ b/action.yml @@ -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: diff --git a/dist/action.js b/dist/action.js index 92dd225..a61a08c 100644 --- a/dist/action.js +++ b/dist/action.js @@ -25966,16 +25966,15 @@ 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 {}; } @@ -25983,8 +25982,10 @@ var getBaseFile = async (path2) => { }; 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")); @@ -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, diff --git a/src/action.ts b/src/action.ts index 4584087..bf781ea 100644 --- a/src/action.ts +++ b/src/action.ts @@ -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 @@ -23,7 +23,6 @@ const getBaseFile = async (path: string) => { }; }; }) - debug(`Base file ${path}: ${JSON.stringify(data)}`); if (!data || !("content" in data)) { return {}; @@ -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[1] = []; const globber = await createGlob(getInput("files")); @@ -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,