tPc@svdZddlmZddlmZddlmZmZmZm Z ddl Z ddl Z ddl Z ddl Z dZdefdYZd efd YZd efd YZd efdYZdefdYZdee jfdYZdee jfdYZdefdYZdefdYZdefdYZddZdS(s>'hooks for integrating with the Bugzilla bug tracker This hook extension adds comments on bugs in Bugzilla when changesets that refer to bugs by Bugzilla ID are seen. The comment is formatted using the Mercurial template mechanism. The bug references can optionally include an update for Bugzilla of the hours spent working on the bug. Bugs can also be marked fixed. Three basic modes of access to Bugzilla are provided: 1. Access via the Bugzilla XMLRPC interface. Requires Bugzilla 3.4 or later. 2. Check data via the Bugzilla XMLRPC interface and submit bug change via email to Bugzilla email interface. Requires Bugzilla 3.4 or later. 3. Writing directly to the Bugzilla database. Only Bugzilla installations using MySQL are supported. Requires Python MySQLdb. Writing directly to the database is susceptible to schema changes, and relies on a Bugzilla contrib script to send out bug change notification emails. This script runs as the user running Mercurial, must be run on the host with the Bugzilla install, and requires permission to read Bugzilla configuration details and the necessary MySQL user and password to have full access rights to the Bugzilla database. For these reasons this access mode is now considered deprecated, and will not be updated for new Bugzilla versions going forward. Only adding comments is supported in this access mode. Access via XMLRPC needs a Bugzilla username and password to be specified in the configuration. Comments are added under that username. Since the configuration must be readable by all Mercurial users, it is recommended that the rights of that user are restricted in Bugzilla to the minimum necessary to add comments. Marking bugs fixed requires Bugzilla 4.0 and later. Access via XMLRPC/email uses XMLRPC to query Bugzilla, but sends email to the Bugzilla email interface to submit comments to bugs. The From: address in the email is set to the email address of the Mercurial user, so the comment appears to come from the Mercurial user. In the event that the Mercurial user email is not recognized by Bugzilla as a Bugzilla user, the email associated with the Bugzilla username used to log into Bugzilla is used instead as the source of the comment. Marking bugs fixed works on all supported Bugzilla versions. Configuration items common to all access modes: bugzilla.version The access type to use. Values recognized are: :``xmlrpc``: Bugzilla XMLRPC interface. :``xmlrpc+email``: Bugzilla XMLRPC and email interfaces. :``3.0``: MySQL access, Bugzilla 3.0 and later. :``2.18``: MySQL access, Bugzilla 2.18 and up to but not including 3.0. :``2.16``: MySQL access, Bugzilla 2.16 and up to but not including 2.18. bugzilla.regexp Regular expression to match bug IDs for update in changeset commit message. It must contain one "()" named group ```` containing the bug IDs separated by non-digit characters. It may also contain a named group ```` with a floating-point number giving the hours worked on the bug. If no named groups are present, the first "()" group is assumed to contain the bug IDs, and work time is not updated. The default expression matches ``Bug 1234``, ``Bug no. 1234``, ``Bug number 1234``, ``Bugs 1234,5678``, ``Bug 1234 and 5678`` and variations thereof, followed by an hours number prefixed by ``h`` or ``hours``, e.g. ``hours 1.5``. Matching is case insensitive. bugzilla.fixregexp Regular expression to match bug IDs for marking fixed in changeset commit message. This must contain a "()" named group ``` containing the bug IDs separated by non-digit characters. It may also contain a named group ```` with a floating-point number giving the hours worked on the bug. If no named groups are present, the first "()" group is assumed to contain the bug IDs, and work time is not updated. The default expression matches ``Fixes 1234``, ``Fixes bug 1234``, ``Fixes bugs 1234,5678``, ``Fixes 1234 and 5678`` and variations thereof, followed by an hours number prefixed by ``h`` or ``hours``, e.g. ``hours 1.5``. Matching is case insensitive. bugzilla.fixstatus The status to set a bug to when marking fixed. Default ``RESOLVED``. bugzilla.fixresolution The resolution to set a bug to when marking fixed. Default ``FIXED``. bugzilla.style The style file to use when formatting comments. bugzilla.template Template to use when formatting comments. Overrides style if specified. In addition to the usual Mercurial keywords, the extension specifies: :``{bug}``: The Bugzilla bug ID. :``{root}``: The full pathname of the Mercurial repository. :``{webroot}``: Stripped pathname of the Mercurial repository. :``{hgweb}``: Base URL for browsing Mercurial repositories. Default ``changeset {node|short} in repo {root} refers to bug {bug}.\ndetails:\n\t{desc|tabindent}`` bugzilla.strip The number of path separator characters to strip from the front of the Mercurial repository path (``{root}`` in templates) to produce ``{webroot}``. For example, a repository with ``{root}`` ``/var/local/my-project`` with a strip of 2 gives a value for ``{webroot}`` of ``my-project``. Default 0. web.baseurl Base URL for browsing Mercurial repositories. Referenced from templates as ``{hgweb}``. Configuration items common to XMLRPC+email and MySQL access modes: bugzilla.usermap Path of file containing Mercurial committer email to Bugzilla user email mappings. If specified, the file should contain one mapping per line:: committer = Bugzilla user See also the ``[usermap]`` section. The ``[usermap]`` section is used to specify mappings of Mercurial committer email to Bugzilla user email. See also ``bugzilla.usermap``. Contains entries of the form ``committer = Bugzilla user``. XMLRPC access mode configuration: bugzilla.bzurl The base URL for the Bugzilla installation. Default ``http://localhost/bugzilla``. bugzilla.user The username to use to log into Bugzilla via XMLRPC. Default ``bugs``. bugzilla.password The password for Bugzilla login. XMLRPC+email access mode uses the XMLRPC access mode configuration items, and also: bugzilla.bzemail The Bugzilla email address. In addition, the Mercurial email settings must be configured. See the documentation in hgrc(5), sections ``[email]`` and ``[smtp]``. MySQL access mode configuration: bugzilla.host Hostname of the MySQL server holding the Bugzilla database. Default ``localhost``. bugzilla.db Name of the Bugzilla database in MySQL. Default ``bugs``. bugzilla.user Username to use to access MySQL server. Default ``bugs``. bugzilla.password Password to use to access MySQL server. bugzilla.timeout Database connection timeout (seconds). Default 5. bugzilla.bzuser Fallback Bugzilla user name to record comments with, if changeset committer cannot be found as a Bugzilla user. bugzilla.bzdir Bugzilla install directory. Used by default notify. Default ``/var/www/html/bugzilla``. bugzilla.notify The command to run to get Bugzilla to send bug change notification emails. Substitutes from a map with 3 keys, ``bzdir``, ``id`` (bug id) and ``user`` (committer bugzilla email). Default depends on version; from 2.18 it is "cd %(bzdir)s && perl -T contrib/sendbugmail.pl %(id)s %(user)s". Activating the extension:: [extensions] bugzilla = [hooks] # run bugzilla hook on every change pulled or pushed in here incoming.bugzilla = python:hgext.bugzilla.hook Example configurations: XMLRPC example configuration. This uses the Bugzilla at ``http://my-project.org/bugzilla``, logging in as user ``bugmail@my-project.org`` with password ``plugh``. It is used with a collection of Mercurial repositories in ``/var/local/hg/repos/``, with a web interface at ``http://my-project.org/hg``. :: [bugzilla] bzurl=http://my-project.org/bugzilla user=bugmail@my-project.org password=plugh version=xmlrpc template=Changeset {node|short} in {root|basename}. {hgweb}/{webroot}/rev/{node|short}\n {desc}\n strip=5 [web] baseurl=http://my-project.org/hg XMLRPC+email example configuration. This uses the Bugzilla at ``http://my-project.org/bugzilla``, logging in as user ``bugmail@my-project.org`` with password ``plugh``. It is used with a collection of Mercurial repositories in ``/var/local/hg/repos/``, with a web interface at ``http://my-project.org/hg``. Bug comments are sent to the Bugzilla email address ``bugzilla@my-project.org``. :: [bugzilla] bzurl=http://my-project.org/bugzilla user=bugmail@my-project.org password=plugh version=xmlrpc bzemail=bugzilla@my-project.org template=Changeset {node|short} in {root|basename}. {hgweb}/{webroot}/rev/{node|short}\n {desc}\n strip=5 [web] baseurl=http://my-project.org/hg [usermap] user@emaildomain.com=user.name@bugzilladomain.com MySQL example configuration. This has a local Bugzilla 3.2 installation in ``/opt/bugzilla-3.2``. The MySQL database is on ``localhost``, the Bugzilla database name is ``bugs`` and MySQL is accessed with MySQL username ``bugs`` password ``XYZZY``. It is used with a collection of Mercurial repositories in ``/var/local/hg/repos/``, with a web interface at ``http://my-project.org/hg``. :: [bugzilla] host=localhost password=XYZZY version=3.0 bzuser=unknown@domain.com bzdir=/opt/bugzilla-3.2 template=Changeset {node|short} in {root|basename}. {hgweb}/{webroot}/rev/{node|short}\n {desc}\n strip=5 [web] baseurl=http://my-project.org/hg [usermap] user@emaildomain.com=user.name@bugzilladomain.com All the above add a comment to the Bugzilla bug record of the form:: Changeset 3b16791d6642 in repository-name. http://my-project.org/hg/repository-name/rev/3b16791d6642 Changeset commit comment. Bug 1234. i(t_(tshort(tcmdutiltmailt templatertutilNtinternaltbzaccesscBsDeZdZdZdZdZdZdZdZRS(s"Base class for access to Bugzilla.cCsD||_|jjdd}|r@|jj|ddgndS(Ntbugzillatusermaptsections(tuitconfigt readconfig(tselfR R ((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyt__init__"s cCsCx<|jjdD](\}}|j|jkr|SqW|S(s,map name of committer to Bugzilla user name.R (R t configitemstlower(Rtusert committertbzuser((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyt map_committer(scCsdS(s7remove bug IDs that do not exist in Bugzilla from bugs.N((Rtbugs((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pytfilter_real_bug_ids7scCsdS(s;remove bug IDs where node occurs in comment text from bugs.N((RtnodeR((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pytfilter_cset_known_bug_ids;scCsdS(supdate the specified bug. Add comment text and set new states. If possible add the comment as being from the committer of the changeset. Otherwise use the default Bugzilla user. N((RtbugidtnewstatettextR((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyt updatebug?scCsdS(sForce sending of Bugzilla notification emails. Only required if the access method does not trigger notification emails automatically. N((RRR((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pytnotifyGs( t__name__t __module__t__doc__RRRRRR(((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyRs     tbzmysqlcBsteZdZedZd ZdZdZdZ dZ dZ dZ dZ d Zd ZRS( sSupport for direct MySQL access to Bugzilla. The earliest Bugzilla version this is tested with is version 2.16. If your Bugzilla is version 3.4 or above, you are strongly recommended to use the XMLRPC access method instead. cCsddjtt|dS(s#return SQL-friendly list of bug idst(t,t)(tjointmaptstr(tids((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyt sql_buglistYsc Csjyddl}|t_Wn,tk rG}tjtd|nXtj|||j j ddd}|j j ddd}|j j dd}|j j dd d}t |j j dd d }|j j td |||d t |ftjjd|d|d|d |d||_|jj|_|j|_i|_d|_dS(Nis&python mysql support not available: %sRthostt localhostRRtpasswordtdbttimeoutis'connecting to %s:%s as %s, password %s t*tpasswdtconnect_timeouts-cd %(bzdir)s && ./processmail %(id)s %(user)s(tMySQLdbR"t_MySQLdbt ImportErrorRtAbortRRRR R tinttnotetlentconnecttconntcursortget_longdesc_idt longdesc_idtuser_idstdefault_notify( RR tmysqlterrR+RR1R.R/((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyR`s*     cOsx|jjtd||fy|jj||Wn:tjjk rs|jjtd||fnXdS(s run a query.s query: %s %s sfailed query: %s %s N(R R8RR<texecuteR"R4t MySQLError(Rtargstkwargs((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pytrunys   cCsR|jd|jj}t|dkrFtjtdn|ddS(sget identity of longdesc fields5select fieldid from fielddefs where name = "longdesc"isunknown database schemai(RGR<tfetchallR9RR6R(RR)((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyR=s  cCs|jdtj|jg|jjD]\}|^q0}xD|jD]6}||krR|jjtd|||=qRqRWdS(s"filter not-existing bugs from set.s*select bug_id from bugs where bug_id in %ssbug %d does not exist N( RGR"R*tkeysR<RHR tstatusR(RRtidtexisting((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyRs % cCsz|jdtj|jt|fxG|jjD]6\}|jjt d|t|f||=q<WdS(s=filter bug ids that already refer to this changeset from set.s]select bug_id from longdescs where bug_id in %s and thetext like "%%%s%%"s(bug %d already knows about changeset %s N( RGR"R*RIRR<RHR RJR(RRRRK((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyRs#c Csn|jjtd|j|\}}x&|jD]}|jjtd||jjdd|j}|jjddd}y||}Wn0tk r|i|d6|d6|d6}nX|jjtd |t j d |} | j } | j } | r8|jj | t jtd t j| d q8q8W|jjtd dS(stell bugzilla to send mail.stelling bugzilla to send mail: s bug %s RRtbzdirs/var/www/html/bugzillaRKRsrunning notify command %s s (%s) 2>&1sbugzilla notify command %sisdone N(R RJRtget_bugzilla_userRIR R@t TypeErrorR8RtpopentreadtclosetwarnR6t explainexit( RRRRtuseridRKtcmdfmtRMtcmdtfptouttret((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyRs(  #  cCsy|j|SWntk ryt|}Wntk r|jjtd||jd||jj }t |dkrt|nt|dd}nX||j|<|SXdS(s!look up numeric bugzilla user id.slooking up user %s sPselect userid from profiles where login_name like %siiN( R?tKeyErrorR7t ValueErrorR R8RRGR<RHR9(RRRUtall((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyt get_user_ids    cCs|j|}y|j|}Wntk ryP|jjdd}|sltjtd|n|j|}|}Wqtk rtjtd||fqXnX||fS(sSee if committer is a registered bugzilla user. Return bugzilla username and userid if so. If not, return default bugzilla username and userid.RRs#cannot find bugzilla user id for %ss)cannot find bugzilla user id for %s or %s(RR^R[R R RR6R(RRRRUt defaultuser((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyRNs    cCst|dkr+|jjtdn|j|\}}tjd}|jd||||f|jd||||jf|j j dS(supdate bug state with comment text. Try adding comment as committer of changeset, otherwise as default bugzilla user.is'Bugzilla/MySQL cannot update bug state s%Y-%m-%d %H:%M:%Ssvinsert into longdescs (bug_id, who, bug_when, thetext) values (%s, %s, %s, %s)sfinsert into bugs_activity (bug_id, who, bug_when, fieldid) values (%s, %s, %s, %s)N( R9R RSRRNttimetstrftimeRGR>R;tcommit(RRRRRRRUtnow((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyRsN(RR R!t staticmethodR*tNoneR4RRGR=RRRR^RNR(((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyR"Ps     t bzmysql_2_18cBseZdZdZRS(s!support for bugzilla 2.18 series.cCstj||d|_dS(Ns>cd %(bzdir)s && perl -T contrib/sendbugmail.pl %(id)s %(user)s(R"RR@(RR ((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyRs(RR R!R(((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyRfst bzmysql_3_0cBs eZdZdZdZRS(s support for bugzilla 3.0 series.cCstj||dS(N(RfR(RR ((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyRscCsR|jd|jj}t|dkrFtjtdn|ddS(sget identity of longdesc fields0select id from fielddefs where name = "longdesc"isunknown database schemai(RGR<RHR9RR6R(RR)((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyR=s  (RR R!RR=(((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyRgs tcookietransportrequestcBs)eZdZgZdZddZRS(sA Transport request method that retains cookies over its lifetime. The regular xmlrpclib transports ignore cookies. Which causes a bit of a problem when you need a cookie-based login, as with the Bugzilla XMLRPC interface. So this is a helper for defining a Transport which looks for cookies being set in responses and saves them to add to all future requests. cCs4|jr0x$|jD]}|jd|qWndS(NtCookie(tcookiest putheader(Rt connectiontcookie((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyt send_cookiess ic Cs||_t|_|j|}|r7|jdn|j||||j|||j||j||j ||y|j }Wn t k r|j j }nXxV|j jdD]B}|jddd}|jddd} |jj| qW|jdkrJtj|||j|j|j jn|j} |j\} } | j| | j| jS(Nis Set-Cookies: t;ii(tverbosetFalsetaccept_gzip_encodingtmake_connectiontset_debuglevelt send_requestt send_hostRntsend_user_agentt send_contentt getresponsetAttributeErrort_conntmsgtgetallmatchingheaderstsplitRjtappendRJt xmlrpclibt ProtocolErrortreasontheadersRQt getparsertfeedRR( RR+thandlert request_bodyRpthtresponsetheadertvalRmtpayloadtparsert unmarshaller((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pytrequests4        (RR R!RjRnR(((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyRh s  tcookietransportcBseZddZRS(icCs/tjtjdr+tjj||ndS(NR(Rt safehasattrRt TransportR(Rt use_datetime((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyRNs(RR R(((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyRMstcookiesafetransportcBseZddZRS(icCs/tjtjdr+tjj||ndS(NR(RRRRt SafeTransportR(RR((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyRSs(RR R(((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyRRstbzxmlrpccBsDeZdZdZdZdZdZdZdZRS(snSupport for access to Bugzilla via the Bugzilla XMLRPC API. Requires a minimum Bugzilla version 3.4. cCs'tj|||jjddd}|jdd}|jjddd}|jjdd}|jjdd d |_|jjdd d |_tj||j ||_ |j j j d j d}t|d|_t|d|_|j jjtd|d|dS(NRtbzurlshttp://localhost/bugzilla/t/s /xmlrpc.cgiRRR-t fixstatustRESOLVEDt fixresolutiontFIXEDtversiont.iitlogin(RRR R trstripRRRt ServerProxyt transporttbzproxytBugzillaRR~R7t bzvermajort bzverminortUserRtdict(RR tbzwebRR1tver((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyR]s  cCs.tj|dddkr#tStSdS(Nthttpithttps(turlparseRR(Rturi((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyRqscCs`|jjjtd|gddg}djg|dt|dD]}|d^qIS(s0Return a string with all comment text for a bug.R)tinclude_fieldsRtRtcomments(RtBugRRR&R((RRKtctt((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pytget_bug_commentsws*cCsz|jjjtdt|jdgdt}x=|dD]1}|d}|jjt d|||=qAWdS(NR)Rt permissivetfaultsRKsbug %d does not exist ( RRtgetRtsortedRItTrueR RJR(RRtprobetbadbugRK((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyR|s$  cCsuxnt|jD]Z}|j|jt|dkr|jjtd|t|f||=qqWdS(Nis(bug %d already knows about changeset %s (RRIRtfindRR RJR(RRRRK((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyRs $cCsi}d|kr#|d|d(?:\d+\s*(?:,?\s*(?:and)?)?\s*)+)\.?\s*(?:h(?:ours?)?\s*(?P\d*(?:\.\d+)?))?sfix(?:es)?\s*(?:bugs?\s*)?,?\s*(?:nos?\.?|num(?:ber)?s?)?\s*(?P(?:#?\d+\s*(?:,?\s*(?:and)?)?\s*)+)\.?\s*(?:h(?:ours?)?\s*(?P\d*(?:\.\d+)?))?cCs||_||_dS(N(R trepo(RR R((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyRs cCs~tjdkrw|jjdd}ytj|}Wn*tk ratjt d|nX||jt_ntjS(sHreturn object that knows how to talk to bugzilla version in use.RRs!bugzilla version %s not supportedN( Rt_bzReR R t _versionsR[RR6R(Rt bzversiontbzclass((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pytbz s cCst|j|S(N(tgetattrR(Rtkey((s2/sys/lib/python2.7/site-packages/hgext/bugzilla.pyt __getattr__sc Cstjd kr~tj|jjddtjtjt_tj|jjddtj tjt_ tjdt_ nd}d}i}tjj |j |}tj j |j |}xtri}| r| rPn|s|}n6|s|}n'|j|jkr&|}n|}|j}||krxtjj |j |}d|kr|d=qn%tj j |j |}d |ds "01DIO