bloodhound-trac / sample-plugins /

import re

from trac.config import Option
from trac.core import *
from trac.resource import ResourceNotFound
from trac.ticket.api import IMilestoneChangeListener
from trac.ticket.model import Version

revision = "$Rev: 9001 $"
url = "$URL: $"

class MilestoneToVersion(Component):
    """Automatically create a version when a milestone is completed.

    Sample plugin demonstrating the IMilestoneChangeListener interface.
    Creates a version from a just-completed milestone based on whether the
    milestone's name matches a specified pattern.


    pattern = Option('milestone_to_version', 'pattern',
        """A regular expression to match the names of milestones that should be
        made into versions when they are completed. The pattern must include
        one named group called 'version' that matches the version number

    def milestone_created(self, milestone):

    def milestone_changed(self, milestone, old_values):
        if not milestone.is_completed or 'completed' not in old_values \
                or old_values['completed'] is not None:
        m = re.match(self.pattern,
        if not m:
        version_name = m.groupdict().get('version')
        if not version_name:
            version = Version(self.env, version_name)
            if not version.time:
                version.time = milestone.completed
      'Existing version "%s" updated with completion '
                              'time from milestone "%s"' %
      'Version "%s" already exists.  No new version '
                              'created from milestone "%s"' %
        except ResourceNotFound:
            version = Version(self.env)
   = version_name
            version.time = milestone.completed
  'New version "%s" created from completed milstone '
                          '"%s".' % (,

    def milestone_deleted(self, milestone):
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
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.