annotate hgext/bugzilla.py @ 2192:2be3ac7abc21

add bugzilla integration hook. example of writing hook in python. hook updates bugzilla bugs when it sees commit comments that mention bug id, such as "i fixed bug 77". only bugzilla 2.16 supported yet, but easy to extend. bugzilla versions have different schema, i have not used later than 2.16.
author Vadim Gelfer <vadim.gelfer@gmail.com>
date Wed, 03 May 2006 14:40:39 -0700
parents
children 5de8b44f0446 afe24f5b7a9e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
1 # bugzilla.py - bugzilla integration for mercurial
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
2 #
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
3 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
4 #
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
5 # This software may be used and distributed according to the terms
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
6 # of the GNU General Public License, incorporated herein by reference.
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
7 #
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
8 # hook extension to update comments of bugzilla bugs when changesets
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
9 # that refer to bugs by id are seen. this hook does not change bug
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
10 # status, only comments.
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
11 #
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
12 # to configure, add items to '[bugzilla]' section of hgrc.
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
13 #
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
14 # to use, configure bugzilla extension and enable like this:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
15 #
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
16 # [extensions]
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
17 # hgext.bugzilla =
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
18 #
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
19 # [hooks]
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
20 # # run bugzilla hook on every change pulled or pushed in here
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
21 # incoming.bugzilla = python:hgext.bugzilla.hook
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
22 #
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
23 # config items:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
24 #
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
25 # REQUIRED:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
26 # host = bugzilla # mysql server where bugzilla database lives
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
27 # password = ** # user's password
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
28 # version = 2.16 # version of bugzilla installed
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
29 #
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
30 # OPTIONAL:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
31 # bzuser = ... # bugzilla user id to record comments with
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
32 # db = bugs # database to connect to
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
33 # hgweb = http:// # root of hg web site for browsing commits
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
34 # notify = ... # command to run to get bugzilla to send mail
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
35 # regexp = ... # regexp to match bug ids (must contain one "()" group)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
36 # strip = 0 # number of slashes to strip for url paths
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
37 # style = ... # style file to use when formatting comments
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
38 # template = ... # template to use when formatting comments
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
39 # timeout = 5 # database connection timeout (seconds)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
40 # user = bugs # user to connect to database as
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
41
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
42 from mercurial.demandload import *
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
43 from mercurial.i18n import gettext as _
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
44 from mercurial.node import *
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
45 demandload(globals(), 'cStringIO mercurial:templater,util os re time')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
46
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
47 try:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
48 import MySQLdb
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
49 except ImportError:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
50 raise util.Abort(_('python mysql support not available'))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
51
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
52 def buglist(ids):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
53 return '(' + ','.join(map(str, ids)) + ')'
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
54
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
55 class bugzilla_2_16(object):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
56 '''support for bugzilla version 2.16.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
57
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
58 def __init__(self, ui):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
59 self.ui = ui
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
60 host = self.ui.config('bugzilla', 'host', 'localhost')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
61 user = self.ui.config('bugzilla', 'user', 'bugs')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
62 passwd = self.ui.config('bugzilla', 'password')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
63 db = self.ui.config('bugzilla', 'db', 'bugs')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
64 timeout = int(self.ui.config('bugzilla', 'timeout', 5))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
65 self.ui.note(_('connecting to %s:%s as %s, password %s\n') %
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
66 (host, db, user, '*' * len(passwd)))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
67 self.conn = MySQLdb.connect(host=host, user=user, passwd=passwd,
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
68 db=db, connect_timeout=timeout)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
69 self.cursor = self.conn.cursor()
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
70 self.run('select fieldid from fielddefs where name = "longdesc"')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
71 ids = self.cursor.fetchall()
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
72 if len(ids) != 1:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
73 raise util.Abort(_('unknown database schema'))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
74 self.longdesc_id = ids[0][0]
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
75 self.user_ids = {}
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
76
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
77 def run(self, *args, **kwargs):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
78 '''run a query.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
79 self.ui.note(_('query: %s %s\n') % (args, kwargs))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
80 try:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
81 self.cursor.execute(*args, **kwargs)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
82 except MySQLdb.MySQLError, err:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
83 self.ui.note(_('failed query: %s %s\n') % (args, kwargs))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
84 raise
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
85
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
86 def filter_real_bug_ids(self, ids):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
87 '''filter not-existing bug ids from list.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
88 self.run('select bug_id from bugs where bug_id in %s' % buglist(ids))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
89 ids = [c[0] for c in self.cursor.fetchall()]
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
90 ids.sort()
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
91 return ids
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
92
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
93 def filter_unknown_bug_ids(self, node, ids):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
94 '''filter bug ids from list that already refer to this changeset.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
95
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
96 self.run('''select bug_id from longdescs where
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
97 bug_id in %s and thetext like "%%%s%%"''' %
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
98 (buglist(ids), short(node)))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
99 unknown = dict.fromkeys(ids)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
100 for (id,) in self.cursor.fetchall():
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
101 self.ui.status(_('bug %d already knows about changeset %s\n') %
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
102 (id, short(node)))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
103 unknown.pop(id, None)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
104 ids = unknown.keys()
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
105 ids.sort()
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
106 return ids
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
107
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
108 def notify(self, ids):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
109 '''tell bugzilla to send mail.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
110
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
111 self.ui.status(_('telling bugzilla to send mail:\n'))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
112 for id in ids:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
113 self.ui.status(_(' bug %s\n') % id)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
114 cmd = self.ui.config('bugzilla', 'notify',
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
115 'cd /var/www/html/bugzilla && '
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
116 './processmail %s nobody@nowhere.com') % id
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
117 fp = os.popen('(%s) 2>&1' % cmd)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
118 out = fp.read()
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
119 ret = fp.close()
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
120 if ret:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
121 self.ui.warn(out)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
122 raise util.Abort(_('bugzilla notify command %s') %
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
123 util.explain_exit(ret)[0])
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
124 self.ui.status(_('done\n'))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
125
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
126 def get_user_id(self, user):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
127 '''look up numeric bugzilla user id.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
128 try:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
129 return self.user_ids[user]
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
130 except KeyError:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
131 try:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
132 userid = int(user)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
133 except ValueError:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
134 self.ui.note(_('looking up user %s\n') % user)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
135 self.run('''select userid from profiles
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
136 where login_name like %s''', user)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
137 all = self.cursor.fetchall()
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
138 if len(all) != 1:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
139 raise KeyError(user)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
140 userid = int(all[0][0])
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
141 self.user_ids[user] = userid
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
142 return userid
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
143
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
144 def add_comment(self, bugid, text, prefuser):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
145 '''add comment to bug. try adding comment as committer of
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
146 changeset, otherwise as default bugzilla user.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
147 try:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
148 userid = self.get_user_id(prefuser)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
149 except KeyError:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
150 try:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
151 defaultuser = self.ui.config('bugzilla', 'bzuser')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
152 userid = self.get_user_id(defaultuser)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
153 except KeyError:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
154 raise util.Abort(_('cannot find user id for %s or %s') %
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
155 (prefuser, defaultuser))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
156 now = time.strftime('%Y-%m-%d %H:%M:%S')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
157 self.run('''insert into longdescs
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
158 (bug_id, who, bug_when, thetext)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
159 values (%s, %s, %s, %s)''',
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
160 (bugid, userid, now, text))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
161 self.run('''insert into bugs_activity (bug_id, who, bug_when, fieldid)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
162 values (%s, %s, %s, %s)''',
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
163 (bugid, userid, now, self.longdesc_id))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
164
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
165 class bugzilla(object):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
166 # supported versions of bugzilla. different versions have
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
167 # different schemas.
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
168 _versions = {
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
169 '2.16': bugzilla_2_16,
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
170 }
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
171
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
172 _default_bug_re = (r'bugs?\s*,?\s*(?:#|nos?\.?|num(?:ber)?s?)?\s*'
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
173 r'((?:\d+\s*(?:,?\s*(?:and)?)?\s*)+)')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
174
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
175 _bz = None
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
176
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
177 def __init__(self, ui, repo):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
178 self.ui = ui
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
179 self.repo = repo
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
180
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
181 def bz(self):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
182 '''return object that knows how to talk to bugzilla version in
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
183 use.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
184
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
185 if bugzilla._bz is None:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
186 bzversion = self.ui.config('bugzilla', 'version')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
187 try:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
188 bzclass = bugzilla._versions[bzversion]
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
189 except KeyError:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
190 raise util.Abort(_('bugzilla version %s not supported') %
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
191 bzversion)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
192 bugzilla._bz = bzclass(self.ui)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
193 return bugzilla._bz
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
194
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
195 def __getattr__(self, key):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
196 return getattr(self.bz(), key)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
197
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
198 _bug_re = None
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
199 _split_re = None
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
200
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
201 def find_bug_ids(self, node, desc):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
202 '''find valid bug ids that are referred to in changeset
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
203 comments and that do not already have references to this
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
204 changeset.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
205
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
206 if bugzilla._bug_re is None:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
207 bugzilla._bug_re = re.compile(
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
208 self.ui.config('bugzilla', 'regexp', bugzilla._default_bug_re),
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
209 re.IGNORECASE)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
210 bugzilla._split_re = re.compile(r'\D+')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
211 start = 0
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
212 ids = {}
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
213 while True:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
214 m = bugzilla._bug_re.search(desc, start)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
215 if not m:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
216 break
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
217 start = m.end()
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
218 for id in bugzilla._split_re.split(m.group(1)):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
219 ids[int(id)] = 1
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
220 ids = ids.keys()
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
221 if ids:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
222 ids = self.filter_real_bug_ids(ids)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
223 if ids:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
224 ids = self.filter_unknown_bug_ids(node, ids)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
225 return ids
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
226
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
227 def update(self, bugid, node, changes):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
228 '''update bugzilla bug with reference to changeset.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
229
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
230 def webroot(root):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
231 '''strip leading prefix of repo root and turn into
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
232 url-safe path.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
233 count = int(self.ui.config('bugzilla', 'strip', 0))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
234 root = util.pconvert(root)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
235 while count > 0:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
236 c = root.find('/')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
237 if c == -1:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
238 break
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
239 root = root[c+1:]
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
240 count -= 1
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
241 return root
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
242
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
243 class stringio(object):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
244 '''wrap cStringIO.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
245 def __init__(self):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
246 self.fp = cStringIO.StringIO()
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
247
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
248 def write(self, *args):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
249 for a in args:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
250 self.fp.write(a)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
251
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
252 write_header = write
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
253
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
254 def getvalue(self):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
255 return self.fp.getvalue()
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
256
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
257 mapfile = self.ui.config('bugzilla', 'style')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
258 tmpl = self.ui.config('bugzilla', 'template')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
259 sio = stringio()
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
260 t = templater.changeset_templater(self.ui, self.repo, mapfile, sio)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
261 if not mapfile and not tmpl:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
262 tmpl = _('changeset {node|short} in repo {root} refers '
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
263 'to bug {bug}.\ndetails:\n\t{desc|tabindent}')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
264 if tmpl:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
265 tmpl = templater.parsestring(tmpl, quoted=False)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
266 t.use_template(tmpl)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
267 t.show(changenode=node, changes=changes,
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
268 bug=str(bugid),
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
269 hgweb=self.ui.config('bugzilla', 'hgweb'),
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
270 root=self.repo.root,
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
271 webroot=webroot(self.repo.root))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
272 self.add_comment(bugid, sio.getvalue(), templater.email(changes[1]))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
273
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
274 def hook(ui, repo, hooktype, node=None, **kwargs):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
275 '''add comment to bugzilla for each changeset that refers to a
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
276 bugzilla bug id. only add a comment once per bug, so same change
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
277 seen multiple times does not fill bug with duplicate data.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
278 if node is None:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
279 raise util.Abort(_('hook type %s does not pass a changeset id') %
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
280 hooktype)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
281 try:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
282 bz = bugzilla(ui, repo)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
283 bin_node = bin(node)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
284 changes = repo.changelog.read(bin_node)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
285 ids = bz.find_bug_ids(bin_node, changes[4])
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
286 if ids:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
287 for id in ids:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
288 bz.update(id, bin_node, changes)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
289 bz.notify(ids)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
290 return True
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
291 except MySQLdb.MySQLError, err:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
292 raise util.Abort(_('database error: %s') % err[1])
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
293