Edgewall Software
Modify

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#11349 closed enhancement (fixed)

Allow commit message action to follow ticket number.

Reported by: anonymous Owned by: Ryan J Ollos
Priority: normal Milestone: 1.0.2
Component: ticket system Version: 1.0.1
Severity: normal Keywords: CommitTicketUpdater
Cc: ethan.jucovy@… Branch:
Release Notes:

CommitTicketUpdater: Use finditer for matching in the regex, so that order of patterns can be interchanged when overriding the regex in subclasses.

API Changes:
Internal Changes:

Description

Currently commit_updater.py allows the status of a ticket to be set through commit messages, I am currently using it and doing "closes #1234" and "fixes #1234" works perfectly. I would also like it to be possible to do this in reverse such as "#1234 fixed" and "1234 and 1235 closed" I was curious how difficult this would be to implement I know it would take some regex modification to http://trac.edgewall.org/browser/trunk/tracopt/ticket/commit_updater.py, probably lines 135-139.

Attachments (0)

Change History (7)

comment:1 by Ryan J Ollos, 10 years ago

Owner: set to Ryan J Ollos
Status: newassigned

This hasn't been tested much so the regex might need to be tweaked a bit more.

You can drop it in your environment plugin's directory, naming it something like custom_commit_updater.py. Make sure to disable tracopt.ticket.commit_updater.CommitTicketUpdater and enable this plugin (it will be enabled by default if you put it in your plugins directory).

from tracopt.ticket.commit_updater import CommitTicketUpdater

class CustomCommitTicketUpdater(CommitTicketUpdater):

    ticket_command = (r'(?P<ticket>%s(?:(?:[, &]*|[ ]?and[ ]?)%s)*)\s*'
                      r'(?P<action>[A-Za-z]*)\s*.?' %
                      (CommitTicketUpdater.ticket_reference,
                       CommitTicketUpdater.ticket_reference))

    def _parse_message(self, message):
        """Parse the commit message and return the ticket references."""
        cmd_groups = self.command_re.finditer(message)
        functions = self._get_functions()
        tickets = {}
        for m in cmd_groups:
            cmd = m.group('action')
            tkts = m.group('ticket')
            func = functions.get(cmd.lower())
            if not func and self.commands_refs.strip() == '<ALL>':
                func = self.cmd_refs
            if func:
                for tkt_id in self.ticket_re.findall(tkts):
                    tickets.setdefault(int(tkt_id), []).append(func)
        return tickets

As for any possible changes to Trac, a modification to _parse_message would simplify CustomCommitTicketUpdater. Really, all that is needed for your case is this change:

  • tracopt/ticket/commit_updater.py

    diff --git a/tracopt/ticket/commit_updater.py b/tracopt/ticket/commit_updater.py
    index aad5cfc..b392f91 100644
    a b class CommitTicketUpdater(Component):  
    185185        cmd_groups = self.command_re.findall(message)
    186186        functions = self._get_functions()
    187187        tickets = {}
    188         for cmd, tkts in cmd_groups:
     188        for tkts, cmd in cmd_groups:
    189189            func = functions.get(cmd.lower())
    190190            if not func and self.commands_refs.strip() == '<ALL>':
    191191                func = self.cmd_refs

However, I modified _parse_message for your case in a way that would work for the existing syntax (e.g. Refs #1) as well as your custom syntax (e.g. #1 Refs), so that we could consider this as a possible patch for Trac:

  • tracopt/ticket/commit_updater.py

    index aad5cfc..649d883 100644
    a b class CommitTicketUpdater(Component):  
    182182
    183183    def _parse_message(self, message):
    184184        """Parse the commit message and return the ticket references."""
    185         cmd_groups = self.command_re.findall(message)
     185        cmd_groups = self.command_re.finditer(message)
    186186        functions = self._get_functions()
    187187        tickets = {}
    188         for cmd, tkts in cmd_groups:
     188        for m in cmd_groups:
     189            cmd = m.group('action')
     190            tkts = m.group('ticket')
    189191            func = functions.get(cmd.lower())
    190192            if not func and self.commands_refs.strip() == '<ALL>':
    191193                func = self.cmd_refs

If we applied that patch to Trac, you plugin would simply be:

from tracopt.ticket.commit_updater import CommitTicketUpdater

class CustomCommitTicketUpdater(CommitTicketUpdater):

    ticket_command = (r'(?P<ticket>%s(?:(?:[, &]*|[ ]?and[ ]?)%s)*)\s*'
                      r'(?P<action>[A-Za-z]*)\s*.?' %
                      (CommitTicketUpdater.ticket_reference,
                       CommitTicketUpdater.ticket_reference))

comment:2 by ethan.jucovy@…, 10 years ago

Cc: ethan.jucovy@… added

comment:3 by anonymous, 10 years ago

Thank you, I will give this a shot and let you know how it goes.

comment:4 by Ryan J Ollos, 10 years ago

Milestone: undecided1.0.2

A cleaner version of the proposed change can be found in log:rjollos.git:t11349. The use case for the change is fairly limited in scope, but at the same time the change seems harmless. I was thinking I'd also take the opportunity to add a few unit tests for CommitTicketUpdater (forthcoming).

comment:5 by Ryan J Ollos, 10 years ago

Added unit tests in log:rjollos.git:t11349.2.

comment:6 by Ryan J Ollos, 10 years ago

Keywords: CommitTicketUpdater added
Release Notes: modified (diff)

Committed to 1.0-stable in [12325:12326], and merged to trunk in [12327].

In Trac 1.0.2dev-r12326 and later, the simpler CommitTicketUpdater subclass can be used:

from tracopt.ticket.commit_updater import CommitTicketUpdater

class CustomCommitTicketUpdater(CommitTicketUpdater):

    ticket_command = (r'(?P<ticket>%s(?:(?:[, &]*|[ ]?and[ ]?)%s)*)\s*'
                      r'(?P<action>[A-Za-z]*)\s*.?' %
                      (CommitTicketUpdater.ticket_reference,
                       CommitTicketUpdater.ticket_reference))

It was mentioned earlier that you should disable tracopt.ticket.commit_updater.CommitTicketUpdater. However, it seems to work fine to leave them both enabled, and then both syntaxes can be used.

Last edited 10 years ago by Ryan J Ollos (previous) (diff)

comment:7 by Ryan J Ollos, 10 years ago

Resolution: fixed
Status: assignedclosed

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Ryan J Ollos.
The resolution will be deleted. Next status will be 'reopened'.
to The owner will be changed from Ryan J Ollos to the specified user.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.