Edgewall Software
Modify

Opened 15 years ago

Closed 15 years ago

Last modified 15 years ago

#9178 closed defect (invalid)

trac-post-commit-hook saving ticket as incorrect author

Reported by: jrozzi@… Owned by:
Priority: normal Milestone:
Component: contrib Version: 0.11-stable
Severity: normal Keywords: trac-post-commit-hook
Cc: Branch:
Release Notes:
API Changes:
Internal Changes:

Description

I have followed the documentation in the Trac Guide for "Automatic reference to the SVN changesets in Trac tickets". As described, I created the following post-commit hook script in my repository 'hooks' directory:

#!/bin/sh

REPOS="$1"
REV="$2"
TRAC_ENV="/home/path/to/trac/"

/usr/local/bin/python /path/to/repos/hooks/trac-post-commit-hook -p "$TRAC_ENV" -r "$REV"

And copied the "out-of-the-box" trac-post-commit-hook within the 'contrib' directory to my repository hooks directory. I will go ahead and paste this script anyways:

#!/usr/bin/env python

# trac-post-commit-hook
# ----------------------------------------------------------------------------
# Copyright (c) 2004 Stephen Hansen 
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
#   The above copyright notice and this permission notice shall be included in
#   all copies or substantial portions of the Software. 
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
# ----------------------------------------------------------------------------

# This Subversion post-commit hook script is meant to interface to the
# Trac (http://www.edgewall.com/products/trac/) issue tracking/wiki/etc 
# system.
# 
# It should be called from the 'post-commit' script in Subversion, such as
# via:
#
# REPOS="$1"
# REV="$2"
# TRAC_ENV="/path/to/tracenv"
#
# /usr/bin/python /usr/local/src/trac/contrib/trac-post-commit-hook \
#  -p "$TRAC_ENV" -r "$REV"
#
# (all the other arguments are now deprecated and not needed anymore)
#
# It searches commit messages for text in the form of:
#   command #1
#   command #1, #2
#   command #1 & #2 
#   command #1 and #2
#
# Instead of the short-hand syntax "#1", "ticket:1" can be used as well, e.g.:
#   command ticket:1
#   command ticket:1, ticket:2
#   command ticket:1 & ticket:2 
#   command ticket:1 and ticket:2
#
# In addition, the ':' character can be omitted and issue or bug can be used
# instead of ticket.
#
# You can have more than one command in a message. The following commands
# are supported. There is more than one spelling for each command, to make
# this as user-friendly as possible.
#
#   close, closed, closes, fix, fixed, fixes
#     The specified issue numbers are closed with the contents of this
#     commit message being added to it. 
#   references, refs, addresses, re, see 
#     The specified issue numbers are left in their current status, but 
#     the contents of this commit message are added to their notes. 
#
# A fairly complicated example of what you can do is with a commit message
# of:
#
#    Changed blah and foo to do this or that. Fixes #10 and #12, and refs #12.
#
# This will close #10 and #12, and add a note to #12.

import re
import os
import sys
from datetime import datetime 
from optparse import OptionParser

parser = OptionParser()
depr = '(not used anymore)'
parser.add_option('-e', '--require-envelope', dest='envelope', default='',
                  help="""
Require commands to be enclosed in an envelope.
If -e[], then commands must be in the form of [closes #4].
Must be two characters.""")
parser.add_option('-p', '--project', dest='project',
                  help='Path to the Trac project.')
parser.add_option('-r', '--revision', dest='rev',
                  help='Repository revision number.')
parser.add_option('-u', '--user', dest='user',
                  help='The user who is responsible for this action '+depr)
parser.add_option('-m', '--msg', dest='msg',
                  help='The log message to search '+depr)
parser.add_option('-c', '--encoding', dest='encoding',
                  help='The encoding used by the log message '+depr)
parser.add_option('-s', '--siteurl', dest='url',
                  help=depr+' the base_url from trac.ini will always be used.')

(options, args) = parser.parse_args(sys.argv[1:])

if not 'PYTHON_EGG_CACHE' in os.environ:
    os.environ['PYTHON_EGG_CACHE'] = os.path.join(options.project, '.egg-cache')

from trac.env import open_environment
from trac.ticket.notification import TicketNotifyEmail
from trac.ticket import Ticket
from trac.ticket.web_ui import TicketModule
# TODO: move grouped_changelog_entries to model.py
from trac.util.text import to_unicode
from trac.util.datefmt import utc
from trac.versioncontrol.api import NoSuchChangeset

ticket_prefix = '(?:#|(?:ticket|issue|bug)[: ]?)'
ticket_reference = ticket_prefix + '[0-9]+'
ticket_command =  (r'(?P<action>[A-Za-z]*).?'
                   '(?P<ticket>%s(?:(?:[, &]*|[ ]?and[ ]?)%s)*)' %
                   (ticket_reference, ticket_reference))

if options.envelope:
    ticket_command = r'\%s%s\%s' % (options.envelope[0], ticket_command,
                                    options.envelope[1])
    
command_re = re.compile(ticket_command)
ticket_re = re.compile(ticket_prefix + '([0-9]+)')

class CommitHook:
    _supported_cmds = {'close':      '_cmdClose',
                       'closed':     '_cmdClose',
                       'closes':     '_cmdClose',
                       'fix':        '_cmdClose',
                       'fixed':      '_cmdClose',
                       'fixes':      '_cmdClose',
                       'addresses':  '_cmdRefs',
                       're':         '_cmdRefs',
                       'references': '_cmdRefs',
                       'refs':       '_cmdRefs',
                       'see':        '_cmdRefs'}

    def __init__(self, project=options.project, author=options.user,
                 rev=options.rev, url=options.url):
        self.env = open_environment(project)
        repos = self.env.get_repository()
        repos.sync()
        
        # Instead of bothering with the encoding, we'll use unicode data
        # as provided by the Trac versioncontrol API (#1310).
        try:
            chgset = repos.get_changeset(rev)
        except NoSuchChangeset:
            return # out of scope changesets are not cached
        self.author = chgset.author
        self.rev = rev
        self.msg = "(In [%s]) %s" % (rev, chgset.message)
        self.now = datetime.now(utc)

        cmd_groups = command_re.findall(self.msg)

        tickets = {}
        for cmd, tkts in cmd_groups:
            funcname = CommitHook._supported_cmds.get(cmd.lower(), '')
            if funcname:
                for tkt_id in ticket_re.findall(tkts):
                    func = getattr(self, funcname)
                    tickets.setdefault(tkt_id, []).append(func)

        for tkt_id, cmds in tickets.iteritems():
            try:
                db = self.env.get_db_cnx()
                
                ticket = Ticket(self.env, int(tkt_id), db)
                for cmd in cmds:
                    cmd(ticket)

                # determine sequence number... 
                cnum = 0
                tm = TicketModule(self.env)
                for change in tm.grouped_changelog_entries(ticket, db):
                    if change['permanent']:
                        cnum += 1
                
                sys.stdout = open('/tmp/post-commit-log','w');
                print >>sys.stdout, 'author: %s, rev: %s \n' % (self.author, self.rev)
                ticket.save_changes(self.author, self.msg, self.now, db, cnum+1)
                db.commit()
                
                tn = TicketNotifyEmail(self.env)
                tn.notify(ticket, newticket=0, modtime=self.now)
            except Exception, e:
                # import traceback
                # traceback.print_exc(file=sys.stderr)
                print>>sys.stderr, 'Unexpected error while processing ticket ' \
                                   'ID %s: %s' % (tkt_id, e)
            

    def _cmdClose(self, ticket):
        ticket['status'] = 'closed'
        ticket['resolution'] = 'fixed'

    def _cmdRefs(self, ticket):
        pass


if __name__ == "__main__":
    if len(sys.argv) < 5:
        print "For usage: %s --help" % (sys.argv[0])
        print
        print "Note that the deprecated options will be removed in Trac 0.12."
    else:
        CommitHook()

All post-commit hooks work fine, meaning that if I commit with "closes #3" then it successfully closes ticket #3. The problem is that the author for the commit shown in trac is incorrect. Instead of it showing the real author of the revision, it shows the repository name as the author. When I remove the post-commit script, all commits thereafter have the correct author, so I am sure that svn has the correct author for the revision and that the post-commit hook script is causing the author to be overridden incorrectly.

I noticed previous trac-post-commit-hook scripts had an "author" field you could specify, however this option does not seem to work anymore. Is there a bug in the trac ticket system or trac-post-commit-hook script causing an incorrect author or am I just doing something wrong? I am using Trac version 0.11.7 and python 2.6.

Thanks,

Joe Rozzi

Attachments (0)

Change History (6)

comment:1 by jrozzi@…, 15 years ago

Just figured out on IRC that doing a

trac-admin /path/to/trac/env resync

fixes the authors of all of the commmits in the timeline. I could be wrong, but this confirms more to me that there is a bug in the trac-post-commit-hook code in version 0.11.7 or within the repos.sync(). They are also telling me this whole thing is not a problem in Trac 0.12. Is this true?

comment:2 by jrozzi@…, 15 years ago

Ok, some comments after more researching

  • I am running trac with mod_python and apache 2.2.14 (running as user nobody)
  • I upgraded to Trac 0.12dev and am experiencing the exact same problem as I was in 0.11-stable (commits are showing author as filesystem owner of repository)
  • I am now using the provided trac-svn-hook script within my post-commit hook and have the following plugins enabled in 0.12dev: CommitTicketReferenceMacro, CommitTicketUpdater
  • The owner uid name of my filesystem that owns the repos and trac is called includes (just for reference below)
  • I noticed that when doing a 'chown -R includes:nobody /path/to/trac /path/to/repos && chmod -R 751 includes:nobody /path/to/trac /path/to/repos' temporarily solves the incorrect author problem, however, the problem autmatigally comes back by itself after 1 or 2 commits are made. It appears some file/folder permissions are being reset by trac or something weird.

I know there is a lot of information in this ticket but just trying to help :)

comment:3 by jrozzi@…, 15 years ago

Ok, I have figured out a fix for this problem, and it may not even be a bug in trac. The developers that were closing a ticket through the commit comments using "closes #<ticketnum>" were connecting to svn+sshrepos using the same ssh key file. This was causing the issue of the log that who is actually closing the ticket.

You can probably close this ticket now and maybe it will help someone solve the same problem I had one of these days.

comment:4 by jrozzi@…, 15 years ago

Resolution: invalid
Status: newclosed

comment:5 by Remy Blank, 15 years ago

The CommitTicketUpdater component should use the same user for the ticket comment as was used for the commit. So indeed, if your developers were logging in to the same account, the ticket comments would be identical as well.

comment:6 by Remy Blank, 15 years ago

Milestone: 0.12

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The ticket will remain with no owner.
The resolution will be deleted. Next status will be 'reopened'.
to The owner will be changed from (none) 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.