Skip to content

Commit 623f717

Browse files
committed
Refactor argparse into main()
1 parent 4d9917f commit 623f717

File tree

1 file changed

+43
-41
lines changed

1 file changed

+43
-41
lines changed

needs_backport.py

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939

4040
@cache
41-
def get_pr_commit(pull_number: int) -> str:
41+
def get_pr_commit(api: GhApi, pull_number: int) -> str:
4242
return api.pulls.get(pull_number=pull_number).merge_commit_sha
4343

4444

@@ -63,6 +63,7 @@ def is_commit_title_in_branch(repo_path: str, title: str, branch: str) -> bool:
6363

6464

6565
def check_prs(
66+
api: GhApi,
6667
repo_path: str,
6768
branch_to_check: str,
6869
start: int = 1,
@@ -100,7 +101,7 @@ def check_prs(
100101
if not pr.pull_request.merged_at:
101102
# PR was closed without being merged, skip it
102103
continue
103-
commit = get_pr_commit(pr.number)
104+
commit = get_pr_commit(api, pr.number)
104105
title = get_title_of_commit(repo_path, commit)
105106
print(f" {title}")
106107

@@ -114,13 +115,50 @@ def check_prs(
114115
return candidates
115116

116117

117-
def main(args) -> None:
118+
def main() -> None:
119+
parser = argparse.ArgumentParser(
120+
description=__doc__,
121+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
122+
)
123+
parser.add_argument("repo_path", help="path to CPython repo")
124+
parser.add_argument(
125+
"-b",
126+
"--branches",
127+
default="3.13,3.12,3.11,3.10,3.9",
128+
help="branches to check",
129+
)
130+
parser.add_argument(
131+
"-s", "--start", default=1, type=int, help="start at this PR number"
132+
)
133+
parser.add_argument(
134+
"-n", "--number", default=100, type=int, help="number of PRs to check"
135+
)
136+
parser.add_argument(
137+
"--sort",
138+
default="newest",
139+
choices=(
140+
"newest",
141+
"oldest",
142+
"most-commented",
143+
"least-commented",
144+
"recently-updated",
145+
"least-recently-updated",
146+
),
147+
help="Sort by",
148+
)
149+
parser.add_argument(
150+
"-o", "--open-prs", action="store_true", help="open PRs in browser"
151+
)
152+
args = parser.parse_args()
153+
154+
api = GhApi(owner="python", repo="cpython", token=GITHUB_TOKEN)
155+
118156
# Find
119157
total_candidates = defaultdict(list)
120158
for branch in args.branches.split(","):
121159
print(f"\nChecking branch {branch}")
122160
candidates = check_prs(
123-
args.repo_path, branch, args.start, args.number, args.sort
161+
api, args.repo_path, branch, args.start, args.number, args.sort
124162
)
125163
for reason, prs in candidates.items():
126164
total_candidates[reason].extend(prs)
@@ -154,40 +192,4 @@ def report(prs: list[PR]):
154192

155193

156194
if __name__ == "__main__":
157-
parser = argparse.ArgumentParser(
158-
description=__doc__,
159-
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
160-
)
161-
parser.add_argument("repo_path", help="path to CPython repo")
162-
parser.add_argument(
163-
"-b",
164-
"--branches",
165-
default="3.13,3.12,3.11,3.10,3.9",
166-
help="branches to check",
167-
)
168-
parser.add_argument(
169-
"-s", "--start", default=1, type=int, help="start at this PR number"
170-
)
171-
parser.add_argument(
172-
"-n", "--number", default=100, type=int, help="number of PRs to check"
173-
)
174-
parser.add_argument(
175-
"--sort",
176-
default="newest",
177-
choices=(
178-
"newest",
179-
"oldest",
180-
"most-commented",
181-
"least-commented",
182-
"recently-updated",
183-
"least-recently-updated",
184-
),
185-
help="Sort by",
186-
)
187-
parser.add_argument(
188-
"-o", "--open-prs", action="store_true", help="open PRs in browser"
189-
)
190-
args = parser.parse_args()
191-
192-
api = GhApi(owner="python", repo="cpython", token=GITHUB_TOKEN)
193-
main(args)
195+
main()

0 commit comments

Comments
 (0)