Skip to content

Commit eda68f0

Browse files
committed
Add support to run check news from workflow
1 parent 87ffb04 commit eda68f0

File tree

2 files changed

+51
-33
lines changed

2 files changed

+51
-33
lines changed

.github/workflows/changelog.yml

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
# Github Action Workflow enforcing our changelog style of enforcing PR number.
2-
# credit: https://github.com/psf/black/blob/main/.github/workflows/changelog.yml
3-
41
name: Changelog Entry Check
52

63
on:
74
pull_request:
85
types: [opened, synchronize, labeled, unlabeled, reopened]
96

107
jobs:
11-
build:
12-
name: Changelog Entry Check
8+
check-news:
139
runs-on: ubuntu-latest
1410

1511
steps:
12+
- name: Checkout repository
13+
uses: actions/checkout@v2
14+
1615
- uses: actions/checkout@v2
17-
- name: Grep Release Notes (docs-en) for PR number
18-
if: contains(github.event.pull_request.labels.*.name, 'skip changelog') != true
16+
- name: Set up Python
17+
uses: actions/setup-python@v2
18+
with:
19+
python-version: '3.9'
20+
21+
- name: Install dependencies using poetry
22+
if: steps.python_cache.outputs.cache-hit != 'true'
23+
run: |
24+
pip install tomli requests
25+
26+
- name: Changelog Entry Check for the current PR
1927
run: |
20-
grep -Pz "\((\n\s*)?#${{ github.event.pull_request.number }}(\n\s*)?\)" docs/changelog.md || \
21-
(
22-
echo "Please add '(#${{ github.event.pull_request.number }})' change line to docs/changelog.md" && exit 1
23-
)
28+
echo "::set-output name=action_fruit::strawberry"
29+
python -m scripts.news.check_news_workflow ${{ github.event.pull_request.number }}
Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
1-
import enum
21
import pathlib
32
import re
3+
import sys
4+
import traceback
5+
from typing import Tuple
46

5-
import click
67
import requests
7-
8-
from ..utils import load_toml_config
9-
8+
import tomli
109

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

15+
16+
def load_toml_config() -> dict:
17+
config_path = pathlib.Path(pathlib.Path.cwd(), "scripts/news/config.toml")
18+
19+
try:
20+
with open(config_path, mode="r") as file:
21+
toml_dict = tomli.loads(file.read())
22+
except tomli.TOMLDecodeError as e:
23+
message = "Invalid changelog news configuration at {}\n{}".format(
24+
config_path,
25+
"".join(traceback.format_exception_only(type(e), e)),
26+
)
27+
print(message)
28+
sys.exit(1)
29+
else:
30+
return toml_dict
31+
32+
1633
CONFIG = load_toml_config()
1734
SECTIONS = [_type for _type, _ in CONFIG.get("types").items()]
1835

@@ -21,32 +38,21 @@
2138
r"pr-\d+(?:,\d+)*\." # Issue number(s)
2239
fr"({'|'.join(SECTIONS)})\." # Section type
2340
r"[A-Za-z0-9_=-]+\." # Nonce (URL-safe base64)
24-
r"md", # File extension"""
41+
r"md", # File extension
2542
re.VERBOSE,
2643
)
2744

2845

29-
class StatusState(enum.Enum):
30-
"""Status state for the changelog checking."""
31-
32-
SUCCESS = "success"
33-
ERROR = "error"
34-
FAILURE = "failure"
35-
36-
3746
def is_news_dir(filename: str) -> bool:
3847
"""Return True if file is in the News directory."""
3948
return filename.startswith(NEWS_NEXT_DIR)
4049

4150

42-
@click.command()
43-
@click.argument("pr", nargs=1, type=int)
44-
def main(pr: int) -> None:
51+
def main(pr: int) -> Tuple[str, bool]:
4552
"""Main function to check for a changelog entry."""
4653
r = requests.get(f"{GH_API_URL}repos/discord-modmail/modmail/pulls/{pr}/files", headers=HEADERS)
4754
files_changed = r.json()
4855
in_next_dir = file_found = False
49-
status = None
5056

5157
for file in files_changed:
5258
if not is_news_dir(file["filename"]):
@@ -57,7 +63,7 @@ def main(pr: int) -> None:
5763
continue
5864
file_found = True
5965
if FILENAME_RE.match(file_path.name) and len(file["patch"]) >= 1:
60-
status = (f"News entry found in {NEWS_NEXT_DIR}", StatusState.SUCCESS)
66+
status = (f"News entry found in {NEWS_NEXT_DIR}", True)
6167
break
6268
else:
6369
_r = requests.get(f"{GH_API_URL}repos/discord-modmail/modmail/pulls/{pr}", headers=HEADERS)
@@ -73,10 +79,16 @@ def main(pr: int) -> None:
7379
else:
7480
description = "News entry file name incorrectly formatted"
7581

76-
status = (description, StatusState.ERROR)
82+
status = (description, False)
7783

78-
print(status)
84+
return status
7985

8086

8187
if __name__ == "__main__":
82-
main()
88+
message, status = main(int(sys.argv[1]))
89+
if not status:
90+
print(f"::set-output name={message}::{message}")
91+
sys.exit(1)
92+
else:
93+
print(f"::set-output name={message}::{message}")
94+
sys.exit(0)

0 commit comments

Comments
 (0)