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

[ci] enable ruff-format on some files, add pre-commit config #6308

Merged
merged 8 commits into from
Feb 15, 2024
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
33 changes: 17 additions & 16 deletions .ci/get_workflow_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,26 @@ def get_runs(trigger_phrase):
"""
pr_runs = []
if environ.get("GITHUB_EVENT_NAME", "") == "pull_request":
pr_number = int(environ.get("GITHUB_REF").split('/')[-2])
pr_number = int(environ.get("GITHUB_REF").split("/")[-2])
page = 1
while True:
req = request.Request(
url="{}/repos/microsoft/LightGBM/issues/{}/comments?page={}&per_page=100".format(
environ.get("GITHUB_API_URL"),
pr_number,
page
environ.get("GITHUB_API_URL"), pr_number, page
),
headers={"Accept": "application/vnd.github.v3+json"}
headers={"Accept": "application/vnd.github.v3+json"},
)
url = request.urlopen(req)
data = json.loads(url.read().decode('utf-8'))
data = json.loads(url.read().decode("utf-8"))
url.close()
if not data:
break
runs_on_page = [i for i in data
if i['author_association'].lower() in {'owner', 'member', 'collaborator'}
and i['body'].startswith('/gha run {}'.format(trigger_phrase))]
runs_on_page = [
i
for i in data
if i["author_association"].lower() in {"owner", "member", "collaborator"}
and i["body"].startswith("/gha run {}".format(trigger_phrase))
]
pr_runs.extend(runs_on_page)
page += 1
return pr_runs[::-1]
Expand All @@ -70,20 +71,20 @@ def get_status(runs):
The most recent status of workflow.
Can be 'success', 'failure' or 'in-progress'.
"""
status = 'success'
status = "success"
for run in runs:
body = run['body']
body = run["body"]
if "Status: " in body:
if "Status: skipped" in body:
continue
if "Status: failure" in body:
status = 'failure'
status = "failure"
break
if "Status: success" in body:
status = 'success'
status = "success"
break
else:
status = 'in-progress'
status = "in-progress"
break
return status

Expand All @@ -92,8 +93,8 @@ def get_status(runs):
trigger_phrase = argv[1]
while True:
status = get_status(get_runs(trigger_phrase))
if status != 'in-progress':
if status != "in-progress":
break
sleep(60)
if status == 'failure':
if status == "failure":
exit(1)
17 changes: 3 additions & 14 deletions .ci/lint-python.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
#!/bin/sh

echo "running ruff"
ruff check \
--config=./python-package/pyproject.toml \
. \
|| exit 1
echo "done running ruff"

echo "running isort"
isort \
--check-only \
--settings-path=./python-package/pyproject.toml \
. \
|| exit 1
echo "done running isort"
echo "running pre-commit checks"
pre-commit run --all-files || exit 1
echo "done running pre-commit checks"

echo "running mypy"
mypy \
Expand Down
5 changes: 2 additions & 3 deletions .ci/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ if [[ $TASK == "lint" ]]; then
${CONDA_PYTHON_REQUIREMENT} \
cmakelint \
cpplint \
isort \
mypy \
'r-lintr>=3.1' \
ruff
'pre-commit>=3.6.0' \
'r-lintr>=3.1'
source activate $CONDA_ENV
echo "Linting Python code"
sh ${BUILD_DIRECTORY}/.ci/lint-python.sh || exit 1
Expand Down
8 changes: 4 additions & 4 deletions .nuget/create_nuget.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
copyfile(source / "lib_lightgbm.dylib", osx_folder_path / "lib_lightgbm.dylib")
copyfile(source / "lib_lightgbm.dll", windows_folder_path / "lib_lightgbm.dll")
copyfile(source / "lightgbm.exe", windows_folder_path / "lightgbm.exe")
version = (current_dir.parent / 'VERSION.txt').read_text(encoding='utf-8').strip().replace('rc', '-rc')
version = (current_dir.parent / "VERSION.txt").read_text(encoding="utf-8").strip().replace("rc", "-rc")
nuget_str = rf"""<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
Expand Down Expand Up @@ -75,6 +75,6 @@
</Target>
</Project>
"""
(current_dir / "LightGBM.nuspec").write_text(nuget_str, encoding='utf-8')
(current_dir / "build" / "LightGBM.props").write_text(prop_str, encoding='utf-8')
(current_dir / "build" / "LightGBM.targets").write_text(target_str, encoding='utf-8')
(current_dir / "LightGBM.nuspec").write_text(nuget_str, encoding="utf-8")
(current_dir / "build" / "LightGBM.props").write_text(prop_str, encoding="utf-8")
(current_dir / "build" / "LightGBM.targets").write_text(target_str, encoding="utf-8")
25 changes: 25 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
exclude: |
(?x)^(
build|
external_libs|
lightgbm-python|
lightgbm_r|
)$

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.2.1
hooks:
# Run the linter.
- id: ruff
args: ["--config", "python-package/pyproject.toml"]
# Run the formatter.
- id: ruff-format
args: ["--config", "python-package/pyproject.toml"]
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
name: isort (python)
args: ["--settings-path", "python-package/pyproject.toml"]
22 changes: 21 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
LightGBM has been developed and used by many active community members. Your help is very valuable to make it better for everyone.
# contributing

LightGBM has been developed and used by many active community members.

Your help is very valuable to make it better for everyone.

## How to Contribute

- Check for the [Roadmap](https://github.com/microsoft/LightGBM/projects/1) and the [Feature Requests Hub](https://github.com/microsoft/LightGBM/issues/2302), and submit pull requests to address chosen issue. If you need development guideline, you can check the [Development Guide](https://github.com/microsoft/LightGBM/blob/master/docs/Development-Guide.rst) or directly ask us in Issues/Pull Requests.
- Contribute to the [tests](https://github.com/microsoft/LightGBM/tree/master/tests) to make it more reliable.
- Contribute to the [documentation](https://github.com/microsoft/LightGBM/tree/master/docs) to make it clearer for everyone.
- Contribute to the [examples](https://github.com/microsoft/LightGBM/tree/master/examples) to share your experience with other users.
- Add your stories and experience to [Awesome LightGBM](https://github.com/microsoft/LightGBM/blob/master/examples/README.md). If LightGBM helped you in a machine learning competition or some research application, we want to hear about it!
- [Open an issue](https://github.com/microsoft/LightGBM/issues) to report problems or recommend new features.

## Development Guide

### Linting

Every commit in the repository is tested with multiple static analyzers.

When developing locally, run some of them using `pre-commit` ([pre-commit docs](https://pre-commit.com/)).

```shell
pre-commit run --all-files
```

That command will check for some issues and automatically reformat the code.
Loading
Loading