Skip to content

Commit

Permalink
WIP: Add module for checking news file in a specific pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivansh-007 committed Oct 19, 2021
1 parent aa8bc6f commit b6c19d4
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 2 deletions.
137 changes: 135 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ coloredlogs = "^15.0"
"discord.py" = { url = "https://github.com/Rapptz/discord.py/archive/master.zip" }
pydantic = { version = "^1.8.2", extras = ["dotenv"] }
toml = "^0.10.2"
rich = "^10.12.0"


[tool.poetry.extras]
Expand Down Expand Up @@ -58,6 +59,7 @@ mkdocs-material = ">=7.1.9,<8.0.0"
mkdocs-markdownextradata-plugin = ">=0.1.7,<0.2.0"
click = "^8.0.3"
Jinja2 = "^3.0.2"
gidgethub = "^5.0.1"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ cffi==1.14.6
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"
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"
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"
commonmark==0.9.1
discord.py @ https://github.com/Rapptz/discord.py/archive/master.zip ; python_full_version >= "3.8.0"
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"
idna==3.2 ; python_version >= "3.5"
multidict==5.1.0 ; python_version >= "3.6"
pycares==4.0.0
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"
pydantic==1.8.2 ; python_full_version >= "3.6.1"
pygments==2.10.0 ; python_version >= "3.5"
pyreadline==2.1 ; sys_platform == "win32"
python-dateutil==2.8.2 ; python_version != "3.0"
python-dotenv==0.19.0 ; python_version >= "3.5"
rich==10.12.0 ; python_full_version >= "3.6.2"
six==1.16.0 ; python_version >= "2.7" and python_version != "3.0" and python_version != "3.1" and python_version != "3.2"
toml==0.10.2 ; python_version >= "2.6" and python_version != "3.0" and python_version != "3.1" and python_version != "3.2"
typing-extensions==3.10.0.2
Expand Down
Binary file modified scripts/news/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file modified scripts/news/__pycache__/__main__.cpython-39.pyc
Binary file not shown.
Binary file modified scripts/news/__pycache__/utils.cpython-39.pyc
Binary file not shown.
Empty file.
83 changes: 83 additions & 0 deletions scripts/news/check_news_workflow/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import enum
import pathlib
import re

import click
import requests
from rich import print

from ..utils import load_toml_config


NEWS_NEXT_DIR = "news/next/"
SKIP_NEWS_LABEL = "skip changelog"
GH_API_URL = "https://api.github.com/"
HEADERS = {"accept": "application/vnd.github.v3+json"}

CONFIG = load_toml_config()
SECTIONS = [_type for _type, _ in CONFIG.get("types").items()]

FILENAME_RE = re.compile(
r"\d{4}-\d{2}-\d{2}(?:-\d{2}-\d{2}-\d{2})?\." # match `yyyy-mm-dd` or `yyyy-m-d`
r"pr-\d+(?:,\d+)*\." # Issue number(s)
fr"({'|'.join(SECTIONS)})\." # Section type
r"[A-Za-z0-9_=-]+\." # Nonce (URL-safe base64)
r"md", # File extension"""
re.VERBOSE,
)


class StatusState(enum.Enum):
"""Status state for the changelog checking."""

SUCCESS = "success"
ERROR = "error"
FAILURE = "failure"


def is_news_dir(filename: str) -> bool:
"""Return True if file is in the News directory."""
return filename.startswith(NEWS_NEXT_DIR)


@click.command()
@click.argument("pr", nargs=1, type=int)
def main(pr: int) -> None:
"""Main function to check for a changelog entry."""
r = requests.get(f"{GH_API_URL}repos/discord-modmail/modmail/pulls/{pr}/files", headers=HEADERS)
files_changed = r.json()
in_next_dir = file_found = False
status = None

for file in files_changed:
if not is_news_dir(file["filename"]):
continue
in_next_dir = True
file_path = pathlib.PurePath(file["filename"])
if len(file_path.parts) != 4: # news, next, <type>, <entry>
continue
file_found = True
if FILENAME_RE.match(file_path.name) and len(file["patch"]) >= 1:
status = (f"News entry found in {NEWS_NEXT_DIR}", StatusState.SUCCESS)
break
else:
_r = requests.get(f"{GH_API_URL}repos/discord-modmail/modmail/pulls/{pr}", headers=HEADERS)
pr_data = _r.json()
labels = [label["name"] for label in pr_data["labels"]]
if SKIP_NEWS_LABEL in labels:
description = f"'{SKIP_NEWS_LABEL}' label found"
else:
if not in_next_dir:
description = f'No news entry in {NEWS_NEXT_DIR} or "{SKIP_NEWS_LABEL}" label found'
elif not file_found:
description = "News entry not in an appropriate directory"
else:
description = "News entry file name incorrectly formatted"

status = (description, StatusState.ERROR)

print(status)


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions scripts/news/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ bugfix = { name = "Bug Fixes" }
doc = { name = "Improved Documentation" }
deprecation = { name = "Deprecations" }
breaking = { name = "Breaking Changes" }
internal = { name = "Internal" }

0 comments on commit b6c19d4

Please sign in to comment.