From d8b5e697c08c22617ef3efdcb403afb35651f59f Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Tue, 19 Oct 2021 16:12:58 +0530 Subject: [PATCH] Add support to run check news from workflow --- .github/workflows/changelog.yml | 27 ++++++---- scripts/news/check_news_workflow/__main__.py | 56 ++++++++++++-------- 2 files changed, 50 insertions(+), 33 deletions(-) diff --git a/.github/workflows/changelog.yml b/.github/workflows/changelog.yml index f13b65d4..99a16aa9 100644 --- a/.github/workflows/changelog.yml +++ b/.github/workflows/changelog.yml @@ -1,6 +1,3 @@ -# Github Action Workflow enforcing our changelog style of enforcing PR number. -# credit: https://github.com/psf/black/blob/main/.github/workflows/changelog.yml - name: Changelog Entry Check on: @@ -8,16 +5,24 @@ on: types: [opened, synchronize, labeled, unlabeled, reopened] jobs: - build: - name: Changelog Entry Check + check-news: runs-on: ubuntu-latest steps: + - name: Checkout repository + uses: actions/checkout@v2 + - uses: actions/checkout@v2 - - name: Grep Release Notes (docs-en) for PR number - if: contains(github.event.pull_request.labels.*.name, 'skip changelog') != true + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.9' + + - name: Install dependencies using poetry + if: steps.python_cache.outputs.cache-hit != 'true' + run: | + pip install tomli requests + + - name: Changelog Entry Check for the current PR run: | - grep -Pz "\((\n\s*)?#${{ github.event.pull_request.number }}(\n\s*)?\)" docs/changelog.md || \ - ( - echo "Please add '(#${{ github.event.pull_request.number }})' change line to docs/changelog.md" && exit 1 - ) + python -m scripts.news.check_news_workflow ${{ github.event.pull_request.number }} diff --git a/scripts/news/check_news_workflow/__main__.py b/scripts/news/check_news_workflow/__main__.py index e1de1243..6931f9db 100644 --- a/scripts/news/check_news_workflow/__main__.py +++ b/scripts/news/check_news_workflow/__main__.py @@ -1,18 +1,35 @@ -import enum import pathlib import re +import sys +import traceback +from typing import Tuple -import click import requests - -from ..utils import load_toml_config - +import tomli NEWS_NEXT_DIR = "news/next/" SKIP_NEWS_LABEL = "skip changelog" GH_API_URL = "https://api.github.com/" HEADERS = {"accept": "application/vnd.github.v3+json"} + +def load_toml_config() -> dict: + config_path = pathlib.Path(pathlib.Path.cwd(), "scripts/news/config.toml") + + try: + with open(config_path, mode="r") as file: + toml_dict = tomli.loads(file.read()) + except tomli.TOMLDecodeError as e: + message = "Invalid changelog news configuration at {}\n{}".format( + config_path, + "".join(traceback.format_exception_only(type(e), e)), + ) + print(message) + sys.exit(1) + else: + return toml_dict + + CONFIG = load_toml_config() SECTIONS = [_type for _type, _ in CONFIG.get("types").items()] @@ -21,32 +38,21 @@ 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""" + 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: +def main(pr: int) -> Tuple[str, bool]: """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"]): @@ -57,7 +63,7 @@ def main(pr: int) -> None: 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) + status = (f"News entry found in {NEWS_NEXT_DIR}", True) break else: _r = requests.get(f"{GH_API_URL}repos/discord-modmail/modmail/pulls/{pr}", headers=HEADERS) @@ -73,10 +79,16 @@ def main(pr: int) -> None: else: description = "News entry file name incorrectly formatted" - status = (description, StatusState.ERROR) + status = (description, False) - print(status) + return status if __name__ == "__main__": - main() + message, status = main(int(sys.argv[1])) + if not status: + print(f"::set-output name={message}::{message}") + sys.exit(1) + else: + print(f"::set-output name={message}::{message}") + sys.exit(0)