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
|
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, # You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import absolute_import, unicode_literals
import os
import re
import subprocess
import sys
import logging
from mach.decorators import (
CommandArgument,
CommandProvider,
Command,
)
from mozbuild.base import MachCommandBase
import mozpack.path as mozpath
import json
GITHUB_ROOT = "https://github.com/"
PR_REPOSITORIES = {
"webrender": {
"github": "servo/webrender",
"path": "gfx/wr",
"bugzilla_product": "Core",
"bugzilla_component": "Graphics: WebRender",
},
"webgpu": {
"github": "gfx-rs/wgpu",
"path": "gfx/wgpu",
"bugzilla_product": "Core",
"bugzilla_component": "Graphics: WebGPU",
},
"debugger": {
"github": "firefox-devtools/debugger",
"path": "devtools/client/debugger",
"bugzilla_product": "DevTools",
"bugzilla_component": "Debugger",
},
}
@CommandProvider
class PullRequestImporter(MachCommandBase):
@Command(
"import-pr",
category="misc",
description="Import a pull request from Github to the local repo.",
)
@CommandArgument(
"-b", "--bug-number", help="Bug number to use in the commit messages."
)
@CommandArgument(
"-t",
"--bugzilla-token",
help="Bugzilla API token used to file a new bug if no bug number is "
"provided.",
)
@CommandArgument(
"-r", "--reviewer", help="Reviewer nick to apply to commit messages."
)
@CommandArgument(
"pull_request",
help="URL to the pull request to import (e.g. "
"https://github.com/servo/webrender/pull/3665).",
)
def import_pr(
self, pull_request, bug_number=None, bugzilla_token=None, reviewer=None
):
import requests
pr_number = None
repository = None
for r in PR_REPOSITORIES.values():
if pull_request.startswith(GITHUB_ROOT + r["github"] + "/pull/"):
# sanitize URL, dropping anything after the PR number
pr_number = int(re.search("/pull/([0-9]+)", pull_request).group(1))
pull_request = GITHUB_ROOT + r["github"] + "/pull/" + str(pr_number)
repository = r
break
if repository is None:
self.log(
logging.ERROR,
"unrecognized_repo",
{},
"The pull request URL was not recognized; add it to the list of "
"recognized repos in PR_REPOSITORIES in %s" % __file__,
)
sys.exit(1)
self.log(
logging.INFO,
"import_pr",
{"pr_url": pull_request},
"Attempting to import {pr_url}",
)
dirty = [
f
for f in self.repository.get_changed_files(mode="all")
if f.startswith(repository["path"])
]
if dirty:
self.log(
logging.ERROR,
"dirty_tree",
repository,
"Local {path} tree is dirty; aborting!",
)
sys.exit(1)
target_dir = mozpath.join(self.topsrcdir, os.path.normpath(repository["path"]))
if bug_number is None:
if bugzilla_token is None:
self.log(
logging.WARNING,
"no_token",
{},
"No bug number or bugzilla API token provided; bug number will not "
"be added to commit messages.",
)
else:
bug_number = self._file_bug(bugzilla_token, repository, pr_number)
elif bugzilla_token is not None:
self.log(
logging.WARNING,
"too_much_bug",
{},
"Providing a bugzilla token is unnecessary when a bug number is provided. "
"Using bug number; ignoring token.",
)
pr_patch = requests.get(pull_request + ".patch")
pr_patch.raise_for_status()
for patch in self._split_patches(
pr_patch.content, bug_number, pull_request, reviewer
):
self.log(
logging.INFO,
"commit_msg",
patch,
"Processing commit [{commit_summary}] by [{author}] at [{date}]",
)
patch_cmd = subprocess.Popen(
["patch", "-p1", "-s"], stdin=subprocess.PIPE, cwd=target_dir
)
patch_cmd.stdin.write(patch["diff"].encode("utf-8"))
patch_cmd.stdin.close()
patch_cmd.wait()
if patch_cmd.returncode != 0:
self.log(
logging.ERROR,
"commit_fail",
{},
'Error applying diff from commit via "patch -p1 -s". Aborting...',
)
sys.exit(patch_cmd.returncode)
self.repository.commit(
patch["commit_msg"], patch["author"], patch["date"], [target_dir]
)
self.log(logging.INFO, "commit_pass", {}, "Committed successfully.")
def _file_bug(self, token, repo, pr_number):
import requests
bug = requests.post(
"https://bugzilla.mozilla.org/rest/bug?api_key=%s" % token,
json={
"product": repo["bugzilla_product"],
"component": repo["bugzilla_component"],
"summary": "Land %s#%s in mozilla-central"
% (repo["github"], pr_number),
"version": "unspecified",
},
)
bug.raise_for_status()
self.log(logging.DEBUG, "new_bug", {}, bug.content)
bugnumber = json.loads(bug.content)["id"]
self.log(
logging.INFO, "new_bug", {"bugnumber": bugnumber}, "Filed bug {bugnumber}"
)
return bugnumber
def _split_patches(self, patchfile, bug_number, pull_request, reviewer):
INITIAL = 0
HEADERS = 1
STAT_AND_DIFF = 2
patch = b""
state = INITIAL
for line in patchfile.splitlines():
if state == INITIAL:
if line.startswith(b"From "):
state = HEADERS
elif state == HEADERS:
patch += line + b"\n"
if line == b"---":
state = STAT_AND_DIFF
elif state == STAT_AND_DIFF:
if line.startswith(b"From "):
yield self._parse_patch(patch, bug_number, pull_request, reviewer)
patch = b""
state = HEADERS
else:
patch += line + b"\n"
if len(patch) > 0:
yield self._parse_patch(patch, bug_number, pull_request, reviewer)
return
def _parse_patch(self, patch, bug_number, pull_request, reviewer):
import email
from email import (
header,
policy,
)
parse_policy = policy.compat32.clone(max_line_length=None)
parsed_mail = email.message_from_bytes(patch, policy=parse_policy)
def header_as_unicode(key):
decoded = header.decode_header(parsed_mail[key])
return str(header.make_header(decoded))
author = header_as_unicode("From")
date = header_as_unicode("Date")
commit_summary = header_as_unicode("Subject")
email_body = parsed_mail.get_payload(decode=True).decode("utf-8")
(commit_body, diff) = ("\n" + email_body).rsplit("\n---\n", 1)
bug_prefix = ""
if bug_number is not None:
bug_prefix = "Bug %s - " % bug_number
commit_summary = re.sub(r"^\[PATCH[0-9 /]*\] ", bug_prefix, commit_summary)
if reviewer is not None:
commit_summary += " r=" + reviewer
commit_msg = commit_summary + "\n"
if len(commit_body) > 0:
commit_msg += commit_body + "\n"
commit_msg += "\n[import_pr] From " + pull_request + "\n"
patch_obj = {
"author": author,
"date": date,
"commit_summary": commit_summary,
"commit_msg": commit_msg,
"diff": diff,
}
return patch_obj
|