1515from __future__ import annotations
1616
1717import argparse
18- import dataclasses
1918import logging
2019import pathlib
2120import re
22- import subprocess
2321import typing
2422
25- if typing .TYPE_CHECKING :
26- from collections .abc import Iterable
23+ from util import fetch_pr_info , pr_number_from_github_ref
2724
2825
2926def main () -> None :
3027 args = parse_args ()
3128 logging .basicConfig (format = "%(message)s" , level = logging .INFO )
3229
33- calculated_result = calculate_github_issue (
30+ github_issue = calculate_github_issue (
3431 github_event_name = args .github_event_name ,
3532 github_issue_for_scheduled_run = args .github_issue_for_scheduled_run ,
3633 github_ref = args .github_ref ,
3734 github_repository = args .github_repository ,
3835 pr_body_github_issue_key = args .pr_body_github_issue_key ,
3936 )
4037
41- github_issue = calculated_result .issue
42- github_pr = calculated_result .pr
43-
4438 issue_file_text = "" if github_issue is None else str (github_issue )
4539 logging .info ("Writing '%s' to %s" , issue_file_text , args .issue_output_file )
4640 args .issue_output_file .write_text (issue_file_text , encoding = "utf8" , errors = "replace" )
4741
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
57-
5842
5943def calculate_github_issue (
6044 github_event_name : str ,
6145 github_issue_for_scheduled_run : int ,
6246 github_ref : str ,
6347 github_repository : str ,
6448 pr_body_github_issue_key : str ,
65- ) -> CalculatedGitHubIssue :
49+ ) -> int | None :
6650 if github_event_name == "schedule" :
6751 logging .info (
6852 "GitHub Event name is: %s; using GitHub Issue: %s" ,
6953 github_event_name ,
7054 github_issue_for_scheduled_run ,
7155 )
72- return CalculatedGitHubIssue (
73- issue = github_issue_for_scheduled_run ,
74- pr = None , # scheduled runs are, by definition, not associated with a PR.
75- )
56+ return github_issue_for_scheduled_run
7657
7758 logging .info ("Extracting PR number from string: %s" , github_ref )
7859 pr_number = pr_number_from_github_ref (github_ref )
7960 if pr_number is None :
8061 logging .info ("No PR number extracted" )
81- return CalculatedGitHubIssue ( None , None )
62+ return None
8263 typing .assert_type (pr_number , int )
8364
8465 logging .info ("PR number extracted: %s" , pr_number )
8566 logging .info ("Loading body text of PR: %s" , pr_number )
86- pr_body_text = load_pr_body (
67+ pr_info = fetch_pr_info (
8768 pr_number = pr_number ,
8869 github_repository = github_repository ,
8970 )
9071
9172 logging .info ("Looking for GitHub Issue key in PR body text: %s=NNNN" , pr_body_github_issue_key )
9273 github_issue = github_issue_from_pr_body (
93- pr_body = pr_body_text ,
74+ pr_body = pr_info . body ,
9475 issue_key = pr_body_github_issue_key ,
9576 )
9677
9778 if github_issue is None :
9879 logging .info ("No GitHub Issue key found in PR body" )
99- return CalculatedGitHubIssue ( issue = None , pr = pr_number )
80+ return None
10081 typing .assert_type (github_issue , int )
10182
10283 logging .info ("Found GitHub Issue key in PR body: %s" , github_issue )
103- return CalculatedGitHubIssue (issue = github_issue , pr = pr_number )
104-
105-
106- def pr_number_from_github_ref (github_ref : str ) -> int | None :
107- match = re .fullmatch ("refs/pull/([0-9]+)/merge" , github_ref )
108- return int (match .group (1 )) if match else None
109-
110-
111- def load_pr_body (pr_number : int , github_repository : str ) -> str :
112- gh_args = log_pr_body_gh_args (pr_number = pr_number , github_repository = github_repository )
113- gh_args = tuple (gh_args )
114- logging .info ("Running command: %s" , subprocess .list2cmdline (gh_args ))
115- return subprocess .check_output (gh_args , encoding = "utf8" , errors = "replace" ) # noqa: S603
116-
117-
118- def log_pr_body_gh_args (pr_number : int , github_repository : str ) -> Iterable [str ]:
119- yield "gh"
120- yield "issue"
121- yield "view"
122- yield str (pr_number )
123- yield "--json"
124- yield "body"
125- yield "--jq"
126- yield ".body"
127- yield "-R"
128- yield github_repository
84+ return github_issue
12985
13086
13187def github_issue_from_pr_body (pr_body : str , issue_key : str ) -> int | None :
@@ -139,7 +95,6 @@ def github_issue_from_pr_body(pr_body: str, issue_key: str) -> int | None:
13995
14096class ParsedArgs (typing .Protocol ):
14197 issue_output_file : pathlib .Path
142- pr_output_file : pathlib .Path
14398 github_ref : str
14499 github_repository : str
145100 github_event_name : str
@@ -155,12 +110,6 @@ def parse_args() -> ParsedArgs:
155110 help = "The file to which to write the calculated issue number"
156111 "if no issue number was found, then an empty file will be written" ,
157112 )
158- arg_parser .add_argument (
159- "--pr-output-file" ,
160- required = True ,
161- help = "The file to which to write the calculated triggering PR number"
162- "if no PR was found, then an empty file will be written" ,
163- )
164113 arg_parser .add_argument (
165114 "--github-ref" ,
166115 required = True ,
@@ -192,7 +141,6 @@ def parse_args() -> ParsedArgs:
192141
193142 parse_result = arg_parser .parse_args ()
194143 parse_result .issue_output_file = pathlib .Path (parse_result .issue_output_file )
195- parse_result .pr_output_file = pathlib .Path (parse_result .pr_output_file )
196144 return typing .cast ("ParsedArgs" , parse_result )
197145
198146
0 commit comments