Skip to content

Commit 65af4c3

Browse files
committed
Post a short replacement when Bugzilla rejects a comment
Bugzilla's web filter rejects some comments with an empty 406 response, which we failed to parse and crashed on. It has done this for comments quoting shell trace output: aom's moz.build error comment (which quotes the `set -x` output of generate_sources_mozbuild.sh) has been rejected every month since May, so the bug only got the generic "unknown error" comment and the maintainer never saw the actual error. On a 406, log the rejected comment and post a short replacement pointing at the Taskcluster task's log, keeping the needinfo and assignee. If that is rejected too, raise.
1 parent 272321b commit 65af4c3

3 files changed

Lines changed: 67 additions & 4 deletions

File tree

‎apis/bugzilla_api.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ def task_id_comment_tag():
2323
return os.environ.get('TASK_ID', 'missing')
2424

2525

26+
def task_log_url():
27+
if "TASK_ID" not in os.environ:
28+
return None
29+
root = os.environ.get('TASKCLUSTER_ROOT_URL', 'https://firefox-ci-tc.services.mozilla.com')
30+
return root.rstrip('/') + '/tasks/' + os.environ['TASK_ID']
31+
32+
33+
class CommentRejectedError(Exception):
34+
"""Bugzilla's web filter (not Bugzilla itself) refused the comment, with an
35+
empty 406 response. It has done this for comments quoting shell trace output."""
36+
pass
37+
38+
2639
def task_id_whiteboard():
2740
if "TASK_ID" in os.environ:
2841
return sq('task_id: ' + os.environ['TASK_ID'])
@@ -119,6 +132,9 @@ def commentOnBug(url, apikey, bugID, comment, needinfo=None, assignee=None):
119132
json=data
120133
)
121134

135+
if r.status_code == 406:
136+
raise CommentRejectedError("Bugzilla rejected the comment on Bug %s (status=406)" % bugID)
137+
122138
j = _load_json_or_raise(r, "commentOnBug")
123139

124140
if 'bugs' in j:

‎components/bugzilla.py‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# This Source Code Form is subject to the terms of the Mozilla Public
44
# License, v. 2.0. If a copy of the MPL was not distributed with this
55
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
6-
from apis.bugzilla_api import fileBug, commentOnBug, closeBug, openBugsMetadata, markFFVersionAffected, getBugComments
6+
from apis.bugzilla_api import fileBug, commentOnBug, closeBug, openBugsMetadata, markFFVersionAffected, getBugComments, CommentRejectedError, task_log_url
77
from components.providerbase import BaseProvider, INeedsLoggingProvider
88
from components.logging import LogLevel, logEntryExit
99

@@ -207,6 +207,16 @@ def MISSING_COMPONENT(library, filed_bug_id):
207207
filed_bug_id, FALLBACK_PRODUCT_COMPONENT[0], FALLBACK_PRODUCT_COMPONENT[1],
208208
library.yaml_path, filed_bug_id)
209209

210+
@staticmethod
211+
def COMMENT_REJECTED(log_url):
212+
s = "Updatebot tried to post a comment here, but Bugzilla rejected it, probably because "
213+
s += "it quoted command output that Bugzilla's web filter considered suspicious. "
214+
if log_url:
215+
s += "The full message is in the Updatebot log: " + log_url
216+
else:
217+
s += "The full message is in the log of the Updatebot run that tried to post it."
218+
return s
219+
210220
@staticmethod
211221
def TRY_RUN_SUBMITTED(revision, another=False):
212222
return "I've submitted a" + ("nother" if another else "") + " try run for this commit: https://treeherder.mozilla.org/jobs?repo=try&revision=" + revision
@@ -305,7 +315,7 @@ def _file_missing_component_bug(self, library, filed_bug_id):
305315
self.logger.log("Could not file a bug about the missing component in %s: %s" % (library.yaml_path, e), level=LogLevel.Error)
306316

307317
@logEntryExit
308-
def comment_on_bug(self, bug_id, comment, needinfo=None, assignee=None):
318+
def comment_on_bug(self, bug_id, comment, needinfo=None, assignee=None, _is_fallback=False):
309319
if len(comment) > 65535:
310320
self.logger.log("Comment on Bug %s is too long: %s characters; truncating." % (bug_id, len(comment)), level=LogLevel.Warning)
311321
suffix = "\n\nOops - the above comment was too long. I was unable to shrink it nicely, so I had to truncate it to fit Bugzilla's limits."
@@ -314,10 +324,16 @@ def comment_on_bug(self, bug_id, comment, needinfo=None, assignee=None):
314324
try:
315325
commentOnBug(
316326
self.config['url'], self.config['apikey'], bug_id, comment, needinfo=needinfo, assignee=assignee)
327+
except CommentRejectedError:
328+
if _is_fallback:
329+
raise
330+
self.logger.log("Bugzilla rejected the comment on Bug %s; posting a short replacement instead. The rejected comment was:\n%s" % (bug_id, comment), level=LogLevel.Warning)
331+
self.comment_on_bug(bug_id, CommentTemplates.COMMENT_REJECTED(task_log_url()), needinfo, assignee, _is_fallback=True)
332+
return
317333
except Exception as e:
318334
if is_needinfo_exception(e):
319335
self.logger.log("Developer is not accepting needinfos, trying again without...", level=LogLevel.Info)
320-
self.comment_on_bug(bug_id, comment, None, assignee)
336+
self.comment_on_bug(bug_id, comment, None, assignee, _is_fallback=_is_fallback)
321337
else:
322338
raise e
323339

‎tests/bugzilla.py‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# License, v. 2.0. If a copy of the MPL was not distributed with this
55
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
66

7+
import os
78
import re
89
import sys
910
import unittest
@@ -18,14 +19,15 @@
1819
from components.bugzilla import BugzillaProvider, CommentTemplates
1920
from components.logging import SimpleLoggerConfig
2021

21-
from apis.bugzilla_api import task_id_whiteboard
22+
from apis.bugzilla_api import task_id_whiteboard, CommentRejectedError
2223

2324
TRY_REVISION = "this-is-my-try-link"
2425

2526

2627
class MockBugzillaServer(server.BaseHTTPRequestHandler):
2728
closed_bug_ids = []
2829
filed_bugs = []
30+
received_comments = {}
2931

3032
def do_POST(self):
3133
expectedPath_file = "/bug?api_key=bob"
@@ -105,6 +107,14 @@ def do_PUT(self):
105107
size = int(self.headers.get('content-length'))
106108
content = json.loads(self.rfile.read(size).decode("utf-8"))
107109
bug_id = re.match(r"/bug/([0-9]+)\?api_key=bob", self.path).groups(0)[0]
110+
MockBugzillaServer.received_comments.setdefault(bug_id, []).append(content)
111+
112+
# Like Bugzilla's web filter: an empty 406. Bug 1238 rejects everything
113+
# but Updatebot's short replacement comment; bug 1239 rejects everything.
114+
if bug_id == "1239" or (bug_id == "1238" and "Bugzilla rejected it" not in content['comment']['body']):
115+
self.send_response(406)
116+
self.end_headers()
117+
return
108118

109119
self.send_response(200)
110120
self.send_header("Content-type", "application/json")
@@ -237,6 +247,27 @@ def testFileMissingComponentFollowUpFails(self):
237247
# Failing to file the follow-up must not lose the library's bug.
238248
self.assertEqual(self.bugzillaProvider.file_bug(library, "Update broken to new version V1", "Details", []), 457)
239249

250+
def testCommentRejected(self):
251+
os.environ['TASK_ID'] = 'abc123'
252+
try:
253+
self.bugzillaProvider.comment_on_bug(1238, "> ++ export VIRTUAL_ENV\n> ++ '[' -n /bin/bash ']'", needinfo='Jon')
254+
finally:
255+
del os.environ['TASK_ID']
256+
257+
sent = MockBugzillaServer.received_comments["1238"]
258+
self.assertEqual(len(sent), 2, "Expected the rejected comment, then a replacement")
259+
self.assertIn("Bugzilla rejected it", sent[1]['comment']['body'])
260+
self.assertIn("https://firefox-ci-tc.services.mozilla.com/tasks/abc123", sent[1]['comment']['body'])
261+
self.assertEqual(sent[1]['flags'][0]['requestee'], 'Jon', "The replacement should keep the needinfo")
262+
263+
def testCommentRejectedWithoutTaskId(self):
264+
self.assertIn("log of the Updatebot run", CommentTemplates.COMMENT_REJECTED(None))
265+
266+
def testCommentAndReplacementRejected(self):
267+
with self.assertRaises(CommentRejectedError):
268+
self.bugzillaProvider.comment_on_bug(1239, "> ++ export VIRTUAL_ENV")
269+
self.assertEqual(len(MockBugzillaServer.received_comments["1239"]), 2, "Should give up after the replacement is rejected too")
270+
240271
def testStatus(self):
241272
self.bugzillaProvider.mark_ff_version_affected(456, 76)
242273

0 commit comments

Comments
 (0)