如何使用 Trac & 实施提交后挂钩 Windows环境下的SVN?

发布于 2024-07-04 08:14:22 字数 367 浏览 5 评论 0原文

我在带有 Trac / SVN 的 Windows 环境中运行,我希望提交到存储库以集成到 Trac 并关闭 SVN 注释中指出的错误。

我知道有一些提交后挂钩可以做到这一点,但关于如何在 Windows 上执行此操作的信息不多。

有谁做过成功的吗? 为了实现这一目标,您采取了哪些步骤?

这是我需要在 SVN 中放置的钩子,但我不太确定如何在 Windows 环境中执行此操作。

Trac 提交后挂钩

I'm running in a windows environment with Trac / SVN and I want commits to the repository to integrate to Trac and close the bugs that were noted in the SVN Comment.

I know there's some post commit hooks to do that, but there's not much information about how to do it on windows.

Anyone done it successfully? And what were the steps you followed to achive it?

Here's the hook I need to put in place in SVN, but I'm not exactly sure how to do this in the Windows environment.

Trac Post Commit Hook

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

乖乖哒 2024-07-11 08:14:22

我要添加的一件事是“Code Monkey 的答案是完美的”——要警惕这一点(我的错误),

:: Modify paths here:

:: -- this one must be set
SET TRAC_ENV=d:\trac\MySpecialProject

:: -- set if Python is not in the system path
:: SET PYTHON_PATH=**d:\python**

:: -- set to the folder containing trac/ if installed in a non-standard location 
:: SET TRAC_PATH=**d:\python\Lib\site-packages\trac**

我没有设置非系统路径,花了我一段时间才看到明显的:D

只要匹配确定不-别人也犯同样的错误! 感谢代码猴子! 1000000000 点 :D

One thing I'll add "Code Monkey's Answer is PERFECT" - is to be wary of this (my mistake)

:: Modify paths here:

:: -- this one must be set
SET TRAC_ENV=d:\trac\MySpecialProject

:: -- set if Python is not in the system path
:: SET PYTHON_PATH=**d:\python**

:: -- set to the folder containing trac/ if installed in a non-standard location 
:: SET TRAC_PATH=**d:\python\Lib\site-packages\trac**

I hadn't set the Non-System paths and took me a while to see the obvious :D

Just match sure no-one else makes the same mistake! Thanks Code Monkey! 1000000000 points :D

究竟谁懂我的在乎 2024-07-11 08:14:22

提交后挂钩位于服务器端存储库所在的“hooks”目录中。 我不知道你的环境中哪里有它们,所以这只是一个例子,

例如 (Windows):

C:\Subversion\repositories\repo1\hooks\post-commit

例如(llinux/unix):

/usr/local/subversion/repositories/repo1/hooks/post-commit

Post commit hooks live in the "hooks" directory where ever you have the repository living on the server side. I don't know where you have them in your environment, so this is just an example

e.g. (windows):

C:\Subversion\repositories\repo1\hooks\post-commit

e.g. (llinux/unix):

/usr/local/subversion/repositories/repo1/hooks/post-commit
初吻给了烟 2024-07-11 08:14:22

对于所有想要安装最新 trac (0.11.5) 的 Windows 用户:
按照名为 TracOnWindows 的 Trac 网站上的说明进行操作。

即使您有 64 位 Windows,也请下载 32 位 1.5 Python。
注意:我在某处看到了如何编译 trac 以在 64 位系统上本地工作的说明。

安装所需的所有内容后,请转到存储库文件夹。 有文件夹挂钩。
里面放了 Code Monkey 提到的文件,但不要像他那样创建“trac-post-commit-resolve-ticket-ref.py”。 听取 Quant Analyst 的建议并像他说的那样:

“但是,根据您的 trac 版本获取正确的 python 脚本很重要。要获取适当的版本,SVN 检查文件夹:
http://svn.edgewall.com/repos/trac/branches/xxx -稳定/贡献
其中 xxx 对应于您正在使用的 trac 版本,例如:0.11"

从那里下载文件“trac-post-commit-hook”并将其放入 hooks 文件夹中。

在 trac-post-commit.cmd 中编辑这些行

SET PYTHON_PATH="Python 安装文件夹路径"

SET TRAC_ENV="您所在文件夹的路径
是否进行了 tracd initenv"

记住没有最后一个 \ !!!

我已将最后一行 -r "%REV%" 中的引号删除为 -r %REV% 但我不知道是否需要这样做。这现在不起作用(至少在我的 win 2008 服务器),因为钩子会失败(提交会正常)。默认情况下,权限受到限制,我们需要允许 python 或 svn 或 trac (无论我不知道)更改 trac 信息。因此,转到 trac 文件夹、项目文件夹、db 文件夹,右键单击 trac.db 并选择属性,转到安全选项卡并编辑权限以允许每个人完全控制,但这不是很安全,但我在这个安全性上浪费了一整天。很重要,我不想浪费另一个只是为了找到应该为哪个用户启用权限

希望这会有所帮助......

For all Windows users who wants to install newest trac (0.11.5):
Follow the instructions on Trac's site named TracOnWindows.

Download 32bit 1.5 Python even if You have 64bit Windows.
note: I saw somewhere instructions how to compile trac to work natively on 64bit system.

When You install all that is required go to the repository folder. There is folder hooks.
Inside it put files Code Monkey mentioned, but dont create "trac-post-commit-resolve-ticket-ref.py" like he did. Take advice from Quant Analyst and do like he said:

"However, it's important to get the right python script depending on your trac version. To get the appropriate version, SVN check out the folder:
http://svn.edgewall.com/repos/trac/branches/xxx-stable/contrib
where xxx corresponds to the trac version you're using, for instance: 0.11"

From there downoad file "trac-post-commit-hook" and put it in hooks folder.

Edit these lines in trac-post-commit.cmd

SET PYTHON_PATH="Path to python installation folder"

SET TRAC_ENV="Path to folder where you
did tracd initenv"

Remember no last \ !!!

I have removed quotes from last line -r "%REV%" to be -r %REV% but i dont know if this is needed. This will not work now ( at least on my win 2008 server ), because hook will fail ( commit will go ok). This got to do with permissions. By default permissions are restricted and we need to allow python or svn or trac ( whatever i dont know ) to change trac information. So go to your trac folder,project folder,db folder, right click trac.db and choose properties. Go to the security tab and edit permissions to allow everyone full control. This isn't so secure but i wasted all day on this security matter and i don't want to waste another just to find for which user you should enable permissions.

Hope this helps....

巷雨优美回忆 2024-07-11 08:14:22

首先非常感谢Code Monkey!

但是,根据您的 trac 版本获取正确的 python 脚本非常重要。 要获取适当的版本,SVN 请查看文件夹:

http://svn.edgewall.com /repos/trac/branches/xxx-stable/contrib

其中 xxx 对应于您正在使用的 trac 版本,例如:0.11

否则您将收到如下所示的提交后错误:

commit failed (details follow): MERGE of '/svn/project/trunk/web/directory/': 200 OK

First a big thanks to Code Monkey!

However, it's important to get the right python script depending on your trac version. To get the appropriate version, SVN check out the folder:

http://svn.edgewall.com/repos/trac/branches/xxx-stable/contrib

where xxx corresponds to the trac version you're using, for instance: 0.11

Otherwise you'll get a post-commit error that looks like this:

commit failed (details follow): MERGE of '/svn/project/trunk/web/directory/': 200 OK

夏日浅笑〃 2024-07-11 08:14:22

好吧,现在我有时间在弄清楚这一切后发布我的经验,并感谢克雷格让我走上正确的轨道。 以下是您需要执行的操作(至少对于 SVN v1.4 和 Trac v0.10.3):

  1. 找到您想要启用提交后挂钩的 SVN 存储库。
  2. 在 SVN 存储库内有一个名为 hooks 的目录,您将在其中放置提交后挂钩。
  3. 创建一个文件 post-commit.bat (这是 SVN 提交后自动调用的批处理文件)。
  4. 将以下代码放入 post-commit.bat 文件中(这将调用您的提交后 cmd 文件,并传入 SVN 自动传递的参数%1 是存储库,%2 是已提交的修订版本。

%~dp0\trac- post-commit-hook.cmd %1 %2

  1. 现在创建 trac-post-commit-hook.cmd 文件,如下所示:

@ECHO OFF
::
:: Trac
提交后挂钩脚本
Windows
::
:: 贡献者
markus,由 cboos 修改。

::
用法:
::
:: 1) 插入
post-commit.bat 中的以下行
脚本
::
:: 调用
%~dp0\trac-post-commit-hook.cmd %1
%2
::
:: 2) 检查“修改”
下面的路径部分,请务必设置
至少 TRAC_ENV

::
-------------------------------------------------- --------
:: 在此修改路径:

:: --
这一项必须设置
SET
TRAC_ENV=C:\trac\MySpecialProject

:: -- 如果 Python 不在其中则设置
系统路径
::SET
PYTHON_PATH=

:: -- 设置为
包含 trac/ 的文件夹(如果已安装)
在非标准位置
::SET
TRAC_PATH=
::
-------------------------------------------------- --------

:: 如果 trac 则不执行 hook
环境不存在
如果不存在
存在%TRAC_ENV% 转到:EOF

设置 PATH=%PYTHON_PATH%;%PATH%
设置
PYTHONPATH=%TRAC_PATH%;%PYTHONPATH%

设置 REV=%2

:: 获取
作者和 /F 的日志消息
%%A in ('svnlook 作者 -r %REV% %1')
为 /F 设置 AUTHOR=%%A
“delims==” %%B in ('svnlook log -r
%REV% %1') 设置 LOG=%%B

::
调用 Python 脚本
Python
"%~dp0\trac-post-commit-hook" -p
“%TRAC_ENV%”-r“%REV%”-u“%AUTHOR%”
-m“%LOG%”

这里最重要的部分是设置 TRAC_ENV,它是存储库根目录的路径 (SET TRAC_ENV=C:\trac\MySpecialProject)

此脚本中的下一个最重要的事情是要做下列:

:: 获取作者和日志
消息
for /F %%A in ('svnlook
作者 -r %REV% %1') 进行设置
作者=%%A
for /F "delims==" %%B
在 ('svnlook log -r %REV% %1') 中设置
日志=%%B

如果您在上面的脚本文件中看到我正在使用 svnlook(这是 SVN 的命令行实用程序)来获取 LOG 消息以及提交到存储库的作者。

然后,脚本的下一行实际上是调用 Python 代码来执行票证关闭并解析日志消息。 我必须修改它以传递日志消息和作者(我在 Trac 中使用的用户名与 SVN 中的用户名匹配,所以这很容易)。

调用 Python 脚本
Python
"%~dp0\trac-post-commit-hook" -p
“%TRAC_ENV%”-r“%REV%”-u“%AUTHOR%”
-m“%LOG%”

脚本中的上述行将把 Trac 环境、修订版本、提交者及其评论传递到 python 脚本中。

这是我使用的 Python 脚本。 除了常规脚本之外,我做的一件事是我们使用自定义字段 (fixed_in_ver),我们的 QA 团队使用该字段来判断他们正在验证的修复是否位于他们在 QA 中测试的代码版本中。 因此,我修改了 python 脚本中的代码来更新票证上的该字段。 您可以删除该代码,因为您不需要它,但如果您也想这样做,它是一个很好的示例,说明您可以在 Trac 中更新自定义字段。

我通过让用户选择在他们的评论中包含类似以下内容来做到这一点:

(版本2.1.2223.0)

然后,我使用 Python 脚本与正则表达式相同的技术来获取信息。 还不错。

无论如何,这是我使用的 python 脚本,希望这是一个很好的教程,讲述了我如何让它在 Windows 世界中工作,这样你们都可以在自己的商店中利用它......

如果你不想处理使用我用于更新自定义字段的附加代码,从上面 Craig 提到的位置获取基本脚本 (来自 Edgewall 的脚本)

#!/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"
# LOG=`/usr/bin/svnlook log -r $REV $REPOS`
# AUTHOR=`/usr/bin/svnlook author -r $REV $REPOS`
# TRAC_ENV='/somewhere/trac/project/'
# TRAC_URL='http://trac.mysite.com/project/'
#
# /usr/bin/python /usr/local/src/trac/contrib/trac-post-commit-hook \
#  -p "$TRAC_ENV"  \
#  -r "$REV"       \
#  -u "$AUTHOR"    \
#  -m "$LOG"       \
#  -s "$TRAC_URL"
#
# It searches commit messages for text in the form of:
#   command #1
#   command #1, #2
#   command #1 & #2 
#   command #1 and #2
#
# You can have more then one command in a message. The following commands
# are supported. There is more then one spelling for each command, to make
# this as user-friendly as possible.
#
#   closes, fixes
#     The specified issue numbers are closed with the contents of this
#     commit message being added to it. 
#   references, refs, addresses, re 
#     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
import time 

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.web.href import Href

try:
    from optparse import OptionParser
except ImportError:
    try:
        from optik import OptionParser
    except ImportError:
        raise ImportError, 'Requires Python 2.3 or the Optik option parsing library.'

parser = OptionParser()
parser.add_option('-e', '--require-envelope', dest='env', 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')
parser.add_option('-m', '--msg', dest='msg',
                  help='The log message to search.')
parser.add_option('-c', '--encoding', dest='encoding',
                  help='The encoding used by the log message.')
parser.add_option('-s', '--siteurl', dest='url',
                  help='The base URL to the project\'s trac website (to which '
                       '/ticket/## is appended).  If this is not specified, '
                       'the project URL from trac.ini will be used.')

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

if options.env:
    leftEnv = '\\' + options.env[0]
    rghtEnv = '\\' + options.env[1]
else:
    leftEnv = ''
    rghtEnv = ''

commandPattern = re.compile(leftEnv + r'(?P<action>[A-Za-z]*).?(?P<ticket>#[0-9]+(?:(?:[, &]*|[ ]?and[ ]?)#[0-9]+)*)' + rghtEnv)
ticketPattern = re.compile(r'#([0-9]*)')
versionPattern = re.compile(r"\(version[ ]+(?P<version>([0-9]+)\.([0-9]+)\.([0-9]+)\.([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, msg=options.msg, url=options.url,
                 encoding=options.encoding):
        msg = to_unicode(msg, encoding)
        self.author = author
        self.rev = rev
        self.msg = "(In [%s]) %s" % (rev, msg)
        self.now = int(time.time()) 
        self.env = open_environment(project)
        if url is None:
            url = self.env.config.get('project', 'url')
        self.env.href = Href(url)
        self.env.abs_href = Href(url)

        cmdGroups = commandPattern.findall(msg)


        tickets = {}

        for cmd, tkts in cmdGroups:
            funcname = CommitHook._supported_cmds.get(cmd.lower(), '')

            if funcname:

                for tkt_id in ticketPattern.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

                # get the version number from the checkin... and update the ticket with it.
                version = versionPattern.search(msg)
                if version != None and version.group("version") != None:
                    ticket['fixed_in_ver'] = version.group("version")

                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])
    else:
        CommitHook()

Alright, now that I've got some time to post my experience after figuring this all out, and thanks to Craig for getting me on the right track. Here's what you need to do (at least with SVN v1.4 and Trac v0.10.3):

  1. Locate your SVN repository that you want to enable the Post Commit Hook for.
  2. inside the SVN repository there's a directory called hooks, this is where you'll be placing the post commit hook.
  3. create a file post-commit.bat (this is the batch file that's automatically called by SVN post commit).
  4. Place the following code inside the post-commit.bat file ( this will call your post commit cmd file passing in the parameters that SVN automatically passes %1 is the repository, %2 is the revision that was committed.

%~dp0\trac-post-commit-hook.cmd %1 %2

  1. Now create the trac-post-commit-hook.cmd file as follows:

@ECHO OFF
::
:: Trac
post-commit-hook script for
Windows
::
:: Contributed by
markus, modified by cboos.

::
Usage:
::
:: 1) Insert the
following line in your post-commit.bat
script
::
:: call
%~dp0\trac-post-commit-hook.cmd %1
%2
::
:: 2) Check the 'Modify
paths' section below, be sure to set
at least TRAC_ENV

::
----------------------------------------------------------
:: Modify paths here:

:: --
this one must be set
SET
TRAC_ENV=C:\trac\MySpecialProject

:: -- set if Python is not in the
system path
:: SET
PYTHON_PATH=

:: -- set to the
folder containing trac/ if installed
in a non-standard location
:: SET
TRAC_PATH=
::
----------------------------------------------------------

:: Do not execute hook if trac
environment does not exist
IF NOT
EXIST %TRAC_ENV% GOTO :EOF

set PATH=%PYTHON_PATH%;%PATH%
set
PYTHONPATH=%TRAC_PATH%;%PYTHONPATH%

SET REV=%2

:: GET THE
AUTHOR AND THE LOG MESSAGE
for /F
%%A in ('svnlook author -r %REV% %1')
do set AUTHOR=%%A
for /F
"delims==" %%B in ('svnlook log -r
%REV% %1') do set LOG=%%B

::
CALL THE PYTHON SCRIPT
Python
"%~dp0\trac-post-commit-hook" -p
"%TRAC_ENV%" -r "%REV%" -u "%AUTHOR%"
-m "%LOG%"

The most important parts here are to set your TRAC_ENV which is the path to the repository root (SET TRAC_ENV=C:\trac\MySpecialProject)

The next MAJORLY IMPORTANT THING in this script is to do the following:

:: GET THE AUTHOR AND THE LOG
MESSAGE
for /F %%A in ('svnlook
author -r %REV% %1') do set
AUTHOR=%%A
for /F "delims==" %%B
in ('svnlook log -r %REV% %1') do set
LOG=%%B

if you see in the script file above I'm using svnlook (which is a command line utility with SVN) to get the LOG message and the author that made the commit to the repository.

Then, the next line of the script is actually calling the Python code to perform the closing of the tickets and parse the log message. I had to modify this to pass in the Log message and the author (which the usernames I use in Trac match the usernames in SVN so that was easy).

CALL THE PYTHON SCRIPT
Python
"%~dp0\trac-post-commit-hook" -p
"%TRAC_ENV%" -r "%REV%" -u "%AUTHOR%"
-m "%LOG%"

The above line in the script will pass into the python script the Trac Environment, the revision, the person that made the commit, and their comment.

Here's the Python script that I used. One thing that I did additional to the regular script is we use a custom field (fixed_in_ver) which is used by our QA team to tell if the fix they're validating is in the version of code that they're testing in QA. So, I modified the code in the python script to update that field on the ticket. You can remove that code as you won't need it, but it's a good example of what you can do to update custom fields in Trac if you also want to do that.

I did that by having the users optionally include in their comment something like:

(version 2.1.2223.0)

I then use the same technique that the python script uses with regular expressions to get the information out. It wasn't too bad.

Anyway, here's the python script I used, Hopefully this is a good tutorial on exactly what I did to get it to work in the windows world so you all can leverage this in your own shop...

If you don't want to deal with my additional code for updating the custom field, get the base script from this location as mentioned by Craig above (Script From Edgewall)

#!/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"
# LOG=`/usr/bin/svnlook log -r $REV $REPOS`
# AUTHOR=`/usr/bin/svnlook author -r $REV $REPOS`
# TRAC_ENV='/somewhere/trac/project/'
# TRAC_URL='http://trac.mysite.com/project/'
#
# /usr/bin/python /usr/local/src/trac/contrib/trac-post-commit-hook \
#  -p "$TRAC_ENV"  \
#  -r "$REV"       \
#  -u "$AUTHOR"    \
#  -m "$LOG"       \
#  -s "$TRAC_URL"
#
# It searches commit messages for text in the form of:
#   command #1
#   command #1, #2
#   command #1 & #2 
#   command #1 and #2
#
# You can have more then one command in a message. The following commands
# are supported. There is more then one spelling for each command, to make
# this as user-friendly as possible.
#
#   closes, fixes
#     The specified issue numbers are closed with the contents of this
#     commit message being added to it. 
#   references, refs, addresses, re 
#     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
import time 

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.web.href import Href

try:
    from optparse import OptionParser
except ImportError:
    try:
        from optik import OptionParser
    except ImportError:
        raise ImportError, 'Requires Python 2.3 or the Optik option parsing library.'

parser = OptionParser()
parser.add_option('-e', '--require-envelope', dest='env', 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')
parser.add_option('-m', '--msg', dest='msg',
                  help='The log message to search.')
parser.add_option('-c', '--encoding', dest='encoding',
                  help='The encoding used by the log message.')
parser.add_option('-s', '--siteurl', dest='url',
                  help='The base URL to the project\'s trac website (to which '
                       '/ticket/## is appended).  If this is not specified, '
                       'the project URL from trac.ini will be used.')

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

if options.env:
    leftEnv = '\\' + options.env[0]
    rghtEnv = '\\' + options.env[1]
else:
    leftEnv = ''
    rghtEnv = ''

commandPattern = re.compile(leftEnv + r'(?P<action>[A-Za-z]*).?(?P<ticket>#[0-9]+(?:(?:[, &]*|[ ]?and[ ]?)#[0-9]+)*)' + rghtEnv)
ticketPattern = re.compile(r'#([0-9]*)')
versionPattern = re.compile(r"\(version[ ]+(?P<version>([0-9]+)\.([0-9]+)\.([0-9]+)\.([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, msg=options.msg, url=options.url,
                 encoding=options.encoding):
        msg = to_unicode(msg, encoding)
        self.author = author
        self.rev = rev
        self.msg = "(In [%s]) %s" % (rev, msg)
        self.now = int(time.time()) 
        self.env = open_environment(project)
        if url is None:
            url = self.env.config.get('project', 'url')
        self.env.href = Href(url)
        self.env.abs_href = Href(url)

        cmdGroups = commandPattern.findall(msg)


        tickets = {}

        for cmd, tkts in cmdGroups:
            funcname = CommitHook._supported_cmds.get(cmd.lower(), '')

            if funcname:

                for tkt_id in ticketPattern.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

                # get the version number from the checkin... and update the ticket with it.
                version = versionPattern.search(msg)
                if version != None and version.group("version") != None:
                    ticket['fixed_in_ver'] = version.group("version")

                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])
    else:
        CommitHook()
眼前雾蒙蒙 2024-07-11 08:14:22

本杰明的答案很接近,但在 Windows 上,您需要为挂钩脚本文件提供可执行扩展名,例如 .bat 或 .cmd。 我使用.cmd。 您可以采用模板脚本(unix shell 脚本、shell 脚本)并将它们转换为 .bat/.cmd 语法。

但要回答与 Trac 集成的问题,请按照以下步骤操作。

  1. 确保 Python.exe 位于系统路径上。 这将使您的生活更轻松。

  2. 在 \hooks 文件夹中创建 post-commit.cmd。 这是 Subversion 将在提交后事件上执行的实际挂钩脚本。

    <前><代码>@ECHO 关闭

    :: 提交后挂钩
    ::
    :: 提交后钩子在提交后调用。 Subversion 运行
    :: 通过调用程序(脚本、可执行文件、二进制文件等)来调用此钩子
    :: 命名为“post-commit”(此文件是其模板)
    :: 以下有序参数:
    ::
    :: [1] REPOS-PATH(此存储库的路径)
    :: [2] REV(刚刚提交的修订版本号)
    ::
    :: 调用的默认工作目录未定义,因此
    * 如果程序关心的话,应该明确设置一个。
    ::
    :: 因为提交已经完成并且无法撤消,
    :: 钩子程序的退出代码被忽略。 钩子程序
    :: 可以使用“svnlook”实用程序来帮助它检查
    :: 新提交的树。
    ::
    :: 在 Unix 系统上,正常的过程是“post-commit”
    :: 调用其他程序来完成实际工作,尽管它可能会执行
    :: 本身也起作用。
    ::
    :: 请注意,“提交后”必须可由将要执行此操作的用户执行
    :: 调用它(通常是用户 httpd 运行),并且该用户必须
    :: 具有访问存储库的文件系统级权限。
    ::
    :: 在Windows系统上,你应该给hook程序命名
    :: 'post-commit.bat' 或 'post-commit.exe',
    :: 但基本思想是相同的。
    ::
    :: 钩子程序通常不继承环境
    :: 它的父进程。 例如,一个常见的问题是
    :: PATH 环境变量未设置为其通常的值,因此
    :: 除非通过绝对路径调用,否则子程序无法启动。
    :: 如果您在使用挂钩程序时遇到意外问题,
    :: 罪魁祸首可能是异常(或丢失)的环境变量。
    ::
    :: 这是一个用于 Unix /bin/sh 解释器的示例挂钩脚本。
    :: 有关更多示例和预先编写的挂钩,请参阅
    :: Subversion 存储库位于
    :: http://svn.collab.net/repos/svn/trunk/tools/hook-scripts/ 和
    :: http://svn.collab.net/repos/svn/trunk/contrib/hook-scripts/

    设置本地

    :: 调试设置
    :: 1. 复制该文件。
    :: 2. 启用以下命令来调用复制的文件。
    :: 3.删除所有其他命令
    ::call %~dp0post-commit-run.cmd %* > %1/hooks/post-commit.log 2>&1

    :: 调用 Trac 提交后挂钩
    调用 %~dp0trac-post-commit.cmd %* || 1号出口

    终端本地

  3. 在 \hooks 文件夹中创建 trac-post-commit.cmd:

    <前><代码>@ECHO 关闭
    ::
    :: 适用于 Windows 的 Trac post-commit-hook 脚本
    ::
    * 由markus 提供,由CBOO 修改。

    :: 用法:
    ::
    :: 1) 在 post-commit.bat 脚本中插入以下行
    ::
    :: 调用 %~dp0\trac-post-commit-hook.cmd %1 %2
    ::
    :: 2) 检查下面的“修改路径”部分,确保至少设置 TRAC_ENV

    设置本地

    : ------------------------------------------------ ----------
    :: 修改路径:

    :: -- 这个*必须*设置
    SET TRAC_ENV=D:\projects\trac\membershipdnn

    :: -- 如果Python不在系统路径中则设置
    设置 PYTHON_PATH=

    :: -- 如果安装在非标准位置,则设置为包含 trac/ 的文件夹
    设置 TRAC_PATH=
    ::------------------------------------------------ ----------

    :: 如果trac环境不存在则不执行hook
    如果不存在%TRAC_ENV% 转到:EOF

    设置路径=%PYTHON_PATH%;%PATH%
    设置 PYTHONPATH=%TRAC_PATH%;%PYTHONPATH%

    设置转速=%2

    :: 解决票证引用(修复、关闭、引用等)
    Python "%~dp0trac-post-commit-resolve-ticket-ref.py" -p "%TRAC_ENV%" -r "%REV%"

    终端本地

  4. 在 \hooks 文件夹中创建 trac-post-commit-resolve-ticket-ref.py。 我使用了来自 EdgeWall 的相同脚本,只是我重命名了它以更好地阐明其目的。

Benjamin's answer is close, but on Windows you need to give the hook script files an executable extension, such as .bat or .cmd. I use .cmd. You can take the template scripts, which are unix shell scripts, shell scripts and convert them to .bat/.cmd syntax.

But to answer the question of integrating with Trac, follow these steps.

  1. Ensure that Python.exe is on the system path. This will make your life easier.

  2. Create post-commit.cmd in \hooks folder. This is the actual hook script that Subversion will execute on the post-commit event.

    @ECHO OFF
    
    :: POST-COMMIT HOOK
    ::
    :: The post-commit hook is invoked after a commit.  Subversion runs
    :: this hook by invoking a program (script, executable, binary, etc.)
    :: named 'post-commit' (for which this file is a template) with the 
    :: following ordered arguments:
    ::
    ::   [1] REPOS-PATH   (the path to this repository)
    ::   [2] REV          (the number of the revision just committed)
    ::
    :: The default working directory for the invocation is undefined, so
    :: the program should set one explicitly if it cares.
    ::
    :: Because the commit has already completed and cannot be undone,
    :: the exit code of the hook program is ignored.  The hook program
    :: can use the 'svnlook' utility to help it examine the
    :: newly-committed tree.
    ::
    :: On a Unix system, the normal procedure is to have 'post-commit'
    :: invoke other programs to do the real work, though it may do the
    :: work itself too.
    ::
    :: Note that 'post-commit' must be executable by the user(s) who will
    :: invoke it (typically the user httpd runs as), and that user must
    :: have filesystem-level permission to access the repository.
    ::
    :: On a Windows system, you should name the hook program
    :: 'post-commit.bat' or 'post-commit.exe',
    :: but the basic idea is the same.
    :: 
    :: The hook program typically does not inherit the environment of
    :: its parent process.  For example, a common problem is for the
    :: PATH environment variable to not be set to its usual value, so
    :: that subprograms fail to launch unless invoked via absolute path.
    :: If you're having unexpected problems with a hook program, the
    :: culprit may be unusual (or missing) environment variables.
    :: 
    :: Here is an example hook script, for a Unix /bin/sh interpreter.
    :: For more examples and pre-written hooks, see those in
    :: the Subversion repository at
    :: http://svn.collab.net/repos/svn/trunk/tools/hook-scripts/ and
    :: http://svn.collab.net/repos/svn/trunk/contrib/hook-scripts/
    
    setlocal
    
    :: Debugging setup
    :: 1. Make a copy of this file.
    :: 2. Enable the command below to call the copied file.
    :: 3. Remove all other commands
    ::call %~dp0post-commit-run.cmd %* > %1/hooks/post-commit.log 2>&1
    
    :: Call Trac post-commit hook
    call %~dp0trac-post-commit.cmd %* || exit 1
    
    endlocal
    
  3. Create trac-post-commit.cmd in \hooks folder:

    @ECHO OFF
    ::
    :: Trac post-commit-hook script for Windows
    ::
    :: Contributed by markus, modified by cboos.
    
    :: Usage:
    ::
    :: 1) Insert the following line in your post-commit.bat script
    ::
    :: call %~dp0\trac-post-commit-hook.cmd %1 %2
    ::
    :: 2) Check the 'Modify paths' section below, be sure to set at least TRAC_ENV
    
    setlocal
    
    :: ----------------------------------------------------------
    :: Modify paths here:
    
    :: -- this one *must* be set
    SET TRAC_ENV=D:\projects\trac\membershipdnn
    
    :: -- set if Python is not in the system path
    SET PYTHON_PATH=
    
    :: -- set to the folder containing trac/ if installed in a non-standard location
    SET TRAC_PATH=
    :: ----------------------------------------------------------
    
    :: Do not execute hook if trac environment does not exist
    IF NOT EXIST %TRAC_ENV% GOTO :EOF
    
    set PATH=%PYTHON_PATH%;%PATH%
    set PYTHONPATH=%TRAC_PATH%;%PYTHONPATH%
    
    SET REV=%2
    
    :: Resolve ticket references (fixes, closes, refs, etc.)
    Python "%~dp0trac-post-commit-resolve-ticket-ref.py" -p "%TRAC_ENV%" -r "%REV%"
    
    endlocal
    
  4. Create trac-post-commit-resolve-ticket-ref.py in \hooks folder. I used the same script from EdgeWall, only I renamed it to better clarify its purpose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文