Skip to content

Commit b6c19d4

Browse files
committed
WIP: Add module for checking news file in a specific pull request
1 parent aa8bc6f commit b6c19d4

File tree

9 files changed

+224
-2
lines changed

9 files changed

+224
-2
lines changed

poetry.lock

Lines changed: 135 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ coloredlogs = "^15.0"
2121
"discord.py" = { url = "https://github.com/Rapptz/discord.py/archive/master.zip" }
2222
pydantic = { version = "^1.8.2", extras = ["dotenv"] }
2323
toml = "^0.10.2"
24+
rich = "^10.12.0"
2425

2526

2627
[tool.poetry.extras]
@@ -58,6 +59,7 @@ mkdocs-material = ">=7.1.9,<8.0.0"
5859
mkdocs-markdownextradata-plugin = ">=0.1.7,<0.2.0"
5960
click = "^8.0.3"
6061
Jinja2 = "^3.0.2"
62+
gidgethub = "^5.0.1"
6163

6264
[build-system]
6365
requires = ["poetry-core>=1.0.0"]

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@ cffi==1.14.6
1212
chardet==4.0.0 ; python_version >= "2.7" and python_version != "3.0" and python_version != "3.1" and python_version != "3.2" and python_version != "3.3" and python_version != "3.4"
1313
colorama==0.4.4 ; python_version >= "2.7" and python_version != "3.0" and python_version != "3.1" and python_version != "3.2" and python_version != "3.3" and python_version != "3.4"
1414
coloredlogs==15.0.1 ; python_version >= "2.7" and python_version != "3.0" and python_version != "3.1" and python_version != "3.2" and python_version != "3.3" and python_version != "3.4"
15+
commonmark==0.9.1
1516
discord.py @ https://github.com/Rapptz/discord.py/archive/master.zip ; python_full_version >= "3.8.0"
1617
humanfriendly==9.2 ; python_version >= "2.7" and python_version != "3.0" and python_version != "3.1" and python_version != "3.2" and python_version != "3.3" and python_version != "3.4"
1718
idna==3.2 ; python_version >= "3.5"
1819
multidict==5.1.0 ; python_version >= "3.6"
1920
pycares==4.0.0
2021
pycparser==2.20 ; python_version >= "2.7" and python_version != "3.0" and python_version != "3.1" and python_version != "3.2" and python_version != "3.3"
2122
pydantic==1.8.2 ; python_full_version >= "3.6.1"
23+
pygments==2.10.0 ; python_version >= "3.5"
2224
pyreadline==2.1 ; sys_platform == "win32"
2325
python-dateutil==2.8.2 ; python_version != "3.0"
2426
python-dotenv==0.19.0 ; python_version >= "3.5"
27+
rich==10.12.0 ; python_full_version >= "3.6.2"
2528
six==1.16.0 ; python_version >= "2.7" and python_version != "3.0" and python_version != "3.1" and python_version != "3.2"
2629
toml==0.10.2 ; python_version >= "2.6" and python_version != "3.0" and python_version != "3.1" and python_version != "3.2"
2730
typing-extensions==3.10.0.2
0 Bytes
Binary file not shown.
375 Bytes
Binary file not shown.
-205 Bytes
Binary file not shown.

scripts/news/check_news_workflow/__init__.py

Whitespace-only changes.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import enum
2+
import pathlib
3+
import re
4+
5+
import click
6+
import requests
7+
from rich import print
8+
9+
from ..utils import load_toml_config
10+
11+
12+
NEWS_NEXT_DIR = "news/next/"
13+
SKIP_NEWS_LABEL = "skip changelog"
14+
GH_API_URL = "https://api.github.com/"
15+
HEADERS = {"accept": "application/vnd.github.v3+json"}
16+
17+
CONFIG = load_toml_config()
18+
SECTIONS = [_type for _type, _ in CONFIG.get("types").items()]
19+
20+
FILENAME_RE = re.compile(
21+
r"\d{4}-\d{2}-\d{2}(?:-\d{2}-\d{2}-\d{2})?\." # match `yyyy-mm-dd` or `yyyy-m-d`
22+
r"pr-\d+(?:,\d+)*\." # Issue number(s)
23+
fr"({'|'.join(SECTIONS)})\." # Section type
24+
r"[A-Za-z0-9_=-]+\." # Nonce (URL-safe base64)
25+
r"md", # File extension"""
26+
re.VERBOSE,
27+
)
28+
29+
30+
class StatusState(enum.Enum):
31+
"""Status state for the changelog checking."""
32+
33+
SUCCESS = "success"
34+
ERROR = "error"
35+
FAILURE = "failure"
36+
37+
38+
def is_news_dir(filename: str) -> bool:
39+
"""Return True if file is in the News directory."""
40+
return filename.startswith(NEWS_NEXT_DIR)
41+
42+
43+
@click.command()
44+
@click.argument("pr", nargs=1, type=int)
45+
def main(pr: int) -> None:
46+
"""Main function to check for a changelog entry."""
47+
r = requests.get(f"{GH_API_URL}repos/discord-modmail/modmail/pulls/{pr}/files", headers=HEADERS)
48+
files_changed = r.json()
49+
in_next_dir = file_found = False
50+
status = None
51+
52+
for file in files_changed:
53+
if not is_news_dir(file["filename"]):
54+
continue
55+
in_next_dir = True
56+
file_path = pathlib.PurePath(file["filename"])
57+
if len(file_path.parts) != 4: # news, next, <type>, <entry>
58+
continue
59+
file_found = True
60+
if FILENAME_RE.match(file_path.name) and len(file["patch"]) >= 1:
61+
status = (f"News entry found in {NEWS_NEXT_DIR}", StatusState.SUCCESS)
62+
break
63+
else:
64+
_r = requests.get(f"{GH_API_URL}repos/discord-modmail/modmail/pulls/{pr}", headers=HEADERS)
65+
pr_data = _r.json()
66+
labels = [label["name"] for label in pr_data["labels"]]
67+
if SKIP_NEWS_LABEL in labels:
68+
description = f"'{SKIP_NEWS_LABEL}' label found"
69+
else:
70+
if not in_next_dir:
71+
description = f'No news entry in {NEWS_NEXT_DIR} or "{SKIP_NEWS_LABEL}" label found'
72+
elif not file_found:
73+
description = "News entry not in an appropriate directory"
74+
else:
75+
description = "News entry file name incorrectly formatted"
76+
77+
status = (description, StatusState.ERROR)
78+
79+
print(status)
80+
81+
82+
if __name__ == "__main__":
83+
main()

scripts/news/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ bugfix = { name = "Bug Fixes" }
99
doc = { name = "Improved Documentation" }
1010
deprecation = { name = "Deprecations" }
1111
breaking = { name = "Breaking Changes" }
12+
internal = { name = "Internal" }

0 commit comments

Comments
 (0)