Skip to content

MNT Linter Bot: add concurrency group and a link to the commit for th… #26638

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

Merged
merged 6 commits into from
Jun 21, 2023
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
11 changes: 10 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ name: linter
on:
- pull_request_target

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
lint:
runs-on: ubuntu-latest
Expand All @@ -32,7 +36,11 @@ jobs:
pip install pytest ruff $(get_dep mypy min) $(get_dep black min) cython-lint

- name: Run linting
run: ./build_tools/linting.sh &> /tmp/linting_output.txt
# We download the linting script from main, since this workflow is run
# from main itself.
run: |
curl https://raw.githubusercontent.com/scikit-learn/scikit-learn/main/build_tools/linting.sh -o ./build_tools/linting.sh
./build_tools/linting.sh &> /tmp/linting_output.txt

- name: Upload Artifact
if: always()
Expand Down Expand Up @@ -78,6 +86,7 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
BRANCH_SHA: ${{ github.event.pull_request.head.sha }}
RUN_ID: ${{ github.run_id }}
LOG_FILE: linting_output.txt
run: python ./build_tools/get_comment.py
32 changes: 27 additions & 5 deletions build_tools/get_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def get_step_message(log, start, end, title, message, details):
return res


def get_message(log_file, repo, run_id, details):
def get_message(log_file, repo, pr_number, sha, run_id, details):
with open(log_file, "r") as f:
log = f.read()

Expand Down Expand Up @@ -152,15 +152,21 @@ def get_message(log_file, repo, run_id, details):
details=details,
)

commit_link = (
"\n\n_Generated for commit:"
f" [{sha[:7]}](https://github.com/{repo}/pull/{pr_number}/commits/{sha})_"
)

if not message:
# no issues detected, so this script "fails"
return (
"## Linting Passed\n"
"## ✔️ Linting Passed\n"
"All linting checks passed. Your pull request is in excellent shape! ☀️"
+ commit_link
)

message = (
"## Linting issues\n\n"
"## Linting issues\n\n"
"This PR is introducing linting issues. Here's a summary of the issues. "
"Note that you can avoid having linting issues by enabling `pre-commit` "
"hooks. Instructions to enable them can be found [here]("
Expand All @@ -169,6 +175,7 @@ def get_message(log_file, repo, run_id, details):
"You can see the details of the linting issues under the `lint` job [here]"
f"(https://github.com/{repo}/actions/runs/{run_id})\n\n"
+ message
+ commit_link
)

return message
Expand Down Expand Up @@ -245,6 +252,7 @@ def create_or_update_comment(comment, message, repo, pr_number, token):
repo = os.environ["GITHUB_REPOSITORY"]
token = os.environ["GITHUB_TOKEN"]
pr_number = os.environ["PR_NUMBER"]
sha = os.environ["BRANCH_SHA"]
log_file = os.environ["LOG_FILE"]
run_id = os.environ["RUN_ID"]

Expand All @@ -261,7 +269,14 @@ def create_or_update_comment(comment, message, repo, pr_number, token):
exit(0)

try:
message = get_message(log_file, repo=repo, run_id=run_id, details=True)
message = get_message(
log_file,
repo=repo,
pr_number=pr_number,
sha=sha,
run_id=run_id,
details=True,
)
create_or_update_comment(
comment=comment,
message=message,
Expand All @@ -273,7 +288,14 @@ def create_or_update_comment(comment, message, repo, pr_number, token):
except requests.HTTPError:
# The above fails if the message is too long. In that case, we
# try again without the details.
message = get_message(log_file, repo=repo, run_id=run_id, details=False)
message = get_message(
log_file,
repo=repo,
pr_number=pr_number,
sha=sha,
run_id=run_id,
details=False,
)
create_or_update_comment(
comment=comment,
message=message,
Expand Down