|
| 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() |
0 commit comments