Skip to content

Commit d56c6e5

Browse files
authored
[402] adds checks to the pull request (#403)
* chanegs * mistake * mistake * mistake * changes * doc
1 parent a117434 commit d56c6e5

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

.github/workflows/ci.yml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,24 @@ jobs:
2626
- name: Run ruff (black)
2727
# Do not attempt to install the default dependencies, this is much faster.
2828
# Run temporarily on a sub directory before the main restyling.
29-
run: uv run --no-project --with "ruff==0.9.7" ruff format --target-version py312 --check -n src/
29+
run: uv run --no-project --with "ruff==0.9.7" ruff format --target-version py312 --check -n src/ scripts/
3030

3131
- name: Run ruff (flake8)
3232
# Do not attempt to install the default dependencies, this is much faster.
33-
run: uv run --no-project --with "ruff==0.9.7" ruff check src/
33+
run: uv run --no-project --with "ruff==0.9.7" ruff check src/
34+
pr:
35+
name: PR checks
36+
runs-on: ubuntu-latest
37+
38+
steps:
39+
- uses: actions/checkout@v4
40+
41+
- name: Install uv
42+
uses: astral-sh/setup-uv@v5
43+
with:
44+
# Install a specific version of uv.
45+
version: "0.7.13"
46+
47+
- name: Check PR is linked to an issue
48+
# Send the PR number to the script, which will check if it is linked to an issue.
49+
run: scripts/check_gh_issue.py $GITHUB_REF_NAME

scripts/check_gh_issue.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env -S uv run
2+
# /// script
3+
# dependencies = [ "BeautifulSoup4", "requests"
4+
# ]
5+
# [tool.uv]
6+
# exclude-newer = "2025-01-01T00:00:00Z"
7+
# ///
8+
9+
"""
10+
Checks that a pull request has a corresponding GitHub issue.
11+
12+
Source:
13+
https://stackoverflow.com/questions/60717142/getting-linked-issues-and-projects-associated-with-a-pull-request-form-github-ap
14+
"""
15+
16+
import requests
17+
from bs4 import BeautifulSoup
18+
import re
19+
20+
repo = "ecmwf/WeatherGenerator"
21+
22+
msg_template = """This pull request {pr} does not have a linked issue.
23+
Please link it to an issue in the repository {repo} before merging.
24+
The easiest way to do this is to add a comment with the issue number, like this:
25+
Fixes #1234
26+
This will automatically link the issue to the pull request.
27+
28+
If you just want to reference an issue without closing it, you can use:
29+
Refs #1234
30+
31+
See https://docs.github.com/en/issues/tracking-your-work-with-issues/using-issues/linking-a-pull-request-to-an-issue
32+
"""
33+
34+
35+
if __name__ == "__main__":
36+
import argparse
37+
38+
parser = argparse.ArgumentParser(description="Check GitHub PR for linked issues.")
39+
parser.add_argument("pr", type=str, help="Pull request number")
40+
args = parser.parse_args()
41+
42+
pr: str = args.pr
43+
pr = pr.split("/")[0]
44+
r = requests.get(f"https://github.com/{repo}/pull/{pr}")
45+
soup = BeautifulSoup(r.text, "html.parser")
46+
issueForm = soup.find_all("form", {"aria-label": re.compile("Link issues")})
47+
msg = msg_template.format(pr=pr, repo=repo)
48+
49+
if not issueForm:
50+
print(msg)
51+
exit(1)
52+
issues = [i["href"] for i in issueForm[0].find_all("a")]
53+
issues = [i for i in issues if i is not None and repo in i]
54+
print(f"Linked issues for PR {pr}:")
55+
print(f"Found {len(issues)} linked issues.")
56+
print("\n".join(issues))
57+
if not issues:
58+
print(msg)
59+
exit(1)

0 commit comments

Comments
 (0)