Commits

Anonymous committed 084c12f

[svn r9471] Added option to display the culprit that caused the build.

This is very loosely based off of cboos' author information patch
https://github.com/cboos/trachacks-hudsontracplugin/commit/e7a4b15270f55b256693fe912e1870105adedf2a
but made configurable and with more options for where to get the author from.

Comments (0)

Files changed (1)

0.10/HudsonTrac/HudsonTracPlugin.py

 import base64
 from datetime import datetime
 from trac.core import *
-from trac.config import Option, BoolOption
+from trac.config import Option, BoolOption, ListOption
 from trac.perm import IPermissionRequestor
 from trac.util import Markup, format_datetime, pretty_timedelta
 from trac.util.text import unicode_quote
                                'Also show in-progress builds')
     list_changesets = BoolOption('hudson', 'list_changesets', False,
                                  'List the changesets for each build')
+    disp_culprit = ListOption('hudson', 'display_culprit', [], doc =
+                              'Display the culprit(s) for each build. This is '
+                              'a comma-separated list of zero or more of the '
+                              'following tokens: `starter`, `author`, '
+                              '`authors`, `culprit`, `culprits`. `starter` is '
+                              'the user that started the build, if any; '
+                              '`author` is the author of the first commit, if '
+                              'any; `authors` is the list of authors of all '
+                              'commits; `culprit` is the first of what hudson '
+                              'thinks are the culprits that caused the build; '
+                              'and `culprits` is the list of all culprits. If '
+                              'given a list, the first non-empty value is used.'
+                              ' Example: `starter,authors` (this would show '
+                              'who started the build if it was started '
+                              'manually, else list the authors of the commits '
+                              'that triggered the build if any, else show no '
+                              'author for the build).')
 
     def __init__(self):
         # get base api url
 
         items = 'builds[building,timestamp,duration,result,description,url,' \
                 'fullDisplayName'
+
+        elems = []
         if self.list_changesets:
-            items += ',changeSet[items[revision,id]]'
+            elems.append('revision')
+            elems.append('id')
+        if 'author' in self.disp_culprit or 'authors' in self.disp_culprit:
+            elems.append('user')
+            elems.append('author[fullName]')
+        if elems:
+            items += ',changeSet[items[%s]]' % ','.join(elems)
+
+        if 'culprit' in self.disp_culprit or 'culprits' in self.disp_culprit:
+            items += ',culprits[fullName]'
+
+        if 'starter' in self.disp_culprit:
+            items += ',actions[causes[userName]]'
+
         items += ']'
 
         # assemble final url
                     revs = [self.__fmt_changeset(r, req) for r in revs]
                     changesets = '<br/>Changesets: ' + ', '.join(revs)
 
+            # get author(s)
+            author = None
+            for c in self.disp_culprit:
+                author = {
+                    'starter':  __find_first(entry, 'actions.causes.userName'),
+                    'author':   __find_first(entry, ['changeSet.items.user',
+                                           'changeSet.items.author.fullName']),
+                    'authors':  __find_all(entry, ['changeSet.items.user',
+                                           'changeSet.items.author.fullName']),
+                    'culprit':  __find_first(entry, 'culprits.fullName'),
+                    'culprits': __find_all(entry, 'culprits.fullName'),
+                }.get(c)
+
+                if author and not isinstance(author, basestring):
+                    author = ', '.join(set(author))
+                if author:
+                    author = unicode(author, cset)
+                    break
+
             # format response
             if result == 'IN-PROGRESS':
                 comment = Markup("%s since %s, duration %s%s" % (
             title = 'Build "%s" (%s)' % \
                     (unicode(entry['fullDisplayName'], cset), result.lower())
 
-            yield kind, href, title, completed, None, comment
+            yield kind, href, title, completed, author, comment
 
     class HudsonFormLoginHandler(urllib2.BaseHandler):
         def __init__(self, parent):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.