Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support requirements.txt for version-file #68

Merged
merged 6 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions __tests__/fixtures/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruff==0.9.0
47 changes: 27 additions & 20 deletions dist/ruff-action/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 43 additions & 39 deletions src/utils/pyproject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string[]>;
};
"dependency-groups"?: Map<string, Array<string | object>>;
}
| 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"),
);
Expand All @@ -58,3 +20,45 @@ export function getRuffVersionFromPyproject(

return undefined;
}

function parsePyproject(pyprojectContent: string): string | undefined {
const pyproject: {
project?: {
dependencies?: string[];
"optional-dependencies"?: Map<string, string[]>;
};
"dependency-groups"?: Map<string, Array<string | object>>;
} = 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;
}
}
Loading