From 992901c2013389caa8a96a445484a27dd72f11d4 Mon Sep 17 00:00:00 2001 From: Amin Sadeghi Date: Thu, 4 Apr 2024 14:40:34 -0400 Subject: [PATCH 1/4] First try at implementing only-apply-to-changed-files --- action.yml | 9 +++++++++ action/main.py | 7 ++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 4956cbc..ef2ecfc 100644 --- a/action.yml +++ b/action.yml @@ -16,12 +16,20 @@ inputs: description: 'The version of ruff to use, e.g. "0.0.259"' required: false default: "" + changed-files: + description: 'Whether to only run ruff on changed files. Default: false' + required: false + default: "false" branding: color: "black" icon: "code" runs: using: composite steps: + - name: Get changed files + id: changed-files + if: ${{ inputs.changed-files == 'true' }} + uses: tj-actions/changed-files@v44 - run: | if [ "$RUNNER_OS" == "Windows" ]; then python $GITHUB_ACTION_PATH/action/main.py @@ -33,5 +41,6 @@ runs: INPUT_ARGS: ${{ inputs.args }} INPUT_SRC: ${{ inputs.src }} INPUT_VERSION: ${{ inputs.version }} + 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 1e0e74f..14a3e5f 100644 --- a/action/main.py +++ b/action/main.py @@ -11,6 +11,7 @@ ACTION_PATH = Path(os.environ["GITHUB_ACTION_PATH"]) ARGS = os.getenv("INPUT_ARGS", default="") SRC = os.getenv("INPUT_SRC", default="") VERSION = os.getenv("INPUT_VERSION", default="") +CHANGED_FILES = os.getenv("CHANGED_FILES", "") version_specifier = "" if VERSION != "": @@ -21,6 +22,10 @@ if VERSION != "": req = f"ruff{version_specifier}" -proc = run(["pipx", "run", req, *shlex.split(ARGS), *shlex.split(SRC)]) +# If CHANGED_FILES is not empty, split it into a list; otherwise, use SRC +files_to_check = shlex.split(CHANGED_FILES or SRC) + +proc = run(["pipx", "run", req, *shlex.split(ARGS), *files_to_check]) +# proc = run(["pipx", "run", req, *shlex.split(ARGS), *shlex.split(SRC)]) sys.exit(proc.returncode) From a9c46eeb9c58f2bfad37d09ca7c4768ff435d747 Mon Sep 17 00:00:00 2001 From: Amin Sadeghi Date: Thu, 4 Apr 2024 14:53:05 -0400 Subject: [PATCH 2/4] Exclude non-Python files --- action/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/action/main.py b/action/main.py index 14a3e5f..f7b23fb 100644 --- a/action/main.py +++ b/action/main.py @@ -24,8 +24,9 @@ req = f"ruff{version_specifier}" # If CHANGED_FILES is not empty, split it into a list; otherwise, use SRC files_to_check = shlex.split(CHANGED_FILES or SRC) +# Exclude non-Python files +files_to_check = [f for f in files_to_check if f.endswith(".py")] proc = run(["pipx", "run", req, *shlex.split(ARGS), *files_to_check]) -# proc = run(["pipx", "run", req, *shlex.split(ARGS), *shlex.split(SRC)]) sys.exit(proc.returncode) From 0df4a956866f879b088ff34d6a0f0313e4e35dbc Mon Sep 17 00:00:00 2001 From: Amin Sadeghi Date: Thu, 4 Apr 2024 15:25:49 -0400 Subject: [PATCH 3/4] Exclude non-Python files as part of changed-files action --- action.yml | 2 ++ action/main.py | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index ef2ecfc..c27cf39 100644 --- a/action.yml +++ b/action.yml @@ -30,6 +30,8 @@ runs: id: changed-files if: ${{ inputs.changed-files == 'true' }} uses: tj-actions/changed-files@v44 + with: + files: '**.py' - run: | if [ "$RUNNER_OS" == "Windows" ]; then python $GITHUB_ACTION_PATH/action/main.py diff --git a/action/main.py b/action/main.py index f7b23fb..e91719e 100644 --- a/action/main.py +++ b/action/main.py @@ -24,8 +24,6 @@ req = f"ruff{version_specifier}" # If CHANGED_FILES is not empty, split it into a list; otherwise, use SRC files_to_check = shlex.split(CHANGED_FILES or SRC) -# Exclude non-Python files -files_to_check = [f for f in files_to_check if f.endswith(".py")] proc = run(["pipx", "run", req, *shlex.split(ARGS), *files_to_check]) From 79eabade5419b7a0d74dca0819dc09b201e6f281 Mon Sep 17 00:00:00 2001 From: Amin Sadeghi Date: Thu, 4 Apr 2024 15:34:23 -0400 Subject: [PATCH 4/4] Document `changed-files` keyword in README --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 815919e..78527f0 100644 --- a/README.md +++ b/README.md @@ -52,3 +52,10 @@ See [Configuring Ruff](https://github.com/astral-sh/ruff/blob/main/docs/configur with: args: 'format --check' ``` + +### Only run ruff on changed files +```yaml +- uses: chartboost/ruff-action@v1 + with: + changed-files: 'true' +```