|
| 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