Skip to content

Commit 784ba9e

Browse files
authored
MNT linter bot: tool versions and finished linting check (scikit-learn#26681)
1 parent f1026c0 commit 784ba9e

File tree

3 files changed

+65
-18
lines changed

3 files changed

+65
-18
lines changed

.github/workflows/lint.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,19 @@ jobs:
3434
source build_tools/shared.sh
3535
# Include pytest compatibility with mypy
3636
pip install pytest ruff $(get_dep mypy min) $(get_dep black min) cython-lint
37+
# we save the versions of the linters to be used in the error message later.
38+
python -c "from importlib.metadata import version; print(f\"ruff={version('ruff')}\")" >> /tmp/versions.txt
39+
python -c "from importlib.metadata import version; print(f\"mypy={version('mypy')}\")" >> /tmp/versions.txt
40+
python -c "from importlib.metadata import version; print(f\"black={version('black')}\")" >> /tmp/versions.txt
41+
python -c "from importlib.metadata import version; print(f\"cython-lint={version('cython-lint')}\")" >> /tmp/versions.txt
3742
3843
- name: Run linting
3944
id: lint-script
4045
# We download the linting script from main, since this workflow is run
4146
# from main itself.
4247
run: |
48+
curl https://raw.githubusercontent.com/${{ github.repository }}/main/build_tools/linting.sh --retry 5 -o ./build_tools/linting.sh
4349
set +e
44-
curl https://raw.githubusercontent.com/scikit-learn/scikit-learn/main/build_tools/linting.sh --retry 5 -o ./build_tools/linting.sh
4550
./build_tools/linting.sh &> /tmp/linting_output.txt
4651
cat /tmp/linting_output.txt
4752
@@ -50,7 +55,9 @@ jobs:
5055
uses: actions/upload-artifact@v3
5156
with:
5257
name: lint-log
53-
path: /tmp/linting_output.txt
58+
path: |
59+
/tmp/linting_output.txt
60+
/tmp/versions.txt
5461
retention-days: 1
5562

5663
comment:
@@ -92,4 +99,5 @@ jobs:
9299
BRANCH_SHA: ${{ github.event.pull_request.head.sha }}
93100
RUN_ID: ${{ github.run_id }}
94101
LOG_FILE: linting_output.txt
102+
VERSIONS_FILE: versions.txt
95103
run: python ./build_tools/get_comment.py

build_tools/get_comment.py

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@
77
import requests
88

99

10+
def get_versions(versions_file):
11+
"""Get the versions of the packages used in the linter job.
12+
13+
Parameters
14+
----------
15+
versions_file : str
16+
The path to the file that contains the versions of the packages.
17+
18+
Returns
19+
-------
20+
versions : dict
21+
A dictionary with the versions of the packages.
22+
"""
23+
with open("versions.txt", "r") as f:
24+
return dict(line.strip().split("=") for line in f)
25+
26+
1027
def get_step_message(log, start, end, title, message, details):
1128
"""Get the message for a specific test.
1229
@@ -52,10 +69,29 @@ def get_step_message(log, start, end, title, message, details):
5269
return res
5370

5471

55-
def get_message(log_file, repo, pr_number, sha, run_id, details):
72+
def get_message(log_file, repo, pr_number, sha, run_id, details, versions):
5673
with open(log_file, "r") as f:
5774
log = f.read()
5875

76+
sub_text = (
77+
"\n\n<sub> _Generated for commit:"
78+
f" [{sha[:7]}](https://github.com/{repo}/pull/{pr_number}/commits/{sha}). "
79+
"Link to the linter CI: [here]"
80+
f"(https://github.com/{repo}/actions/runs/{run_id})_ </sub>"
81+
)
82+
83+
if "### Linting completed ###" not in log:
84+
return (
85+
"## ❌ Linting issues\n\n"
86+
"There was an issue running the linter job. Please update with "
87+
"`upstream/main` ([link]("
88+
"https://scikit-learn.org/dev/developers/contributing.html"
89+
"#how-to-contribute)) and push the changes. If you already have done "
90+
"that, please send an empty commit with `git commit --allow-empty` "
91+
"and push the changes to trigger the CI.\n\n"
92+
+ sub_text
93+
)
94+
5995
message = ""
6096

6197
# black
@@ -68,7 +104,8 @@ def get_message(log_file, repo, pr_number, sha, run_id, details):
68104
"`black` detected issues. Please run `black .` locally and push "
69105
"the changes. Here you can see the detected issues. Note that "
70106
"running black might also fix some of the issues which might be "
71-
"detected by `ruff`."
107+
"detected by `ruff`. Note that the installed `black` version is "
108+
f"`black={versions['black']}`."
72109
),
73110
details=details,
74111
)
@@ -82,7 +119,8 @@ def get_message(log_file, repo, pr_number, sha, run_id, details):
82119
message=(
83120
"`ruff` detected issues. Please run `ruff --fix --show-source .` "
84121
"locally, fix the remaining issues, and push the changes. "
85-
"Here you can see the detected issues."
122+
"Here you can see the detected issues. Note that the installed "
123+
f"`ruff` version is `ruff={versions['ruff']}`."
86124
),
87125
details=details,
88126
)
@@ -95,7 +133,8 @@ def get_message(log_file, repo, pr_number, sha, run_id, details):
95133
title="`mypy`",
96134
message=(
97135
"`mypy` detected issues. Please fix them locally and push the changes. "
98-
"Here you can see the detected issues."
136+
"Here you can see the detected issues. Note that the installed `mypy` "
137+
f"version is `mypy={versions['mypy']}`."
99138
),
100139
details=details,
101140
)
@@ -108,7 +147,9 @@ def get_message(log_file, repo, pr_number, sha, run_id, details):
108147
title="`cython-lint`",
109148
message=(
110149
"`cython-lint` detected issues. Please fix them locally and push "
111-
"the changes. Here you can see the detected issues."
150+
"the changes. Here you can see the detected issues. Note that the "
151+
"installed `cython-lint` version is "
152+
f"`cython-lint={versions['cython-lint']}`."
112153
),
113154
details=details,
114155
)
@@ -152,13 +193,6 @@ def get_message(log_file, repo, pr_number, sha, run_id, details):
152193
details=details,
153194
)
154195

155-
sub_text = (
156-
"\n\n<sub> _Generated for commit:"
157-
f" [{sha[:7]}](https://github.com/{repo}/pull/{pr_number}/commits/{sha}). "
158-
"Link to the linter CI: [here]"
159-
f"(https://github.com/{repo}/actions/runs/{run_id})_ </sub>"
160-
)
161-
162196
if not message:
163197
# no issues detected, so this script "fails"
164198
return (
@@ -215,10 +249,8 @@ def find_lint_bot_comments(repo, token, pr_number):
215249
response.raise_for_status()
216250
all_comments = response.json()
217251

218-
failed_comment = "This PR is introducing linting issues. Here's a summary of the"
219-
success_comment = (
220-
"All linting checks passed. Your pull request is in excellent shape"
221-
)
252+
failed_comment = "❌ Linting issues"
253+
success_comment = "✔️ Linting Passed"
222254

223255
# Find all comments that match the linting bot, and return the first one.
224256
# There should always be only one such comment, or none, if the PR is
@@ -269,6 +301,9 @@ def create_or_update_comment(comment, message, repo, pr_number, token):
269301
sha = os.environ["BRANCH_SHA"]
270302
log_file = os.environ["LOG_FILE"]
271303
run_id = os.environ["RUN_ID"]
304+
versions_file = os.environ["VERSIONS_FILE"]
305+
306+
versions = get_versions(versions_file)
272307

273308
if not repo or not token or not pr_number or not log_file or not run_id:
274309
raise ValueError(
@@ -290,6 +325,7 @@ def create_or_update_comment(comment, message, repo, pr_number, token):
290325
sha=sha,
291326
run_id=run_id,
292327
details=True,
328+
versions=versions,
293329
)
294330
create_or_update_comment(
295331
comment=comment,
@@ -309,6 +345,7 @@ def create_or_update_comment(comment, message, repo, pr_number, token):
309345
sha=sha,
310346
run_id=run_id,
311347
details=False,
348+
versions=versions,
312349
)
313350
create_or_update_comment(
314351
comment=comment,

build_tools/linting.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ else
113113
global_status=1
114114
fi
115115

116+
echo -e "### Linting completed ###\n"
117+
116118
if [[ $global_status -eq 1 ]]
117119
then
118120
echo -e "Linting failed\n"

0 commit comments

Comments
 (0)