1
- import enum
2
1
import pathlib
3
2
import re
3
+ import sys
4
+ import traceback
5
+ from typing import Tuple
4
6
5
- import click
6
7
import requests
7
-
8
- from ..utils import load_toml_config
9
-
8
+ import tomli
10
9
11
10
NEWS_NEXT_DIR = "news/next/"
12
11
SKIP_NEWS_LABEL = "skip changelog"
13
12
GH_API_URL = "https://api.github.com/"
14
13
HEADERS = {"accept" : "application/vnd.github.v3+json" }
15
14
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
+
16
33
CONFIG = load_toml_config ()
17
34
SECTIONS = [_type for _type , _ in CONFIG .get ("types" ).items ()]
18
35
21
38
r"pr-\d+(?:,\d+)*\." # Issue number(s)
22
39
fr"({ '|' .join (SECTIONS )} )\." # Section type
23
40
r"[A-Za-z0-9_=-]+\." # Nonce (URL-safe base64)
24
- r"md" , # File extension"""
41
+ r"md" , # File extension
25
42
re .VERBOSE ,
26
43
)
27
44
28
45
29
- class StatusState (enum .Enum ):
30
- """Status state for the changelog checking."""
31
-
32
- SUCCESS = "success"
33
- ERROR = "error"
34
- FAILURE = "failure"
35
-
36
-
37
46
def is_news_dir (filename : str ) -> bool :
38
47
"""Return True if file is in the News directory."""
39
48
return filename .startswith (NEWS_NEXT_DIR )
40
49
41
50
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 ]:
45
52
"""Main function to check for a changelog entry."""
46
53
r = requests .get (f"{ GH_API_URL } repos/discord-modmail/modmail/pulls/{ pr } /files" , headers = HEADERS )
47
54
files_changed = r .json ()
48
55
in_next_dir = file_found = False
49
- status = None
50
56
51
57
for file in files_changed :
52
58
if not is_news_dir (file ["filename" ]):
@@ -57,7 +63,7 @@ def main(pr: int) -> None:
57
63
continue
58
64
file_found = True
59
65
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 )
61
67
break
62
68
else :
63
69
_r = requests .get (f"{ GH_API_URL } repos/discord-modmail/modmail/pulls/{ pr } " , headers = HEADERS )
@@ -73,10 +79,16 @@ def main(pr: int) -> None:
73
79
else :
74
80
description = "News entry file name incorrectly formatted"
75
81
76
- status = (description , StatusState . ERROR )
82
+ status = (description , False )
77
83
78
- print ( status )
84
+ return status
79
85
80
86
81
87
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