Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/buildbot/buildbot/status/web/feeds.py
blob: c86ca3bdfe34798a2f7ce1ee670198b98e873c37 (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# This module enables ATOM and RSS feeds from webstatus.
#
# It is based on "feeder.py" which was part of the Buildbot
# configuration for the Subversion project. The original file was
# created by Lieven Gobaerts and later adjusted by API
# (apinheiro@igalia.coma) and also here
# http://code.google.com/p/pybots/source/browse/trunk/master/Feeder.py
#
# All subsequent changes to feeder.py where made by Chandan-Dutta
# Chowdhury <chandan-dutta.chowdhury @ hp.com> and Gareth Armstrong
# <gareth.armstrong @ hp.com>.
#
# Those modifications are as follows:
# 1) the feeds are usable from baseweb.WebStatus
# 2) feeds are fully validated ATOM 1.0 and RSS 2.0 feeds, verified
#    with code from http://feedvalidator.org
# 3) nicer xml output
# 4) feeds can be filtered as per the /waterfall display with the
#    builder and category filters
# 5) cleaned up white space and imports
#
# Finally, the code was directly integrated into these two files,
# buildbot/status/web/feeds.py (you're reading it, ;-)) and
# buildbot/status/web/baseweb.py.

import os
import re
import sys
import time
from twisted.web import resource
from buildbot.status.builder import SUCCESS, WARNINGS, FAILURE, EXCEPTION

class XmlResource(resource.Resource):
    contentType = "text/xml; charset=UTF-8"
    def render(self, request):
        data = self.content(request)
        request.setHeader("content-type", self.contentType)
        if request.method == "HEAD":
            request.setHeader("content-length", len(data))
            return ''
        return data
    docType = ''
    def header (self, request):
        data = ('<?xml version="1.0"?>\n')
        return data
    def footer(self, request):
        data = ''
        return data
    def content(self, request):
        data = self.docType
        data += self.header(request)
        data += self.body(request)
        data += self.footer(request)
        return data
    def body(self, request):
        return ''

class FeedResource(XmlResource):
    title = None
    link = 'http://dummylink'
    language = 'en-us'
    description = 'Dummy rss'
    status = None

    def __init__(self, status, categories=None, title=None):
        self.status = status
        self.categories = categories
        self.title = title
        self.link = self.status.getBuildbotURL()
        self.description = 'List of FAILED builds'
        self.pubdate = time.gmtime(int(time.time()))

    def getBuilds(self, request):
        builds = []
        # THIS is lifted straight from the WaterfallStatusResource Class in
        # status/web/waterfall.py
        #
        # we start with all Builders available to this Waterfall: this is
        # limited by the config-file -time categories= argument, and defaults
        # to all defined Builders.
        allBuilderNames = self.status.getBuilderNames(categories=self.categories)
        builders = [self.status.getBuilder(name) for name in allBuilderNames]

        # but if the URL has one or more builder= arguments (or the old show=
        # argument, which is still accepted for backwards compatibility), we
        # use that set of builders instead. We still don't show anything
        # outside the config-file time set limited by categories=.
        showBuilders = request.args.get("show", [])
        showBuilders.extend(request.args.get("builder", []))
        if showBuilders:
            builders = [b for b in builders if b.name in showBuilders]

        # now, if the URL has one or category= arguments, use them as a
        # filter: only show those builders which belong to one of the given
        # categories.
        showCategories = request.args.get("category", [])
        if showCategories:
            builders = [b for b in builders if b.category in showCategories]

        maxFeeds = 25

        # Copy all failed builds in a new list.
        # This could clearly be implemented much better if we had
        # access to a global list of builds.
        for b in builders:
            lastbuild = b.getLastFinishedBuild()
            if lastbuild is None:
                continue

            lastnr = lastbuild.getNumber()

            totalbuilds = 0
            i = lastnr
            while i >= 0:
                build = b.getBuild(i)
                i -= 1
                if not build:
                    continue

                results = build.getResults()

                # only add entries for failed builds!
                if results == FAILURE:
                    totalbuilds += 1
                    builds.append(build)

                # stop for this builder when our total nr. of feeds is reached
                if totalbuilds >= maxFeeds:
                    break

        # Sort build list by date, youngest first.
        if sys.version_info[:3] >= (2,4,0):
            builds.sort(key=lambda build: build.getTimes(), reverse=True)
        else:
            # If you need compatibility with python < 2.4, use this for
            # sorting instead:
            # We apply Decorate-Sort-Undecorate
            deco = [(build.getTimes(), build) for build in builds]
            deco.sort()
            deco.reverse()
            builds = [build for (b1, build) in deco]

        if builds:
            builds = builds[:min(len(builds), maxFeeds)]
        return builds

    def body (self, request):
        data = ''
        builds = self.getBuilds(request)

        for build in builds:
            start, finished = build.getTimes()
            finishedTime = time.gmtime(int(finished))
            projectName = self.status.getProjectName()
            link = re.sub(r'index.html', "", self.status.getURLForThing(build))

            # title: trunk r22191 (plus patch) failed on 'i686-debian-sarge1 shared gcc-3.3.5'
            ss = build.getSourceStamp()
            source = ""
            if ss.branch:
                source += "Branch %s " % ss.branch
            if ss.revision:
                source += "Revision %s " % str(ss.revision)
            if ss.patch:
                source += " (plus patch)"
            if ss.changes:
                pass
            if (ss.branch is None and ss.revision is None and ss.patch is None
                and not ss.changes):
                source += "Latest revision "
            got_revision = None
            try:
                got_revision = build.getProperty("got_revision")
            except KeyError:
                pass
            if got_revision:
                got_revision = str(got_revision)
                if len(got_revision) > 40:
                    got_revision = "[revision string too long]"
                source += "(Got Revision: %s)" % got_revision
            title = ('%s failed on "%s"' %
                     (source, build.getBuilder().getName()))

            # get name of the failed step and the last 30 lines of its log.
            if build.getLogs():
                log = build.getLogs()[-1]
                laststep = log.getStep().getName()
                try:
                    lastlog = log.getText()
                except IOError:
                    # Probably the log file has been removed
                    lastlog='<b>log file not available</b>'

            lines = re.split('\n', lastlog)
            lastlog = ''
            for logline in lines[max(0, len(lines)-30):]:
                lastlog = lastlog + logline + '<br/>'
            lastlog = lastlog.replace('\n', '<br/>')

            description = ''
            description += ('Date: %s<br/><br/>' %
                            time.strftime("%a, %d %b %Y %H:%M:%S GMT",
                                          finishedTime))
            description += ('Full details available here: <a href="%s">%s</a><br/>' %
                            (self.link, projectName))
            builder_summary_link = ('%s/builders/%s' %
                                    (re.sub(r'/index.html', '', self.link),
                                     build.getBuilder().getName()))
            description += ('Build summary: <a href="%s">%s</a><br/><br/>' %
                            (builder_summary_link,
                             build.getBuilder().getName()))
            description += ('Build details: <a href="%s">%s</a><br/><br/>' %
                            (link, self.link + link[1:]))
            description += ('Author list: <b>%s</b><br/><br/>' %
                            ",".join(build.getResponsibleUsers()))
            description += ('Failed step: <b>%s</b><br/><br/>' % laststep)
            description += 'Last lines of the build log:<br/>'

            data += self.item(title, description=description, lastlog=lastlog,
                              link=link, pubDate=finishedTime)

        return data

    def item(self, title='', link='', description='', pubDate=''):
        """Generates xml for one item in the feed."""

class Rss20StatusResource(FeedResource):
    def __init__(self, status, categories=None, title=None):
        FeedResource.__init__(self, status, categories, title)
        contentType = 'application/rss+xml'

    def header(self, request):
        data = FeedResource.header(self, request)
        data += ('<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">\n')
        data += ('  <channel>\n')
        if self.title is None:
            title = 'Build status of ' + status.getProjectName()
        else:
            title = self.title
        data += ('    <title>%s</title>\n' % title)
        if self.link is not None:
            data += ('    <link>%s</link>\n' % self.link)
        link = re.sub(r'/index.html', '', self.link)
        data += ('    <atom:link href="%s/rss" rel="self" type="application/rss+xml"/>\n' % link)
        if self.language is not None:
            data += ('    <language>%s</language>\n' % self.language)
        if self.description is not None:
            data += ('    <description>%s</description>\n' % self.description)
        if self.pubdate is not None:
            rfc822_pubdate = time.strftime("%a, %d %b %Y %H:%M:%S GMT",
                                           self.pubdate)
            data += ('    <pubDate>%s</pubDate>\n' % rfc822_pubdate)
        return data

    def item(self, title='', link='', description='', lastlog='', pubDate=''):
        data = ('      <item>\n')
        data += ('        <title>%s</title>\n' % title)
        if link is not None:
            data += ('        <link>%s</link>\n' % link)
        if (description is not None and lastlog is not None):
            lastlog = re.sub(r'<br/>', "\n", lastlog)
            lastlog = re.sub(r'&', "&amp;", lastlog)
            lastlog = re.sub(r"'", "&apos;", lastlog)
            lastlog = re.sub(r'"', "&quot;", lastlog)
            lastlog = re.sub(r'<', '&lt;', lastlog)
            lastlog = re.sub(r'>', '&gt;', lastlog)
            lastlog = lastlog.replace('\n', '<br/>')
            content = '<![CDATA['
            content += description
            content += lastlog
            content += ']]>'
            data += ('        <description>%s</description>\n' % content)
        if pubDate is not None:
            rfc822pubDate = time.strftime("%a, %d %b %Y %H:%M:%S GMT",
                                          pubDate)
            data += ('        <pubDate>%s</pubDate>\n' % rfc822pubDate)
            # Every RSS item must have a globally unique ID
            guid = ('tag:%s@%s,%s:%s' % (os.environ['USER'],
                                         os.environ['HOSTNAME'],
                                         time.strftime("%Y-%m-%d", pubDate),
                                         time.strftime("%Y%m%d%H%M%S",
                                                       pubDate)))
            data += ('    <guid isPermaLink="false">%s</guid>\n' % guid)
        data += ('      </item>\n')
        return data

    def footer(self, request):
        data = ('  </channel>\n'
                '</rss>')
        return data

class Atom10StatusResource(FeedResource):
    def __init__(self, status, categories=None, title=None):
        FeedResource.__init__(self, status, categories, title)
        contentType = 'application/atom+xml'

    def header(self, request):
        data = FeedResource.header(self, request)
        data += '<feed xmlns="http://www.w3.org/2005/Atom">\n'
        data += ('  <id>%s</id>\n' % self.status.getBuildbotURL())
        if self.title is None:
            title = 'Build status of ' + status.getProjectName()
        else:
            title = self.title
        data += ('  <title>%s</title>\n' % title)
        if self.link is not None:
            link = re.sub(r'/index.html', '', self.link)
            data += ('  <link rel="self" href="%s/atom"/>\n' % link)
            data += ('  <link rel="alternate" href="%s/"/>\n' % link)
        if self.description is not None:
            data += ('  <subtitle>%s</subtitle>\n' % self.description)
        if self.pubdate is not None:
            rfc3339_pubdate = time.strftime("%Y-%m-%dT%H:%M:%SZ",
                                            self.pubdate)
            data += ('  <updated>%s</updated>\n' % rfc3339_pubdate)
        data += ('  <author>\n')
        data += ('    <name>Build Bot</name>\n')
        data += ('  </author>\n')
        return data

    def item(self, title='', link='', description='', lastlog='', pubDate=''):
        data = ('  <entry>\n')
        data += ('    <title>%s</title>\n' % title)
        if link is not None:
            data += ('    <link href="%s"/>\n' % link)
        if (description is not None and lastlog is not None):
            lastlog = re.sub(r'<br/>', "\n", lastlog)
            lastlog = re.sub(r'&', "&amp;", lastlog)
            lastlog = re.sub(r"'", "&apos;", lastlog)
            lastlog = re.sub(r'"', "&quot;", lastlog)
            lastlog = re.sub(r'<', '&lt;', lastlog)
            lastlog = re.sub(r'>', '&gt;', lastlog)
            data += ('    <content type="xhtml">\n')
            data += ('      <div xmlns="http://www.w3.org/1999/xhtml">\n')
            data += ('        %s\n' % description)
            data += ('        <pre xml:space="preserve">%s</pre>\n' % lastlog)
            data += ('      </div>\n')
            data += ('    </content>\n')
        if pubDate is not None:
            rfc3339pubDate = time.strftime("%Y-%m-%dT%H:%M:%SZ",
                                           pubDate)
            data += ('    <updated>%s</updated>\n' % rfc3339pubDate)
            # Every Atom entry must have a globally unique ID
            # http://diveintomark.org/archives/2004/05/28/howto-atom-id
            guid = ('tag:%s@%s,%s:%s' % (os.environ['USER'],
                                         os.environ['HOSTNAME'],
                                         time.strftime("%Y-%m-%d", pubDate),
                                         time.strftime("%Y%m%d%H%M%S",
                                                       pubDate)))
            data += ('    <id>%s</id>\n' % guid)
        data += ('    <author>\n')
        data += ('      <name>Build Bot</name>\n')
        data += ('    </author>\n')
        data += ('  </entry>\n')
        return data

    def footer(self, request):
        data = ('</feed>')
        return data