15
15
from __future__ import annotations
16
16
17
17
import argparse
18
+ import dataclasses
18
19
import logging
19
20
import pathlib
20
21
import re
@@ -29,17 +30,30 @@ def main() -> None:
29
30
args = parse_args ()
30
31
logging .basicConfig (format = "%(message)s" , level = logging .INFO )
31
32
32
- github_issue = calculate_github_issue (
33
+ calculated_result = calculate_github_issue (
33
34
github_event_name = args .github_event_name ,
34
35
github_issue_for_scheduled_run = args .github_issue_for_scheduled_run ,
35
36
github_ref = args .github_ref ,
36
37
github_repository = args .github_repository ,
37
38
pr_body_github_issue_key = args .pr_body_github_issue_key ,
38
39
)
39
40
40
- file_text = "" if github_issue is None else str (github_issue )
41
- logging .info ("Writing '%s' to %s" , file_text , args .issue_output_file )
42
- args .issue_output_file .write_text (file_text , encoding = "utf8" , errors = "replace" )
41
+ github_issue = calculated_result .issue
42
+ github_pr = calculated_result .pr
43
+
44
+ issue_file_text = "" if github_issue is None else str (github_issue )
45
+ logging .info ("Writing '%s' to %s" , issue_file_text , args .issue_output_file )
46
+ args .issue_output_file .write_text (issue_file_text , encoding = "utf8" , errors = "replace" )
47
+
48
+ pr_file_text = "" if github_pr is None else str (github_pr )
49
+ logging .info ("Writing '%s' to %s" , pr_file_text , args .pr_output_file )
50
+ args .pr_output_file .write_text (pr_file_text , encoding = "utf8" , errors = "replace" )
51
+
52
+
53
+ @dataclasses .dataclass (frozen = True )
54
+ class CalculatedGitHubIssue :
55
+ issue : int | None
56
+ pr : int | None
43
57
44
58
45
59
def calculate_github_issue (
@@ -48,21 +62,24 @@ def calculate_github_issue(
48
62
github_ref : str ,
49
63
github_repository : str ,
50
64
pr_body_github_issue_key : str ,
51
- ) -> int | None :
65
+ ) -> CalculatedGitHubIssue :
52
66
if github_event_name == "schedule" :
53
67
logging .info (
54
68
"GitHub Event name is: %s; using GitHub Issue: %s" ,
55
69
github_event_name ,
56
70
github_issue_for_scheduled_run ,
57
71
)
58
- return github_issue_for_scheduled_run
72
+ return CalculatedGitHubIssue (
73
+ issue = github_issue_for_scheduled_run ,
74
+ pr = None , # scheduled runs are, by definition, not associated with a PR.
75
+ )
59
76
60
77
logging .info ("Extracting PR number from string: %s" , github_ref )
61
- pr_number : int | None = pr_number_from_github_ref (github_ref )
62
-
78
+ pr_number = pr_number_from_github_ref (github_ref )
63
79
if pr_number is None :
64
80
logging .info ("No PR number extracted" )
65
- return None
81
+ return CalculatedGitHubIssue (None , None )
82
+ typing .assert_type (pr_number , int )
66
83
67
84
logging .info ("PR number extracted: %s" , pr_number )
68
85
logging .info ("Loading body text of PR: %s" , pr_number )
@@ -79,10 +96,11 @@ def calculate_github_issue(
79
96
80
97
if github_issue is None :
81
98
logging .info ("No GitHub Issue key found in PR body" )
82
- return None
99
+ return CalculatedGitHubIssue (issue = None , pr = pr_number )
100
+ typing .assert_type (github_issue , int )
83
101
84
102
logging .info ("Found GitHub Issue key in PR body: %s" , github_issue )
85
- return github_issue
103
+ return CalculatedGitHubIssue ( issue = github_issue , pr = pr_number )
86
104
87
105
88
106
def pr_number_from_github_ref (github_ref : str ) -> int | None :
0 commit comments