summaryrefslogtreecommitdiff
path: root/zuul/reporter/__init__.py
blob: 9cd60bff69fbd7cf82a0cef5b86d68d0c8393bcc (plain)
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# Copyright 2014 Rackspace Australia
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import abc
import logging
from zuul.lib.config import get_default


class BaseReporter(object, metaclass=abc.ABCMeta):
    """Base class for reporters.

    Defines the exact public methods that must be supplied.
    """

    log = logging.getLogger("zuul.reporter.BaseReporter")

    def __init__(self, driver, connection, config=None):
        self.driver = driver
        self.connection = connection
        self.config = config or {}
        self._action = None

    def setAction(self, action):
        self._action = action

    @abc.abstractmethod
    def report(self, item, phase1=True, phase2=True):
        """Send the compiled report message

        Two-phase reporting may be enabled if one or the other of the
        `phase1` or `phase2` arguments is False.

        Phase1 should report everything except the actual merge action.
        Phase2 should report only the merge action.

        :arg phase1 bool: Whether to enable phase1 reporting
        :arg phase2 bool: Whether to enable phase2 reporting

        """

    def getSubmitAllowNeeds(self):
        """Get a list of code review labels that are allowed to be
        "needed" in the submit records for a change, with respect
        to this queue.  In other words, the list of review labels
        this reporter itself is likely to set before submitting.
        """
        return []

    def postConfig(self):
        """Run tasks after configuration is reloaded"""

    def addConfigurationErrorComments(self, item, comments):
        """Add file comments for configuration errors.

        Updates the comments dictionary with additional file comments
        for any relevant configuration errors for this item's change.

        :arg QueueItem item: The queue item
        :arg dict comments: a file comments dictionary

        """

        for err in item.getConfigErrors():
            context = err.key.context
            mark = err.key.mark
            if not (context and mark and err.short_error):
                continue
            if context.project_canonical_name != \
                    item.change.project.canonical_name:
                continue
            if not hasattr(item.change, 'branch'):
                continue
            if context.branch != item.change.branch:
                continue
            if context.path not in item.change.files:
                continue
            existing_comments = comments.setdefault(context.path, [])
            existing_comments.append(dict(line=mark.end_line,
                                          message=err.short_error,
                                          range=dict(
                                              start_line=mark.line + 1,
                                              start_character=mark.column,
                                              end_line=mark.end_line,
                                              end_character=mark.end_column)))

    def _getFileComments(self, item):
        """Get the file comments from the zuul_return value"""
        ret = {}
        for build in item.current_build_set.getBuilds():
            fc = build.result_data.get("zuul", {}).get("file_comments")
            if not fc:
                continue
            for fn, comments in fc.items():
                existing_comments = ret.setdefault(fn, [])
                existing_comments.extend(comments)
        self.addConfigurationErrorComments(item, ret)
        return ret

    def getFileComments(self, item):
        comments = self._getFileComments(item)
        self.filterComments(item, comments)
        return comments

    def filterComments(self, item, comments):
        """Filter comments for files in change

        Remove any comments for files which do not appear in the
        item's change.  Leave warning messages if this happens.

        :arg QueueItem item: The queue item
        :arg dict comments: a file comments dictionary (modified in place)
        """

        for fn in list(comments.keys()):
            if fn not in item.change.files:
                del comments[fn]
                item.warning("Comments left for invalid file %s" % (fn,))

    def _getFormatter(self, action):
        format_methods = {
            'enqueue': self._formatItemReportEnqueue,
            'start': self._formatItemReportStart,
            'success': self._formatItemReportSuccess,
            'failure': self._formatItemReportFailure,
            'merge-conflict': self._formatItemReportMergeConflict,
            'merge-failure': self._formatItemReportMergeFailure,
            'no-jobs': self._formatItemReportNoJobs,
            'disabled': self._formatItemReportDisabled,
            'dequeue': self._formatItemReportDequeue,
        }
        return format_methods[action]

    def _formatItemReport(self, item, with_jobs=True, action=None):
        """Format a report from the given items. Usually to provide results to
        a reporter taking free-form text."""
        action = action or self._action
        ret = self._getFormatter(action)(item, with_jobs)

        if item.current_build_set.warning_messages:
            warning = '\n  '.join(item.current_build_set.warning_messages)
            ret += '\nWarning:\n  ' + warning + '\n'

        if item.current_build_set.debug_messages:
            debug = '\n  '.join(item.current_build_set.debug_messages)
            ret += '\nDebug information:\n  ' + debug + '\n'

        if item.pipeline.footer_message:
            ret += '\n' + item.pipeline.footer_message

        return ret

    def _formatItemReportEnqueue(self, item, with_jobs=True):
        status_url = self.connection.sched.globals.web_status_url
        if status_url:
            status_url = item.formatUrlPattern(status_url)

        return item.pipeline.enqueue_message.format(
            pipeline=item.pipeline.getSafeAttributes(),
            change=item.change.getSafeAttributes(),
            status_url=status_url)

    def _formatItemReportStart(self, item, with_jobs=True):
        status_url = self.connection.sched.globals.web_status_url
        if status_url:
            status_url = item.formatUrlPattern(status_url)

        return item.pipeline.start_message.format(
            pipeline=item.pipeline.getSafeAttributes(),
            change=item.change.getSafeAttributes(),
            status_url=status_url)

    def _formatItemReportSuccess(self, item, with_jobs=True):
        msg = item.pipeline.success_message
        if with_jobs:
            status_url = item.formatStatusUrl()
            if status_url is not None:
                msg += '\n' + status_url
            msg += '\n\n' + self._formatItemReportJobs(item)
        return msg

    def _formatItemReportFailure(self, item, with_jobs=True):
        if item.cannotMergeBundle():
            msg = 'This change is part of a bundle that can not merge.\n'
            if isinstance(item.bundle.cannot_merge, str):
                msg += '\n' + item.bundle.cannot_merge + '\n'
        elif item.dequeued_needing_change:
            msg = 'This change depends on a change that failed to merge.\n'
            if isinstance(item.dequeued_needing_change, str):
                msg += '\n' + item.dequeued_needing_change + '\n'
        elif item.dequeued_missing_requirements:
            msg = ('This change is unable to merge '
                   'due to a missing merge requirement.\n')
        elif item.isBundleFailing():
            msg = 'This change is part of a bundle that failed.\n'
            if with_jobs:
                msg = '{}\n\n{}'.format(msg, self._formatItemReportJobs(item))
            msg = "{}\n\n{}".format(
                msg, self._formatItemReportOtherBundleItems(item))
        elif item.didMergerFail():
            msg = item.pipeline.merge_conflict_message
        elif item.getConfigErrors():
            msg = str(item.getConfigErrors()[0].error)
        else:
            msg = item.pipeline.failure_message
            if with_jobs:
                status_url = item.formatStatusUrl()
                if status_url is not None:
                    msg += '\n' + status_url
                msg += '\n\n' + self._formatItemReportJobs(item)
        return msg

    def _formatItemReportMergeConflict(self, item, with_jobs=True):
        return item.pipeline.merge_conflict_message

    def _formatItemReportMergeFailure(self, item, with_jobs=True):
        return 'This change was not merged by the code review system.\n'

    def _formatItemReportNoJobs(self, item, with_jobs=True):
        status_url = get_default(self.connection.sched.config,
                                 'web', 'status_url', '')
        if status_url:
            status_url = item.formatUrlPattern(status_url)

        return item.pipeline.no_jobs_message.format(
            pipeline=item.pipeline.getSafeAttributes(),
            change=item.change.getSafeAttributes(),
            status_url=status_url)

    def _formatItemReportDisabled(self, item, with_jobs=True):
        if item.current_build_set.result == 'SUCCESS':
            return self._formatItemReportSuccess(item)
        elif item.current_build_set.result == 'FAILURE':
            return self._formatItemReportFailure(item)
        else:
            return self._formatItemReport(item)

    def _formatItemReportDequeue(self, item, with_jobs=True):
        msg = item.pipeline.dequeue_message
        if with_jobs:
            msg += '\n\n' + self._formatItemReportJobs(item)
        return msg

    def _formatItemReportOtherBundleItems(self, item):
        related_changes = item.pipeline.manager.resolveChangeReferences(
            item.change.needs_changes)
        return "Related changes:\n{}\n".format("\n".join(
            f'  - {c.url}' for c in related_changes if c is not item.change))

    def _getItemReportJobsFields(self, item):
        # Extract the report elements from an item
        config = self.connection.sched.config
        jobs_fields = []
        skipped = 0
        for job in item.getJobs():
            build = item.current_build_set.getBuild(job.name)
            (result, url) = item.formatJobResult(job)
            if result == 'SKIPPED':
                skipped += 1
                continue
            if not job.voting:
                voting = ' (non-voting)'
            else:
                voting = ''

            if config and config.has_option(
                'zuul', 'report_times'):
                report_times = config.getboolean(
                    'zuul', 'report_times')
            else:
                report_times = True

            if report_times and build.end_time and build.start_time:
                dt = int(build.end_time - build.start_time)
                m, s = divmod(dt, 60)
                h, m = divmod(m, 60)
                if h:
                    elapsed = ' in %dh %02dm %02ds' % (h, m, s)
                elif m:
                    elapsed = ' in %dm %02ds' % (m, s)
                else:
                    elapsed = ' in %ds' % (s)
            else:
                elapsed = ''
            if build.error_detail:
                error = ' ' + build.error_detail
            else:
                error = ''
            name = job.name + ' '
            # TODO(mordred) The gerrit consumption interface depends on
            # something existing in the url field and don't have a great
            # behavior defined for url being none/missing. Put name into
            # the url field to match old behavior until we can deal with
            # the gerrit-side piece as well
            url = url or job.name
            # Pass user defined success_message deciding the build result
            # during result formatting
            success_message = job.success_message
            jobs_fields.append(
                (name, url, result, error, elapsed, voting, success_message))
        return jobs_fields, skipped

    def _formatItemReportJobs(self, item):
        # Return the list of jobs portion of the report
        ret = ''
        jobs_fields, skipped = self._getItemReportJobsFields(item)
        for job_fields in jobs_fields:
            ret += '- %s%s : %s%s%s%s\n' % job_fields[:6]
        if skipped:
            jobtext = 'job' if skipped == 1 else 'jobs'
            ret += 'Skipped %i %s\n' % (skipped, jobtext)
        return ret