Skip to content

Commit 6e2d44c

Browse files
authored
Merge commit from fork
* Split bot lint comment in two * Use curl to download script after artifact has been downloaded * Use actions/checkout with sparse checkout and clean-up variable usage * runner.temp is not defined at job level somehow ... * fix * Better error when PR_NUMBER is not a number * Control PR number from workflow_run than leaving it to the user * remove debug * tweak * Remove unneeded pr_number.txt artifact
1 parent 810b920 commit 6e2d44c

File tree

3 files changed

+84
-53
lines changed

3 files changed

+84
-53
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
name: Bot linter comment
2+
# We need these permissions to be able to post / update comments
3+
permissions:
4+
pull-requests: write
5+
issues: write
6+
7+
on:
8+
workflow_run:
9+
workflows: ["Linter"]
10+
types:
11+
- completed
12+
13+
jobs:
14+
bot-comment:
15+
runs-on: ubuntu-latest
16+
if: ${{ github.event.workflow_run.conclusion != 'cancelled' }}
17+
steps:
18+
- name: Define ARTIFACTS_DIR environment variable
19+
run: |
20+
echo "ARTIFACTS_DIR=${{ runner.temp }}/artifacts" >> "$GITHUB_ENV"
21+
22+
- name: Create temporary artifacts directory
23+
run: mkdir -p "$ARTIFACTS_DIR"
24+
25+
- name: Download artifact
26+
uses: actions/download-artifact@v4
27+
with:
28+
name: lint-log
29+
path: ${{ runner.temp }}/artifacts
30+
github-token: ${{ secrets.GITHUB_TOKEN }}
31+
run-id: ${{ github.event.workflow_run.id }}
32+
33+
# Adapted from https://github.com/docker-mailserver/docker-mailserver/pull/4267#issuecomment-2484565209
34+
# Unfortunately there is no easier way to do it
35+
- name: Get PR number from triggering workflow information
36+
env:
37+
GH_TOKEN: ${{ github.token }}
38+
PR_TARGET_REPO: ${{ github.repository }}
39+
PR_BRANCH: |-
40+
${{
41+
(github.event.workflow_run.head_repository.owner.login != github.event.workflow_run.repository.owner.login)
42+
&& format('{0}:{1}', github.event.workflow_run.head_repository.owner.login, github.event.workflow_run.head_branch)
43+
|| github.event.workflow_run.head_branch
44+
}}
45+
run: |
46+
gh pr view --repo "${PR_TARGET_REPO}" "${PR_BRANCH}" \
47+
--json 'number' \
48+
--jq '"PR_NUMBER=\(.number)"' \
49+
>> $GITHUB_ENV
50+
51+
- uses: actions/checkout@v4
52+
with:
53+
sparse-checkout: build_tools/get_comment.py
54+
55+
- name: Set up Python
56+
uses: actions/setup-python@v5
57+
with:
58+
python-version: 3.11
59+
60+
- name: Install dependencies
61+
run: python -m pip install requests
62+
63+
- name: Create/update GitHub comment
64+
env:
65+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
66+
BRANCH_SHA: ${{ github.event.workflow_run.head_sha }}
67+
RUN_ID: ${{ github.event.workflow_run.id }}
68+
run: |
69+
set -e
70+
export LOG_FILE="$ARTIFACTS_DIR/linting_output.txt"
71+
export VERSIONS_FILE="$ARTIFACTS_DIR/versions.txt"
72+
73+
python ./build_tools/get_comment.py

.github/workflows/lint.yml

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
# This linter job on GH actions is used to trigger the commenter bot
2-
# in bot-lint-comment.yml file. It stores the output of the linter to be used
3-
# by the commenter bot.
4-
name: linter
1+
# This workflow is used to trigger the commenter bot in bot-lint-comment.yml
2+
# file. It stores the output of the linter to be used by the commenter bot.
3+
name: Linter
4+
permissions:
5+
contents: read
56

67
on:
7-
- pull_request_target
8+
- pull_request
89

910
concurrency:
1011
group: ${{ github.workflow }}-${{ github.head_ref }}
@@ -31,7 +32,6 @@ jobs:
3132

3233
- name: Install dependencies
3334
run: |
34-
curl https://raw.githubusercontent.com/${{ github.repository }}/main/build_tools/shared.sh --retry 5 -o ./build_tools/shared.sh
3535
source build_tools/shared.sh
3636
# Include pytest compatibility with mypy
3737
pip install pytest $(get_dep ruff min) $(get_dep mypy min) cython-lint
@@ -41,11 +41,7 @@ jobs:
4141
python -c "from importlib.metadata import version; print(f\"cython-lint={version('cython-lint')}\")" >> /tmp/versions.txt
4242
4343
- name: Run linting
44-
id: lint-script
45-
# We download the linting script from main, since this workflow is run
46-
# from main itself.
4744
run: |
48-
curl https://raw.githubusercontent.com/${{ github.repository }}/main/build_tools/linting.sh --retry 5 -o ./build_tools/linting.sh
4945
set +e
5046
./build_tools/linting.sh &> /tmp/linting_output.txt
5147
cat /tmp/linting_output.txt
@@ -59,45 +55,3 @@ jobs:
5955
/tmp/linting_output.txt
6056
/tmp/versions.txt
6157
retention-days: 1
62-
63-
comment:
64-
needs: lint
65-
if: ${{ !cancelled() }}
66-
runs-on: ubuntu-latest
67-
68-
# We need these permissions to be able to post / update comments
69-
permissions:
70-
pull-requests: write
71-
issues: write
72-
73-
steps:
74-
- name: Checkout code
75-
uses: actions/checkout@v4
76-
77-
- name: Set up Python
78-
uses: actions/setup-python@v5
79-
with:
80-
python-version: 3.11
81-
82-
- name: Install dependencies
83-
run: python -m pip install requests
84-
85-
- name: Download artifact
86-
id: download-artifact
87-
uses: actions/download-artifact@v4
88-
with:
89-
name: lint-log
90-
91-
- name: Print log
92-
run: cat linting_output.txt
93-
94-
- name: Process Comments
95-
id: process-comments
96-
env:
97-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
98-
PR_NUMBER: ${{ github.event.pull_request.number }}
99-
BRANCH_SHA: ${{ github.event.pull_request.head.sha }}
100-
RUN_ID: ${{ github.run_id }}
101-
LOG_FILE: linting_output.txt
102-
VERSIONS_FILE: versions.txt
103-
run: python ./build_tools/get_comment.py

build_tools/get_comment.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# This script fails if there are not comments to be posted.
44

55
import os
6+
import re
67

78
import requests
89

@@ -20,7 +21,7 @@ def get_versions(versions_file):
2021
versions : dict
2122
A dictionary with the versions of the packages.
2223
"""
23-
with open("versions.txt", "r") as f:
24+
with open(versions_file, "r") as f:
2425
return dict(line.strip().split("=") for line in f)
2526

2627

@@ -305,6 +306,9 @@ def create_or_update_comment(comment, message, repo, pr_number, token):
305306
"GITHUB_REPOSITORY, GITHUB_TOKEN, PR_NUMBER, LOG_FILE, RUN_ID"
306307
)
307308

309+
if not re.match(r"\d+$", pr_number):
310+
raise ValueError(f"PR_NUMBER should be a number, got {pr_number!r} instead")
311+
308312
try:
309313
comment = find_lint_bot_comments(repo, token, pr_number)
310314
except RuntimeError:

0 commit comments

Comments
 (0)