bloodhound-trac / contrib / trac-pre-commit-hook

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Author: Jonas Borgström <jonas@edgewall.com>
#
# This script will enforce the following policy:
#
#  "A checkin must reference an open ticket."
#
# This script should be invoked from the subversion pre-commit hook like this:
#
#  REPOS="$1"
#  TXN="$2"
#  TRAC_ENV="/somewhere/trac/project/"
#  LOG=`/usr/bin/svnlook log -t "$TXN" "$REPOS"`
#  /usr/bin/python /some/path/trac-pre-commit-hook "$TRAC_ENV" "$LOG" || exit 1
#
import os
import re
import sys

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

from trac.env import open_environment

def main():
    if len(sys.argv) != 3:
        print >> sys.stderr, 'Usage: %s <trac_project> <log_message>' % sys.argv[0]
        sys.exit(1)

    env_path = sys.argv[1]
    log = sys.argv[2]

    tickets = []
    for tmp in re.findall('(?:close|closed|closes|fix|fixed|fixes|addresses|references|refs|re|see)'
                          '.?(#[0-9]+(?:(?:[, &]+| *and *)#[0-9]+)*)', log.lower()):
        tickets += re.findall('#([0-9]+)', tmp)
    
    # At least one ticket has to be mentioned in the log message
    if tickets == []:
        print >> sys.stderr, 'At least one open ticket must be mentioned ' \
              'in the log message.'
        sys.exit(1)

    env = open_environment(env_path)
    db = env.get_db_cnx()

    cursor = db.cursor()
    cursor.execute("SELECT COUNT(id) FROM ticket WHERE "
                   "status <> 'closed' AND id IN (%s)" % ','.join(tickets))
    row = cursor.fetchone()
    # At least one of the tickets mentioned in the log messages has to
    # be open
    if not row or row[0] < 1:
        print >> sys.stderr, 'At least one open ticket must be mentioned ' \
              'in the log message.'
        sys.exit(1)
    else:
        sys.exit(0)

if __name__ == '__main__':
    main()
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.