This repository was archived by the owner on May 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathnss-land-commit.py
More file actions
executable file
·298 lines (242 loc) · 9.52 KB
/
nss-land-commit.py
File metadata and controls
executable file
·298 lines (242 loc) · 9.52 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env python3
import io, os
import bugzilla
import json
import hglib
from colorama import init, Fore
from optparse import OptionParser
from pathlib import Path
from whaaaaat import prompt
from utils.types import Patch, PackageVersion, Validator
def info(message):
print(Fore.GREEN + message)
def log(message):
print(message)
def get_version(hgclient, *, rev=None, validator) -> PackageVersion:
if Path("lib/nss/nss.h").exists():
contents = hgclient.cat([b"lib/nss/nss.h"], rev=rev).decode(encoding="UTF-8")
return PackageVersion.from_header(
component="NSS", header=contents, validator=validator
)
elif Path("pr/include/prinit.h").exists():
contents = hgclient.cat([b"pr/include/prinit.h"], rev=rev).decode(
encoding="UTF-8"
)
return PackageVersion.from_header(
component="NSPR", header=contents, validator=validator
)
raise Exception("No version files found")
def bug_status_check(*, bugdata, patch, validator: Validator):
if patch.type == "patch":
if bugdata.status not in ["NEW", "ASSIGNED", "REOPENED"]:
validator.warn(
f"Bug {bugdata.id} is in an odd state for a patch: {bugdata.status}"
)
elif patch.type == "backout":
if bugdata.status not in ["RESOLVED"]:
validator.warn(
f"Bug {bugdata.id} is in an odd state for a backout: {bugdata.status}"
)
else:
validator.fatal("Unknown patch type: " + patch.type)
def resolve(*, hgclient, bzapi, patch: Patch, validator: Validator):
repo = hgclient.paths(name=b"default").decode(encoding="UTF-8").split("@")[1]
bugdata = bzapi.getbug(patch.bug)
bug_status_check(bugdata=bugdata, patch=patch, validator=validator)
comment = f"https://{repo}rev/{patch.hash.decode(encoding='UTF-8')}\n"
if patch.type == "backout":
comment = f"Backed out for {patch.reason}\n{comment}"
elif hgclient.outgoing(revrange=patch.hash):
validator.fatal(f"Patch {patch} doesn't appear to have landed.")
version = get_version(hgclient, rev=patch.hash, validator=validator)
info(f"Commit {patch} is against {version.component} {version.number}")
info(f"Adding comment to bug {patch.bug} - {bugdata.summary} [{bugdata.status}]:")
if patch.type == "patch":
log(comment)
update = None
if "leave-open" in bugdata.keywords:
answers = prompt(
[
{
"type": "confirm",
"message": "Submit this comment and leave the bug open?",
"name": "leaveopen",
}
]
)
if answers["leaveopen"]:
update = bzapi.build_update(
comment=comment, target_milestone=version.number
)
else:
answers = prompt(
[
{
"type": "confirm",
"message": "Submit this comment and resolve the bug?",
"name": "resolve",
}
]
)
if answers["resolve"]:
update = bzapi.build_update(
comment=comment,
status="RESOLVED",
resolution="FIXED",
target_milestone=version.number,
)
# breakpoint()
if update is not None:
bzapi.update_bugs([patch.bug], update)
info(f"Updated {bugdata.weburl}")
elif patch.type == "backout":
log(comment)
answers = prompt(
[
{
"type": "confirm",
"message": "Submit this comment and reopen the bug?",
"name": "resolve",
}
]
)
if answers["resolve"]:
update = bzapi.build_update(
comment=comment,
status="REOPENED",
resolution="---",
target_milestone="---",
)
# breakpoint()
bzapi.update_bugs([patch.bug], update)
info(f"Reopened {bugdata.weburl}")
else:
validator.fatal(f"Unknown patch type: {patch.type}")
def process_patches(
*, hgclient, bzapi, revrange: str, patches: list, validator: Validator
):
bug = None
version = get_version(hgclient, rev=revrange, validator=validator)
info(f"Patchset {revrange} is against {version.component} {version.number}")
if len(patches) != 1:
raise Exception("One at a time right now")
for patch in patches:
if patch.type == "tag":
continue
if bug is None:
bug = patch.bug
elif bug != patch.bug:
validator.fatal(f"Multiple bugs in one revrange: {bug}, {patch.bug}")
bugdata = bzapi.getbug(patch.bug)
info(bugdata.__str__())
log(f"Component: {bugdata.component}")
log(bugdata.weburl)
log(bugdata.status)
log(bugdata.type)
log(f"Version: {bugdata.version}")
log(f"Target: {bugdata.target_milestone}")
if (
bugdata.component != version.component
and bugdata.product != version.component
):
validator.fatal(
f"Bug component mismatch. Bug is for {bugdata.product}::{bugdata.component}, but we're in {version.component}"
)
if bugdata.target_milestone != version.number:
validator.warn(
f"Bug target milestone ({bugdata.target_milestone}) is not set to {version.number}"
)
bug_status_check(bugdata=bugdata, patch=patch, validator=validator)
answers = prompt(
[
{
"type": "confirm",
"message": "Ready to Push patch (will be manual)?",
"name": "push",
}
]
)
if answers["push"]:
log("Now run:")
info(f" hg push -r {revrange}")
if prompt(
[
{
"type": "confirm",
"message": "Was your push successful?",
"name": "push",
}
]
)["push"]:
resolve(
hgclient=hgclient, bzapi=bzapi, patch=patch, validator=validator
)
def main():
init(autoreset=True)
parser = OptionParser()
parser.add_option("-b", "--bug", help="bug number to search, used with -l")
parser.add_option("-l", "--landed", help="as-landed hg revision, used with -b")
parser.add_option("-e", "--revrange", default=".", help="hg revision range")
parser.add_option("-r", "--resolve", help="resolve bugs for a given revision range")
(options, args) = parser.parse_args()
hgclient = hglib.open(".")
config = {}
confFile = Path.home() / ".nss-land-commit.json"
if confFile.exists():
with open(confFile, "r") as conf:
config = json.load(conf)
if "api_key" not in config:
print(
Fore.YELLOW
+ "Note: Not logging into Bugzilla. BZ actions won't work. Make a file at ~/.nss-land-commit.json"
)
print(Fore.YELLOW + "with contents like:")
log(json.dumps({"api_key": "random_api_key_1e87d00d1c2fb"}))
bzapi = bugzilla.Bugzilla("bugzilla.mozilla.org")
else:
bzapi = bugzilla.Bugzilla("bugzilla.mozilla.org", api_key=config["api_key"])
validator = Validator()
info(f"Interacting with Bugzilla at {bzapi.url}. Logged in = {bzapi.logged_in}")
try:
if options.bug and options.landed:
commits = hgclient.log(revrange=options.landed)
if len(commits) != 1:
validator.fatal(f"Couldn't find revision {options.landed}")
patch = Patch(commit=commits[0], validator=validator)
patch.validate(validator=validator)
resolve(hgclient=hgclient, bzapi=bzapi, patch=patch, validator=validator)
elif options.bug or options.landed:
validator.fatal("You have to specify --bug and --landed together")
elif options.resolve:
commits = hgclient.log(revrange=options.resolve)
if not commits:
validator.fatal("No changes found")
if len(commits) != 1:
raise Exception("Only one at a time now")
for commit in commits:
patch = Patch(commit=commit, validator=validator)
patch.validate(validator=validator)
if patch.type == "patch":
resolve(
hgclient=hgclient, bzapi=bzapi, patch=patch, validator=validator
)
else:
commits = hgclient.outgoing(revrange=options.revrange)
if not commits:
validator.fatal("No changes found")
patches = []
for commit in commits:
patch = Patch(commit=commit, validator=validator)
patch.validate(validator=validator)
patches.append(patch)
process_patches(
hgclient=hgclient,
bzapi=bzapi,
revrange=options.revrange,
patches=patches,
validator=validator,
)
except hglib.error.CommandError as ce:
validator.fatal(f"Mercurial error {ce.err.decode(encoding='UTF-8')}")
if __name__ == "__main__":
main()