From d8f577dec404a8f2793b667c28edd330427c680c Mon Sep 17 00:00:00 2001 From: Dave Johansen Date: Thu, 6 Feb 2025 09:41:44 -0700 Subject: [PATCH] Support requirements.txt for version-file (#68) --- .github/workflows/test.yml | 17 ++++++ README.md | 4 +- __tests__/fixtures/requirements.txt | 1 + dist/ruff-action/index.js | Bin 1450753 -> 1450988 bytes src/utils/pyproject.ts | 82 +++++++++++++++------------- 5 files changed, 63 insertions(+), 41 deletions(-) create mode 100644 __tests__/fixtures/requirements.txt diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index eaae432..24b3318 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -135,6 +135,23 @@ jobs: fi env: RUFF_VERSION: ${{ steps.ruff-action.outputs.ruff-version }} + test-default-version-from-requirements: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Use default version from requirements.txt + id: ruff-action + uses: ./ + with: + src: __tests__/fixtures/python-project + version-file: __tests__/fixtures/requirements.txt + - name: Correct version gets installed + run: | + if [ "$RUFF_VERSION" != "0.9.0" ]; then + exit 1 + fi + env: + RUFF_VERSION: ${{ steps.ruff-action.outputs.ruff-version }} test-semver-range: runs-on: ubuntu-latest steps: diff --git a/README.md b/README.md index 371453b..f437a14 100644 --- a/README.md +++ b/README.md @@ -124,13 +124,13 @@ to install the latest version that satisfies the range. #### Install a version from a specified version file You can specify a file to read the version from. -Currently `pyproject.toml` is supported. +Currently `pyproject.toml` and `requirements.txt` are supported. ```yaml - name: Install a version from a specified version file uses: astral-sh/ruff-action@v3 with: - version-file: "my-path/to/pyproject.toml" + version-file: "my-path/to/pyproject.toml-or-requirements.txt" ``` ### Validate checksum diff --git a/__tests__/fixtures/requirements.txt b/__tests__/fixtures/requirements.txt new file mode 100644 index 0000000..74c80dc --- /dev/null +++ b/__tests__/fixtures/requirements.txt @@ -0,0 +1 @@ +ruff==0.9.0 diff --git a/dist/ruff-action/index.js b/dist/ruff-action/index.js index 8d79f29fa2f7f9faf1eaca1e67d6d1c0b8ba1cc0..4478d967f4931a4922ce6cdee1e4e23edb02f6e0 100644 GIT binary patch delta 332 zcmZoX7V+kIL_-T>3sVbo3rh=Y3tJ0&3r7oQ3s(zw3r`Dg3ttO=i$IIu7NO7$Dg}u} z#i>E5g{7HAsky0nCB+(vIXNz=1*v%{sd>qnsl}QK)!R>O7P`e~npT>ZT#}iervTOy zP+3rvpOuh4i-@ZW3}~lq^auDJ_C(N3n8x z?`9z@7ENoe+UedKgucpVrYUHoW#*&?B$j080bNoYo>`Kip`=$*QKF3sVbo3rh=Y3tJ0&3r7oQ3s(zw3r`Dg3ttO=i$IIu7NO7$(+k%N zNl!1|B6L9^C$&VOpt7JSKPxr4#F|S12ug}76{^83AZL2VCL#UlZ?_7~+rD{|kO!kg za(-TMi9%vdj!SAmYFn$;Vw~=>ja{UD;Wi;476xLG L?F+YwdglTF?D;%J diff --git a/src/utils/pyproject.ts b/src/utils/pyproject.ts index ec81eba..055ffc9 100644 --- a/src/utils/pyproject.ts +++ b/src/utils/pyproject.ts @@ -2,45 +2,7 @@ import * as fs from "node:fs"; import * as core from "@actions/core"; import * as toml from "smol-toml"; -export function getRuffVersionFromPyproject( - filePath: string, -): string | undefined { - if (!fs.existsSync(filePath)) { - core.warning(`Could not find file: ${filePath}`); - return undefined; - } - const pyprojectContent = fs.readFileSync(filePath, "utf-8"); - let pyproject: - | { - project?: { - dependencies?: string[]; - "optional-dependencies"?: Map; - }; - "dependency-groups"?: Map>; - } - | undefined; - try { - pyproject = toml.parse(pyprojectContent); - } catch (err) { - const message = (err as Error).message; - core.warning(`Error while parsing ${filePath}: ${message}`); - return undefined; - } - - const dependencies: string[] = pyproject?.project?.dependencies || []; - const optionalDependencies: string[] = Object.values( - pyproject?.project?.["optional-dependencies"] || {}, - ).flat(); - const devDependencies: string[] = Object.values( - pyproject?.["dependency-groups"] || {}, - ) - .flat() - .filter((item: string | object) => typeof item === "string"); - const allDependencies: string[] = dependencies.concat( - optionalDependencies, - devDependencies, - ); - +function parseRequirements(allDependencies: string[]): string | undefined { const ruffVersionDefinition = allDependencies.find((dep: string) => dep.startsWith("ruff"), ); @@ -58,3 +20,45 @@ export function getRuffVersionFromPyproject( return undefined; } + +function parsePyproject(pyprojectContent: string): string | undefined { + const pyproject: { + project?: { + dependencies?: string[]; + "optional-dependencies"?: Map; + }; + "dependency-groups"?: Map>; + } = toml.parse(pyprojectContent); + const dependencies: string[] = pyproject?.project?.dependencies || []; + const optionalDependencies: string[] = Object.values( + pyproject?.project?.["optional-dependencies"] || {}, + ).flat(); + const devDependencies: string[] = Object.values( + pyproject?.["dependency-groups"] || {}, + ) + .flat() + .filter((item: string | object) => typeof item === "string"); + return parseRequirements( + dependencies.concat(optionalDependencies, devDependencies), + ); +} + +export function getRuffVersionFromPyproject( + filePath: string, +): string | undefined { + if (!fs.existsSync(filePath)) { + core.warning(`Could not find file: ${filePath}`); + return undefined; + } + const pyprojectContent = fs.readFileSync(filePath, "utf-8"); + if (filePath.endsWith(".txt")) { + return parseRequirements(pyprojectContent.split("\n")); + } + try { + return parsePyproject(pyprojectContent); + } catch (err) { + const message = (err as Error).message; + core.warning(`Error while parsing ${filePath}: ${message}`); + return undefined; + } +}