hgext/bugzilla.py
changeset 2306 4c67ba93560b
parent 2239 5e5adc1910ed
child 3425 e4452c3fa736
equal deleted inserted replaced
2305:e7de4dd43472 2306:4c67ba93560b
    19 #   [hooks]
    19 #   [hooks]
    20 #   # run bugzilla hook on every change pulled or pushed in here
    20 #   # run bugzilla hook on every change pulled or pushed in here
    21 #   incoming.bugzilla = python:hgext.bugzilla.hook
    21 #   incoming.bugzilla = python:hgext.bugzilla.hook
    22 #
    22 #
    23 # config items:
    23 # config items:
       
    24 #
       
    25 # section name is 'bugzilla'.
       
    26 #  [bugzilla]
    24 #
    27 #
    25 # REQUIRED:
    28 # REQUIRED:
    26 #   host = bugzilla # mysql server where bugzilla database lives
    29 #   host = bugzilla # mysql server where bugzilla database lives
    27 #   password = **   # user's password
    30 #   password = **   # user's password
    28 #   version = 2.16  # version of bugzilla installed
    31 #   version = 2.16  # version of bugzilla installed
    29 #
    32 #
    30 # OPTIONAL:
    33 # OPTIONAL:
    31 #   bzuser = ...    # bugzilla user id to record comments with
    34 #   bzuser = ...    # fallback bugzilla user name to record comments with
    32 #   db = bugs       # database to connect to
    35 #   db = bugs       # database to connect to
    33 #   notify = ...    # command to run to get bugzilla to send mail
    36 #   notify = ...    # command to run to get bugzilla to send mail
    34 #   regexp = ...    # regexp to match bug ids (must contain one "()" group)
    37 #   regexp = ...    # regexp to match bug ids (must contain one "()" group)
    35 #   strip = 0       # number of slashes to strip for url paths
    38 #   strip = 0       # number of slashes to strip for url paths
    36 #   style = ...     # style file to use when formatting comments
    39 #   style = ...     # style file to use when formatting comments
    37 #   template = ...  # template to use when formatting comments
    40 #   template = ...  # template to use when formatting comments
    38 #   timeout = 5     # database connection timeout (seconds)
    41 #   timeout = 5     # database connection timeout (seconds)
    39 #   user = bugs     # user to connect to database as
    42 #   user = bugs     # user to connect to database as
    40 #   [web]
    43 #   [web]
    41 #   baseurl = http://hgserver/... # root of hg web site for browsing commits
    44 #   baseurl = http://hgserver/... # root of hg web site for browsing commits
       
    45 #
       
    46 # if hg committer names are not same as bugzilla user names, use
       
    47 # "usermap" feature to map from committer email to bugzilla user name.
       
    48 # usermap can be in hgrc or separate config file.
       
    49 #
       
    50 #   [bugzilla]
       
    51 #   usermap = filename # cfg file with "committer"="bugzilla user" info
       
    52 #   [usermap]
       
    53 #   committer_email = bugzilla_user_name
    42 
    54 
    43 from mercurial.demandload import *
    55 from mercurial.demandload import *
    44 from mercurial.i18n import gettext as _
    56 from mercurial.i18n import gettext as _
    45 from mercurial.node import *
    57 from mercurial.node import *
    46 demandload(globals(), 'mercurial:templater,util os re time')
    58 demandload(globals(), 'mercurial:templater,util os re time')
    58         host = self.ui.config('bugzilla', 'host', 'localhost')
    70         host = self.ui.config('bugzilla', 'host', 'localhost')
    59         user = self.ui.config('bugzilla', 'user', 'bugs')
    71         user = self.ui.config('bugzilla', 'user', 'bugs')
    60         passwd = self.ui.config('bugzilla', 'password')
    72         passwd = self.ui.config('bugzilla', 'password')
    61         db = self.ui.config('bugzilla', 'db', 'bugs')
    73         db = self.ui.config('bugzilla', 'db', 'bugs')
    62         timeout = int(self.ui.config('bugzilla', 'timeout', 5))
    74         timeout = int(self.ui.config('bugzilla', 'timeout', 5))
       
    75         usermap = self.ui.config('bugzilla', 'usermap')
       
    76         if usermap:
       
    77             self.ui.readconfig(usermap)
    63         self.ui.note(_('connecting to %s:%s as %s, password %s\n') %
    78         self.ui.note(_('connecting to %s:%s as %s, password %s\n') %
    64                      (host, db, user, '*' * len(passwd)))
    79                      (host, db, user, '*' * len(passwd)))
    65         self.conn = MySQLdb.connect(host=host, user=user, passwd=passwd,
    80         self.conn = MySQLdb.connect(host=host, user=user, passwd=passwd,
    66                                     db=db, connect_timeout=timeout)
    81                                     db=db, connect_timeout=timeout)
    67         self.cursor = self.conn.cursor()
    82         self.cursor = self.conn.cursor()
   137                     raise KeyError(user)
   152                     raise KeyError(user)
   138                 userid = int(all[0][0])
   153                 userid = int(all[0][0])
   139             self.user_ids[user] = userid
   154             self.user_ids[user] = userid
   140             return userid
   155             return userid
   141 
   156 
   142     def add_comment(self, bugid, text, prefuser):
   157     def map_committer(self, user):
       
   158         '''map name of committer to bugzilla user name.'''
       
   159         for committer, bzuser in self.ui.configitems('usermap'):
       
   160             if committer.lower() == user.lower():
       
   161                 return bzuser
       
   162         return user
       
   163 
       
   164     def add_comment(self, bugid, text, committer):
   143         '''add comment to bug. try adding comment as committer of
   165         '''add comment to bug. try adding comment as committer of
   144         changeset, otherwise as default bugzilla user.'''
   166         changeset, otherwise as default bugzilla user.'''
       
   167         user = self.map_committer(committer)
   145         try:
   168         try:
   146             userid = self.get_user_id(prefuser)
   169             userid = self.get_user_id(user)
   147         except KeyError:
   170         except KeyError:
   148             try:
   171             try:
   149                 defaultuser = self.ui.config('bugzilla', 'bzuser')
   172                 defaultuser = self.ui.config('bugzilla', 'bzuser')
       
   173                 if not defaultuser:
       
   174                     raise util.Abort(_('cannot find bugzilla user id for %s') %
       
   175                                      user)
   150                 userid = self.get_user_id(defaultuser)
   176                 userid = self.get_user_id(defaultuser)
   151             except KeyError:
   177             except KeyError:
   152                 raise util.Abort(_('cannot find user id for %s or %s') %
   178                 raise util.Abort(_('cannot find bugzilla user id for %s or %s') %
   153                                  (prefuser, defaultuser))
   179                                  (user, defaultuser))
   154         now = time.strftime('%Y-%m-%d %H:%M:%S')
   180         now = time.strftime('%Y-%m-%d %H:%M:%S')
   155         self.run('''insert into longdescs
   181         self.run('''insert into longdescs
   156                     (bug_id, who, bug_when, thetext)
   182                     (bug_id, who, bug_when, thetext)
   157                     values (%s, %s, %s, %s)''',
   183                     values (%s, %s, %s, %s)''',
   158                  (bugid, userid, now, text))
   184                  (bugid, userid, now, text))