Skip to content

Commit

Permalink
Add support to run check news from workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivansh-007 committed Oct 19, 2021
1 parent 87ffb04 commit 6b2d5ef
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 33 deletions.
27 changes: 16 additions & 11 deletions .github/workflows/changelog.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
# 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:
pull_request:
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 }}
52 changes: 30 additions & 22 deletions scripts/news/check_news_workflow/__main__.py
Original file line number Diff line number Diff line change
@@ -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()]

Expand All @@ -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"]):
Expand All @@ -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)
Expand All @@ -73,10 +79,12 @@ 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]))
print(message)
sys.exit(0 if status else 1)

0 comments on commit 6b2d5ef

Please sign in to comment.