From 61703ed12ea97bfe6e7ede39595d6e14a3a2ecec Mon Sep 17 00:00:00 2001 From: "David J. Felix" Date: Mon, 30 Sep 2024 12:17:11 -0500 Subject: [PATCH] Fix changed-files for cases where no python files changed (#4) ## Problem When a PR contains no changed python files, but changed-files is set to true, ruff will run on all files, including ones not changed. ## Solution Pass the input through to the script and short circuit to success in cases where both the flag is set and no python files were changed. --- action.yml | 1 + action/main.py | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/action.yml b/action.yml index 3c56a65..1c0fbf0 100644 --- a/action.yml +++ b/action.yml @@ -41,6 +41,7 @@ runs: INPUT_ARGS: ${{ inputs.args }} INPUT_SRC: ${{ inputs.src }} INPUT_VERSION: ${{ inputs.version }} + IS_CHANGED_FILES_ENABLED: ${{ inputs.changed-files }} CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} pythonioencoding: utf-8 shell: bash diff --git a/action/main.py b/action/main.py index 66a2afa..8482cd2 100644 --- a/action/main.py +++ b/action/main.py @@ -12,6 +12,7 @@ SRC = os.getenv("INPUT_SRC", default="") VERSION = os.getenv("INPUT_VERSION", default="") CHANGED_FILES = os.getenv("CHANGED_FILES", "") +IS_CHANGED_FILES_ENABLED = os.getenv("IS_CHANGED_FILES_ENABLED", default="false") version_specifier = "" if VERSION != "": @@ -25,6 +26,11 @@ # If `CHANGED_FILES` is not empty, split it into a list; otherwise, use `SRC`. files_to_check = shlex.split(CHANGED_FILES or SRC) +# If IS_CHANGED_FILES_ENABLED is true and CHANGED_FILES was empty, there were no files to check +# Short circuit to prevent ruff from running on all files because it got no file argument +if IS_CHANGED_FILES_ENABLED == "true" and not CHANGED_FILES: + sys.exit(0) + proc = run(["pipx", "run", req, *shlex.split(ARGS), *files_to_check]) sys.exit(proc.returncode)