-
Notifications
You must be signed in to change notification settings - Fork 42
/
lando.py
76 lines (61 loc) · 2.68 KB
/
lando.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
# -*- coding: utf-8 -*-
# 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/.
import structlog
from code_review_bot import Level
from code_review_bot.report.base import Reporter
logger = structlog.get_logger(__name__)
LANDO_MESSAGE = "The code review bot found {errors} {errors_noun} which should be fixed to avoid backout and {warnings} {warnings_noun}."
class LandoReporter(Reporter):
"""
Update lando with a warning message
"""
def __init__(self, configuration):
self.lando_api = None
def setup_api(self, lando_api):
logger.info("Publishing warnings to lando is enabled by the bot!")
self.lando_api = lando_api
def publish(self, issues, revision, task_failures, links, reviewers):
"""
Send an email to administrators
"""
assert (
revision.phabricator_id and revision.phabricator_phid and revision.diff
), "Revision must have a Phabricator ID, a PHID and a diff"
if self.lando_api is None:
logger.info("Lando integration is not set!")
return
nb_publishable = len([i for i in issues if i.is_publishable()])
nb_publishable_errors = sum(
1 for i in issues if i.is_publishable() and i.level == Level.Error
)
nb_publishable_warnings = nb_publishable - nb_publishable_errors
logger.info(
"Publishing warnings to lando for {0} errors and {1} warnings".format(
nb_publishable_errors, nb_publishable_warnings
),
revision=revision.phabricator_id,
diff=revision.diff["id"],
)
try:
# code-review.events sends an initial warning message to lando to specify that the analysis is in progress,
# we should remove it
self.lando_api.del_all_warnings(
revision.phabricator_id, revision.diff["id"]
)
if nb_publishable > 0:
self.lando_api.add_warning(
LANDO_MESSAGE.format(
errors=nb_publishable_errors,
errors_noun="error" if nb_publishable_errors == 1 else "errors",
warnings=nb_publishable_warnings,
warnings_noun="warning"
if nb_publishable_warnings == 1
else "warnings",
),
revision.phabricator_id,
revision.diff["id"],
)
except Exception as ex:
logger.error(str(ex), exc_info=True)