From 013e2c2887e0fff49bece8d0db91afb64c1ca059 Mon Sep 17 00:00:00 2001 From: Omar Diab Date: Wed, 29 Nov 2023 05:17:25 -0500 Subject: [PATCH] Feat: Support include/exclude in github action (#135) --- .github/workflows/test.yml | 25 ++++++++++++++++++++++++- action.yml | 6 ++++++ action/deps.js | 6 +++--- action/index.js | 11 +++++++++-- action/tests/hello.ts | 11 +++++++++++ action/tests/import_bomb1 | 0 action/tests/import_bomb2 | 0 action/tests/import_map.json | 5 +++++ action/tests/include_exclude.ts | 17 +++++++++++++++++ 9 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 action/tests/hello.ts create mode 100644 action/tests/import_bomb1 create mode 100644 action/tests/import_bomb2 create mode 100644 action/tests/import_map.json create mode 100644 action/tests/include_exclude.ts diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c142091a..cb3af3db 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,6 +20,29 @@ jobs: uses: ./ with: project: happy-rat-64 - root: examples + root: action/tests entrypoint: hello.ts import-map: ./import_map.json + - name: Deploy with single include + uses: ./ + with: + project: happy-rat-64 + root: action/tests + entrypoint: include_exclude.ts + include: include_exclude.ts + - name: Deploy with comma-separated exclude + uses: ./ + with: + project: happy-rat-64 + root: action/tests + entrypoint: include_exclude.ts + exclude: import_bomb1,import_bomb2 + - name: Deploy with multiline exclude + uses: ./ + with: + project: happy-rat-64 + root: action/tests + entrypoint: include_exclude.ts + exclude: | + import_bomb1 + import_bomb2 diff --git a/action.yml b/action.yml index eab3ea7a..92cf5df1 100644 --- a/action.yml +++ b/action.yml @@ -16,6 +16,12 @@ inputs: import-map: description: The path or URL to an import map file required: false + include: + description: Only upload files that match this pattern (multiline and/or comma-separated) + required: false + exclude: + description: Exclude files that match this pattern (multiline and/or comma-separated) + required: false root: description: The path to the directory containing the code and assets to upload required: false diff --git a/action/deps.js b/action/deps.js index 1b4d7cd3..66120d34 100644 --- a/action/deps.js +++ b/action/deps.js @@ -4006,10 +4006,10 @@ async function calculateGitSha1(bytes) { return hashHex; } function include(path, include, exclude) { - if (include && !include.some((pattern)=>path.startsWith(pattern))) { + if (include.length && !include.some((pattern)=>path.startsWith(pattern))) { return false; } - if (exclude && exclude.some((pattern)=>path.startsWith(pattern))) { + if (exclude.length && exclude.some((pattern)=>path.startsWith(pattern))) { return false; } return true; @@ -4054,5 +4054,5 @@ async function walk(cwd, dir, files, options) { export { parseEntrypoint as parseEntrypoint }; export { API as API, APIError as APIError }; export { walk as walk }; -export { fromFileUrl2 as fromFileUrl, resolve2 as resolve }; +export { fromFileUrl2 as fromFileUrl, resolve2 as resolve, normalize3 as normalize }; diff --git a/action/index.js b/action/index.js index 40276b55..edcd3f92 100644 --- a/action/index.js +++ b/action/index.js @@ -5,6 +5,7 @@ import { API, APIError, fromFileUrl, + normalize, parseEntrypoint, resolve, walk, @@ -17,6 +18,8 @@ async function main() { const projectId = core.getInput("project", { required: true }); const entrypoint = core.getInput("entrypoint", { required: true }); const importMap = core.getInput("import-map", {}); + const include = core.getMultilineInput("include", {}); + const exclude = core.getMultilineInput("exclude", {}); const cwd = resolve(process.cwd(), core.getInput("root", {})); if (github.context.eventName === "pull_request") { @@ -72,8 +75,12 @@ async function main() { core.debug(`Discovering assets in "${cwd}"`); const assets = new Map(); const entries = await walk(cwd, cwd, assets, { - include: undefined, - exclude: undefined, + include: include.flatMap((i) => i.split(",")).map((pattern) => + normalize(pattern) + ), + exclude: exclude.flatMap((e) => e.split(",")).map((pattern) => + normalize(pattern) + ), }); core.debug(`Discovered ${assets.size} assets`); diff --git a/action/tests/hello.ts b/action/tests/hello.ts new file mode 100644 index 00000000..805c71ca --- /dev/null +++ b/action/tests/hello.ts @@ -0,0 +1,11 @@ +import { serve } from "std/http/server.ts"; + +async function handler(_req: Request) { + const text = await Deno.readTextFile(new URL(import.meta.url)); + return new Response(text, { + headers: { "content-type": "text/plain; charset=utf8" }, + }); +} + +console.log("Listening on http://localhost:8000"); +serve(handler); diff --git a/action/tests/import_bomb1 b/action/tests/import_bomb1 new file mode 100644 index 00000000..e69de29b diff --git a/action/tests/import_bomb2 b/action/tests/import_bomb2 new file mode 100644 index 00000000..e69de29b diff --git a/action/tests/import_map.json b/action/tests/import_map.json new file mode 100644 index 00000000..62266803 --- /dev/null +++ b/action/tests/import_map.json @@ -0,0 +1,5 @@ +{ + "imports": { + "std/": "https://deno.land/std@0.128.0/" + } +} diff --git a/action/tests/include_exclude.ts b/action/tests/include_exclude.ts new file mode 100644 index 00000000..dcbda1a4 --- /dev/null +++ b/action/tests/include_exclude.ts @@ -0,0 +1,17 @@ +try { + await Deno.lstat("import_bomb1"); + throw new Error("BOOM!"); +} catch (e) { + if (!(e instanceof Deno.errors.NotFound)) { + throw e; + } +} +try { + await Deno.lstat("import_bomb2"); + throw new Error("BOOM!"); +} catch (e) { + if (!(e instanceof Deno.errors.NotFound)) { + throw e; + } +} +Deno.serve(() => new Response("Hello World"));