1- import enum
21import pathlib
32import re
3+ import sys
4+ import traceback
5+ from typing import Tuple
46
5- import click
67import requests
7-
8- from ..utils import load_toml_config
9-
8+ import tomli
109
1110NEWS_NEXT_DIR = "news/next/"
1211SKIP_NEWS_LABEL = "skip changelog"
1312GH_API_URL = "https://api.github.com/"
1413HEADERS = {"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+
1633CONFIG = load_toml_config ()
1734SECTIONS = [_type for _type , _ in CONFIG .get ("types" ).items ()]
1835
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-
3746def 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
8187if __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