-
Notifications
You must be signed in to change notification settings - Fork 2
/
backport.py
275 lines (231 loc) · 8.51 KB
/
backport.py
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"""
Put this file in a master checkout under .github/.
It should be next to backport-languages.py.
This assumes your git "origin" points to your fork, and "upstream" to upstream.
This will force-push to a branch called "release-backport".
The GITHUB_TOKEN is a classic token you need to generate that has public_repo
scope enabled. This token is used to push the backported PRs to GitHub.
Execute with:
$ export GITHUB_TOKEN=ghp_XXX
$ export GITHUB_USERNAME=XXX
$ python3 .github/backport.py
And follow the instructions. After the PR is merged, run:
$ python3 .github/backport.py --mark-done <PR-NUMBER>
"""
import json
import os
import subprocess
import sys
BEARER_TOKEN = os.getenv("GITHUB_TOKEN")
USERNAME = os.getenv("GITHUB_USERNAME")
# NOTE: Replace with the version branch to backport to
RELEASE = "13"
pr_query = """
query ($number: Int!) {
repository(owner: "OpenTTD", name: "OpenTTD") {
pullRequest(number: $number) {
body
}
}
}
"""
pr_search_query = """
query ($search: String!) {
search(query: $search, type: ISSUE, first: 100) {
issueCount
edges {
node {
... on PullRequest {
number
title
commits(first: 100) {
totalCount
nodes {
commit {
messageHeadline
}
}
}
mergedAt
mergeCommit {
oid
}
labels(first: 10) {
nodes {
name
}
}
}
}
}
}
}
"""
def do_query(query, variables):
query = query.replace("\n", "").replace("\\", "\\\\").replace('"', '\\"')
variables = json.dumps(variables).replace("\\", "\\\\").replace('"', '\\"')
res = subprocess.run(
[
"curl",
"--fail",
"-H",
f"Authorization: bearer {BEARER_TOKEN}",
"-X",
"POST",
"-d",
f'{{"query": "{query}", "variables": "{variables}"}}',
"https://api.github.com/graphql",
],
capture_output=True,
)
if res.returncode != 0:
return None
return json.loads(res.stdout)
def do_remove_label(number):
return subprocess.run(
[
"curl",
"--fail",
"-H",
f"Authorization: bearer {BEARER_TOKEN}",
"-X",
"DELETE",
f"https://api.github.com/repos/OpenTTD/OpenTTD/issues/{number}/labels/backport%20requested",
],
capture_output=True,
)
def do_add_label(number):
return subprocess.run(
[
"curl",
"--fail",
"-H",
f"Authorization: bearer {BEARER_TOKEN}",
"-X",
"POST",
"-d",
'{"labels": ["backported"]}',
f"https://api.github.com/repos/OpenTTD/OpenTTD/issues/{number}/labels",
],
capture_output=True,
)
def do_command(command):
return subprocess.run(command, capture_output=True)
def main():
if len(sys.argv) > 1 and sys.argv[1] == "--mark-done":
backport_pr = do_query(pr_query, {"number": int(sys.argv[2])})
if backport_pr is None:
print("ERROR: couldn't fetch backport PR")
return
for line in backport_pr["data"]["repository"]["pullRequest"]["body"].split("\n"):
if line.startswith("<!-- Backported: "):
prs = [int(pr) for pr in line.split(":")[1].split(" ")[1].split(",")]
print("Update labels from backported PRs")
for pr in prs:
print(f"- #{pr} ..")
res = do_remove_label(pr)
if res.returncode != 0:
print(f"ERROR: failed to remove label from {pr}")
res = do_add_label(pr)
if res.returncode != 0:
print(f"ERROR: failed to add label to {pr}")
print("All done")
return
dont_push = False
if len(sys.argv) > 1 and sys.argv[1] == "--dont-push":
dont_push = True
resume = None
resume_i = None
if os.path.exists(".backport-resume"):
with open(".backport-resume", "r") as fp:
resume_str, _, resume_i_str = fp.read().partition(",")
resume = int(resume_str)
resume_i = int(resume_i_str)
print(f"Resuming backporting from {resume}")
all_prs = do_query(pr_search_query, {"search": 'is:closed is:pr label:"backport requested" repo:OpenTTD/OpenTTD'})
if all_prs is None:
print("ERROR: couldn't fetch all Pull Requests marked for 'backport requested'")
return
if not resume:
do_command(["git", "fetch", "upstream"])
do_command(["git", "checkout", f"upstream/release/{RELEASE}", "-B", "release-backport"])
for pr in sorted(all_prs["data"]["search"]["edges"], key=lambda x: x["node"]["mergedAt"]):
if resume:
if resume != pr["node"]["number"]:
continue
resume = None
print(f"Merging #{pr['node']['number']}: {pr['node']['title']} (resuming)")
else:
print(f"Merging #{pr['node']['number']}: {pr['node']['title']}")
# In case of multiple commits, check if it was squashed or rebased.
# We do this by comparing commit titles. As if you rebased, they have
# to be identical.
if pr["node"]["commits"]["totalCount"] > 1:
for i in range(pr["node"]["commits"]["totalCount"]):
commit = pr["node"]["commits"]["totalCount"] - i - 1
commit_str = f'{pr["node"]["mergeCommit"]["oid"]}' + "".join(["^"] * commit)
res = do_command(["git", "log", "--pretty=format:%s", f"{commit_str}^..{commit_str}"])
if res.stdout.decode() != pr["node"]["commits"]["nodes"][i]["commit"]["messageHeadline"]:
print(" -> was squashed")
pr["node"]["commits"]["totalCount"] = 1
break
for i in range(pr["node"]["commits"]["totalCount"]):
if resume_i is not None:
if resume_i != i:
continue
resume_i = None
continue
commit = pr["node"]["commits"]["totalCount"] - i - 1
commit_str = f'{pr["node"]["mergeCommit"]["oid"]}' + "".join(["^"] * commit)
print(f" Commit #{i}: {commit_str} ...")
res = do_command(["git", "cherry-pick", commit_str])
if res.returncode != 0:
with open(".backport-resume", "w") as fp:
fp.write(str(pr["node"]["number"]) + "," + str(i))
print(res.stdout.decode())
print("")
print("Cherry-pick failed: please fix the issue manually and run script again.")
return
if os.path.exists(".backport-resume"):
os.unlink(".backport-resume")
print("")
print("Done cherry-picking")
print("Backporting language changes")
res = do_command(
[sys.executable or "python3", os.path.dirname(os.path.realpath(__file__)) + "/backport-languages.py"]
)
if res.returncode != 0:
print("ERROR: backporting language changes failed")
return
do_command(["git", "add", "src/lang/*.txt"])
do_command(["git", "commit", "-m", "Update: Backport language changes"])
print("Done backporting language changes")
print("")
print("Your commit message:")
print("")
marker = []
print("## Description")
print(f"Backport of all closed Pull Requests labeled as 'backport requested' into `release/{RELEASE}`.")
for pr in sorted(all_prs["data"]["search"]["edges"], key=lambda x: x["node"]["mergedAt"]):
print(f"- https://github.com/OpenTTD/OpenTTD/pull/{pr['node']['number']}")
marker.append(str(pr["node"]["number"]))
print("- All language changes")
print(f"<!-- Backported: {','.join(marker)} -->")
print("")
if dont_push:
print("Done with backport; you can now push this branch to remote:")
print(" git push -f --set-upstream origin release-backport")
print("After that, go to this URL:")
else:
res = do_command(["git", "push", "-f", "--set-upstream", "origin", "release-backport"])
if res.returncode != 0:
print("ERROR: failed to push to remote")
else:
print("Pushed to remote")
print("You can create the PR here:")
print(
f"https://github.com/OpenTTD/OpenTTD/compare/release/{RELEASE}...{USERNAME}"
f":release-backport?expand=1&title=Backport%20master%20into%20release%2f{RELEASE}"
)
if __name__ == "__main__":
main()