-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathneeds_backport.py
More file actions
231 lines (193 loc) · 6.62 KB
/
needs_backport.py
File metadata and controls
231 lines (193 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
"""
Go through each merged PR with a "needs backport to 3.x" label and check
if there is a commit on the corresponding branch that refers to that PR.
Some PRs have those labels but:
* the backport was manually done and the label is still there, or
* the backport is missing and was forgotten about, or
* the backport was deliberately held up
"""
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "ghapi",
# "rich",
# "stamina",
# ]
# ///
from __future__ import annotations
import argparse
import json
import os
import subprocess
import urllib.request
from collections import defaultdict
from functools import cache
from typing import Any, TypeAlias
from fastcore.xtras import obj2dict
from ghapi.all import GhApi, paged # pip install ghapi
from rich import print # pip install rich
from potential_closeable_issues import save_json, sort_by_to_sort_and_direction
PR: TypeAlias = dict[str, Any]
GITHUB_TOKEN = os.environ["GITHUB_TOOLS_TOKEN"]
@cache
def fetch_active_branches() -> str:
url = "https://peps.python.org/api/release-cycle.json"
with urllib.request.urlopen(url) as response:
data = json.loads(response.read().decode("utf-8"))
active_versions = []
for version, info in data.items():
status = info.get("status", "")
if status in ("bugfix", "security"):
active_versions.append(version)
return ",".join(active_versions)
@cache
def get_pr_commit(api: GhApi, pull_number: int) -> str:
return api.pulls.get(pull_number=pull_number).merge_commit_sha
@cache
def get_title_of_commit(repo_path: str, commit_sha: str) -> str:
return (
subprocess.check_output(
["git", "log", "-1", "--pretty=%B", commit_sha], cwd=repo_path
)
.decode("utf-8")
.split("\n")[0]
)
@cache
def is_commit_title_in_branch(repo_path: str, title: str, branch: str) -> bool:
output = subprocess.check_output(
["git", "log", "--grep", title, "--fixed-strings", branch], cwd=repo_path
).decode("utf-8")
return output != ""
def check_prs(
api: GhApi,
repo_path: str,
branch_to_check: str,
start: int = 1,
number: int = 100,
sort_by: str = "newest",
) -> dict[str, list[PR]]:
sort, direction = sort_by_to_sort_and_direction(sort_by)
candidates = defaultdict(list) # reason => PRs
pr_count = 0
for page in paged(
# The PR API doesn't filter by label.
# PR are really issues, so use the issues API.
api.issues.list_for_repo,
state="closed",
sort=sort,
direction=direction,
per_page=100,
labels=f"needs backport to {branch_to_check}",
):
for pr in page:
pr_count += 1
print(pr_count, start, number, pr.html_url)
if pr_count < start:
continue
if pr_count >= start + number:
return candidates
if "/issues/" in pr.html_url:
print(" [red]Issue with backport labels[/red]")
candidates["issues with backport labels"].append(pr)
continue
if not pr.pull_request.merged_at:
# PR was closed without being merged, skip it
continue
commit = get_pr_commit(api, pr.number)
title = get_title_of_commit(repo_path, commit)
print(f" {title}")
if is_commit_title_in_branch(repo_path, title, branch_to_check):
print(" [red]Backport found, remove label?[/red]")
candidates["PRs with backports"].append(pr)
else:
print(f" [yellow]Backport to {branch_to_check} missing[/yellow]")
candidates["PRs missing backports"].append(pr)
return candidates
def main() -> None:
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("repo_path", help="path to CPython repo")
parser.add_argument(
"-b",
"--branches",
default=fetch_active_branches(),
help="branches to check",
)
parser.add_argument(
"-s", "--start", default=1, type=int, help="start at this PR number"
)
parser.add_argument(
"-n",
"--number",
default=100,
type=int,
help="number of PRs to check per branch",
)
parser.add_argument(
"--sort",
default="newest",
choices=(
"newest",
"oldest",
"most-commented",
"least-commented",
"recently-updated",
"least-recently-updated",
),
help="Sort by",
)
parser.add_argument("-j", "--json", action="store_true", help="output to JSON file")
parser.add_argument(
"-o", "--open-prs", action="store_true", help="open PRs in browser"
)
args = parser.parse_args()
api = GhApi(owner="python", repo="cpython", token=GITHUB_TOKEN)
# Find
total_candidates = defaultdict(list)
for branch in args.branches.split(","):
print(f"\nChecking branch {branch}")
output = subprocess.check_output(
["git", "fetch", "origin", f"{branch}:{branch}"], cwd=args.repo_path
).decode("utf-8")
print(output)
candidates = check_prs(
api, args.repo_path, branch, args.start, args.number, args.sort
)
for reason, prs in candidates.items():
# Remove duplicates
prs = [pr for pr in prs if pr not in total_candidates[reason]]
total_candidates[reason].extend(prs)
# Report
def report(prs: list[PR]):
if prs:
cmd = "open "
for pr in prs:
print(f'\\[#{pr["number"]}]({pr["html_url"]}) {pr["title"]}')
cmd += f"{pr['html_url']} "
print()
print(cmd)
if args.open_prs:
os.system(cmd)
print("\n[bold]Results[/bold]")
for reason, prs in total_candidates.items():
total = len({pr["number"] for pr in prs})
print(f"\nFound {total} {reason}")
report(prs)
print("\n[bold]Summary[/bold]\n")
for reason, prs in total_candidates.items():
total = len({pr["number"] for pr in prs})
print(f"* Found {total} {reason}")
if args.json:
data = {
"reasons": [
{reason: [obj2dict(pr) for pr in prs]}
for reason, prs in total_candidates.items()
],
}
# Use same name as this .py but with .json
filename = os.path.splitext(__file__)[0] + ".json"
save_json(data, filename)
if __name__ == "__main__":
main()