mirror of
https://github.com/astral-sh/ruff-action.git
synced 2026-05-12 20:50:14 +02:00
Add support for reading the ruff version from Poetry groups (#127)
Not sure if this is a desired feature since this was requested already (https://github.com/astral-sh/ruff-action/issues/40#issuecomment-2581207691) without any sort of response. Unfortunately Poetry doesn't yet support PEP 735 (https://github.com/python-poetry/poetry/issues/9751) and it doesn't make sense to add ruff as a project dependency.
This commit is contained in:
@@ -118,6 +118,23 @@ jobs:
|
|||||||
fi
|
fi
|
||||||
env:
|
env:
|
||||||
RUFF_VERSION: ${{ steps.ruff-action.outputs.ruff-version }}
|
RUFF_VERSION: ${{ steps.ruff-action.outputs.ruff-version }}
|
||||||
|
test-default-version-from-pyproject-poetry-groups:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Use default version from pyproject.toml poetry group dependencies
|
||||||
|
id: ruff-action
|
||||||
|
uses: ./
|
||||||
|
with:
|
||||||
|
src: __tests__/fixtures/pyproject-dependency-poetry-project
|
||||||
|
version-file: __tests__/fixtures/pyproject-dependency-poetry-project/pyproject.toml
|
||||||
|
- name: Correct version gets installed
|
||||||
|
run: |
|
||||||
|
if [ "$RUFF_VERSION" != "0.8.3" ]; then
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
env:
|
||||||
|
RUFF_VERSION: ${{ steps.ruff-action.outputs.ruff-version }}
|
||||||
test-default-version-from-pyproject-optional-dependencies:
|
test-default-version-from-pyproject-optional-dependencies:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
@@ -259,6 +276,7 @@ jobs:
|
|||||||
- test-default-version-from-pyproject
|
- test-default-version-from-pyproject
|
||||||
- test-default-version-from-pyproject-dev-group
|
- test-default-version-from-pyproject-dev-group
|
||||||
- test-default-version-from-pyproject-dependency-groups
|
- test-default-version-from-pyproject-dependency-groups
|
||||||
|
- test-default-version-from-pyproject-poetry-groups
|
||||||
- test-default-version-from-pyproject-optional-dependencies
|
- test-default-version-from-pyproject-optional-dependencies
|
||||||
- test-default-version-from-requirements
|
- test-default-version-from-requirements
|
||||||
- test-semver-range
|
- test-semver-range
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
[project]
|
||||||
|
name = "pyproject-dependency-poetry-project"
|
||||||
|
version = "0.1.0"
|
||||||
|
description = "Add your description here"
|
||||||
|
readme = "README.md"
|
||||||
|
requires-python = ">=3.12"
|
||||||
|
|
||||||
|
[tool.poetry.group.dev.dependencies]
|
||||||
|
sphinx = "*"
|
||||||
|
ruff = "0.8.3"
|
||||||
|
|
||||||
|
[build-system]
|
||||||
|
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
||||||
|
build-backend = "poetry.core.masonry.api"
|
||||||
+2
@@ -0,0 +1,2 @@
|
|||||||
|
def hello() -> str:
|
||||||
|
return "Hello from python-project!"
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
print("Hello world!")
|
||||||
+14
-1
@@ -30933,7 +30933,20 @@ function parsePyproject(pyprojectContent) {
|
|||||||
const devDependencies = Object.values(pyproject?.["dependency-groups"] || {})
|
const devDependencies = Object.values(pyproject?.["dependency-groups"] || {})
|
||||||
.flat()
|
.flat()
|
||||||
.filter((item) => typeof item === "string");
|
.filter((item) => typeof item === "string");
|
||||||
return getRuffVersionFromAllDependencies(dependencies.concat(optionalDependencies, devDependencies));
|
return (getRuffVersionFromAllDependencies(dependencies.concat(optionalDependencies, devDependencies)) || getRuffVersionFromPoetryGroups(pyproject));
|
||||||
|
}
|
||||||
|
function getRuffVersionFromPoetryGroups(pyproject) {
|
||||||
|
// Special handling for Poetry until it supports PEP 735
|
||||||
|
// See: <https://github.com/python-poetry/poetry/issues/9751>
|
||||||
|
const poetryGroups = Object.values(pyproject?.tool?.poetry?.group || {});
|
||||||
|
return poetryGroups
|
||||||
|
.flatMap((group) => Object.entries(group.dependencies))
|
||||||
|
.map(([name, spec]) => {
|
||||||
|
if (name === "ruff" && typeof spec === "string")
|
||||||
|
return spec;
|
||||||
|
return undefined;
|
||||||
|
})
|
||||||
|
.find((version) => version !== undefined);
|
||||||
}
|
}
|
||||||
function getRuffVersionFromRequirementsFile(filePath) {
|
function getRuffVersionFromRequirementsFile(filePath) {
|
||||||
if (!fs.existsSync(filePath)) {
|
if (!fs.existsSync(filePath)) {
|
||||||
|
|||||||
+33
-9
@@ -23,14 +23,21 @@ function getRuffVersionFromAllDependencies(
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
function parsePyproject(pyprojectContent: string): string | undefined {
|
interface Pyproject {
|
||||||
const pyproject: {
|
project?: {
|
||||||
project?: {
|
dependencies?: string[];
|
||||||
dependencies?: string[];
|
"optional-dependencies"?: Record<string, string[]>;
|
||||||
"optional-dependencies"?: Map<string, string[]>;
|
};
|
||||||
|
"dependency-groups"?: Record<string, Array<string | object>>;
|
||||||
|
tool?: {
|
||||||
|
poetry?: {
|
||||||
|
group?: Record<string, { dependencies: Record<string, string | object> }>;
|
||||||
};
|
};
|
||||||
"dependency-groups"?: Map<string, Array<string | object>>;
|
};
|
||||||
} = toml.parse(pyprojectContent);
|
}
|
||||||
|
|
||||||
|
function parsePyproject(pyprojectContent: string): string | undefined {
|
||||||
|
const pyproject: Pyproject = toml.parse(pyprojectContent);
|
||||||
const dependencies: string[] = pyproject?.project?.dependencies || [];
|
const dependencies: string[] = pyproject?.project?.dependencies || [];
|
||||||
const optionalDependencies: string[] = Object.values(
|
const optionalDependencies: string[] = Object.values(
|
||||||
pyproject?.project?.["optional-dependencies"] || {},
|
pyproject?.project?.["optional-dependencies"] || {},
|
||||||
@@ -40,11 +47,28 @@ function parsePyproject(pyprojectContent: string): string | undefined {
|
|||||||
)
|
)
|
||||||
.flat()
|
.flat()
|
||||||
.filter((item: string | object) => typeof item === "string");
|
.filter((item: string | object) => typeof item === "string");
|
||||||
return getRuffVersionFromAllDependencies(
|
return (
|
||||||
dependencies.concat(optionalDependencies, devDependencies),
|
getRuffVersionFromAllDependencies(
|
||||||
|
dependencies.concat(optionalDependencies, devDependencies),
|
||||||
|
) || getRuffVersionFromPoetryGroups(pyproject)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getRuffVersionFromPoetryGroups(
|
||||||
|
pyproject: Pyproject,
|
||||||
|
): string | undefined {
|
||||||
|
// Special handling for Poetry until it supports PEP 735
|
||||||
|
// See: <https://github.com/python-poetry/poetry/issues/9751>
|
||||||
|
const poetryGroups = Object.values(pyproject?.tool?.poetry?.group || {});
|
||||||
|
return poetryGroups
|
||||||
|
.flatMap((group) => Object.entries(group.dependencies))
|
||||||
|
.map(([name, spec]) => {
|
||||||
|
if (name === "ruff" && typeof spec === "string") return spec;
|
||||||
|
return undefined;
|
||||||
|
})
|
||||||
|
.find((version) => version !== undefined);
|
||||||
|
}
|
||||||
|
|
||||||
export function getRuffVersionFromRequirementsFile(
|
export function getRuffVersionFromRequirementsFile(
|
||||||
filePath: string,
|
filePath: string,
|
||||||
): string | undefined {
|
): string | undefined {
|
||||||
|
|||||||
Reference in New Issue
Block a user