From 6eaa5c15eb569ce9a9f8cedaa09a416f0647f43f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFs=20Postula?= Date: Fri, 2 Feb 2024 22:09:11 +0100 Subject: [PATCH] feat: add working_directory --- action.yml | 4 ++++ src/main.ts | 19 ++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/action.yml b/action.yml index b7a5fd0..ac0c6d7 100644 --- a/action.yml +++ b/action.yml @@ -21,6 +21,10 @@ inputs: description: 'Fallback value in case no file inside the given workspaces were changed' required: false + working-directory: + description: + 'Subdirectory where the root Cargo.toml is' + required: false outputs: matrix: diff --git a/src/main.ts b/src/main.ts index ad7b804..8ff6a01 100644 --- a/src/main.ts +++ b/src/main.ts @@ -10,7 +10,7 @@ type OctoKit = InstanceType; * @returns {Promise} Resolves when the action is complete. */ async function run() { - const { workspaces, fallback, token, base_ref, head_ref } = fetchInputs(); + const { workspaces, fallback, token, base_ref, head_ref, working_directory } = fetchInputs(); const octokit = github.getOctokit(token); const context = github.context; @@ -28,7 +28,7 @@ async function run() { const tree = await fetchTree(octokit, context, head_ref); debugArray('Tree:', tree); - const crate_paths = filterTree(tree, workspaces); + const crate_paths = filterTree(tree, workspaces, working_directory); debugArray('Crate paths:', crate_paths); const diff = await fetchDiff(octokit, context, base_ref, head_ref); debugArray('Changed files:', diff); @@ -63,8 +63,14 @@ async function fetchTree( return tree; } -function filterTree(tree: string[], workspaces: string[]): string[] { - const filtered_tree = tree.filter(x => workspaces.some(w => x.startsWith(w))); +function filterTree(tree: string[], workspaces: string[], working_directory: string): string[] { + const filtered_tree = tree.filter(x => + workspaces.some(w => { + const workspace_path = + working_directory.length > 0 ? `${working_directory}/${w}` : w; + return x.startsWith(workspace_path); + }) + ); const crate_paths = filtered_tree .filter(x => x.endsWith('Cargo.toml')) .map(x => { @@ -112,7 +118,10 @@ function fetchInputs() { // todo: we can fetch these 2 ourselves const base_ref = core.getInput('base_ref'); const head_ref = core.getInput('head_ref'); - return { workspaces, fallback, token, base_ref, head_ref }; + + const working_directory = core.getInput('working-directory'); + + return { workspaces, fallback, token, base_ref, head_ref, working_directory }; } function setOutput(matrix: string[], fallback = false) {