Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
daveisfera committed Feb 1, 2025
1 parent 3ba6e14 commit bb5308c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 76 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ jobs:
id: ruff-action
uses: ./
with:
src: __tests__/fixtures/requirements
version-file: __tests__/fixtures/requirements/requirements.txt
src: __tests__/fixtures/python-project
version-file: __tests__/fixtures/requirements.txt
- name: Correct version gets installed
run: |
if [ "$RUFF_VERSION" != "0.8.3" ]; then
Expand Down
13 changes: 2 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +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"
```

And `requirements.txt` is supported.

```yaml
- name: Install a version from a specified version file
uses: astral-sh/ruff-action@v3
with:
version-file: "my-path/to/requirements.txt"
version-file: "my-path/to/pyproject.toml-or-requirements.txt"
```

### Validate checksum
Expand Down
File renamed without changes.
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.

86 changes: 43 additions & 43 deletions src/utils/pyproject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +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;
let allDependencies: string[];
if (filePath.endsWith(".txt")) {
allDependencies = pyprojectContent.split("\n");
} else {
try {
pyproject = 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");
allDependencies = dependencies.concat(
optionalDependencies,
devDependencies,
);
} catch (err) {
const message = (err as Error).message;
core.warning(`Error while parsing ${filePath}: ${message}`);
return undefined;
}
}

function parseRequirements(allDependencies: string[]): string | undefined {
const ruffVersionDefinition = allDependencies.find((dep: string) =>
dep.startsWith("ruff"),
);
Expand All @@ -62,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;
}
}

0 comments on commit bb5308c

Please sign in to comment.