\x89\x50\x4E\x47\x0D\x0A\x1A\x0A PNG  \x89\x50\x4E\x47\x0D\x0A\x1A\x0A  13\c@@sedZddlmZddlZddlZddlZddlmZddlm Z ddl m Z m Z m Z mZmZmZddlmZmZejZdZiZejeZed d d d ed d d ded dd ded dd ded dd ded dd ded dd ded dd ded dd ded dd ded dd ejed dd ded dd d ed d!d ded d"d ded d#d ded d$d d%ed d&d ded d'd ded d(d dd)efd*YZd+efd,YZd-efd.YZd/efd0YZd1efd2YZd3eej fd4YZ!d5eej"fd6YZ#d7efd8YZ$d9e$fd:YZ%d;e&fd<YZ'd=efd>YZ(d efd?YZ)dd@Z*dS(As)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. Four basic modes of access to Bugzilla are provided: 1. Access via the Bugzilla REST-API. Requires bugzilla 5.0 or later. 2. Access via the Bugzilla XMLRPC interface. Requires Bugzilla 3.4 or later. 3. Check data via the Bugzilla XMLRPC interface and submit bug change via email to Bugzilla email interface. Requires Bugzilla 3.4 or later. 4. 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. Access via the REST-API needs either a Bugzilla username and password or an apikey specified in the configuration. Comments are made under the given username or the user associated with the apikey in Bugzilla. Configuration items common to all access modes: bugzilla.version The access type to use. Values recognized are: :``restapi``: Bugzilla REST-API, Bugzilla 5.0 and later. :``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 and REST-API 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. REST-API access mode uses the options listed above as well as: bugzilla.apikey An apikey generated on the Bugzilla instance for api access. Using an apikey removes the need to store the user and password options. 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+email 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(tabsolute_importN(t_(tshort(terrort logcmdutiltmailt registrarturltutil(tprocutilt stringutilsships-with-hg-coretbugzillatapikeytdefaultttbzdirs/var/www/html/bugzillatbzemailtbzurlshttp://localhost/bugzilla/tbzusertdbtbugst fixregexpsfix(?:es)?\s*(?:bugs?\s*)?,?\s*(?:nos?\.?|num(?:ber)?s?)?\s*(?P(?:#?\d+\s*(?:,?\s*(?:and)?)?\s*)+)\.?\s*(?:h(?:ours?)?\s*(?P\d*(?:\.\d+)?))?t fixresolutiontFIXEDt fixstatustRESOLVEDthostt localhosttnotifytpasswordtregexpsbugs?\s*,?\s*(?:#|nos?\.?|num(?:ber)?s?)?\s*(?P(?:\d+\s*(?:,?\s*(?:and)?)?\s*)+)\.?\s*(?:h(?:ours?)?\s*(?P\d*(?:\.\d+)?))?tstriptstylettemplatettimeoutitusertusermaptversiontbzaccesscB@sDeZdZdZdZdZdZdZdZRS(s"Base class for access to Bugzilla.cC@sD||_|jjdd}|r@|jj|ddgndS(NR R$tsections(tuitconfigt readconfig(tselfR(R$((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyt__init__s cC@sCx<|jjdD](\}}|j|jkr|SqW|S(s,map name of committer to Bugzilla user name.R$(R(t configitemstlower(R+R#t committerR((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyt map_committerscC@sdS(s7remove bug IDs that do not exist in Bugzilla from bugs.N((R+R((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pytfilter_real_bug_idsscC@sdS(s;remove bug IDs where node occurs in comment text from bugs.N((R+tnodeR((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pytfilter_cset_known_bug_idsscC@sdS(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((R+tbugidtnewstatettextR/((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyt updatebugscC@sdS(sForce sending of Bugzilla notification emails. Only required if the access method does not trigger notification emails automatically. N((R+RR/((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyRs( t__name__t __module__t__doc__R,R0R1R3R7R(((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyR&s     tbzmysqlcB@steZdZedZd 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. cC@sddjtt|dS(s#return SQL-friendly list of bug idst(t,t)(tjointmaptstr(tids((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyt sql_buglistsc C@s^yddl}|t_Wn,tk rG}tjtd|nXtj|||j j dd}|j j dd}|j j dd}|j j dd}t |j j dd}|j j td |||d t |ftjjd|d|d |d|d ||_|jj|_|j|_i|_d |_dS(Nis&python mysql support not available: %sR RR#RRR"s'connecting to %s:%s as %s, password %s t*tpasswdtconnect_timeouts-cd %(bzdir)s && ./processmail %(id)s %(user)s(tMySQLdbR;t_MySQLdbt ImportErrorRtAbortRR&R,R(R)tinttnotetlentconnecttconntcursortget_longdesc_idt longdesc_idtuser_idstdefault_notify( R+R(tmysqlterrRR#RERR"((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyR,s*     cO@sx|jjtd||fy|jj||Wn:tjjk rs|jjtd||fnXdS(s run a query.s query: %s %s sfailed query: %s %s N(R(RLRRPtexecuteR;RHt MySQLError(R+targstkwargs((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pytruns   cC@sR|jd|jj}t|dkrFtjtdn|ddS(sget identity of longdesc fields5select fieldid from fielddefs where name = "longdesc"isunknown database schemai(R[RPtfetchallRMRRJR(R+RB((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyRQs  cC@s|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( R[R;RCtkeysRPR\R(tstatusR(R+Rtidtexisting((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyR1s % cC@sz|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( R[R;RCR]RRPR\R(R^R(R+R2RR_((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyR3s#c C@ss|jjtd|j|\}}x+|jD]}|jjtd||jjdd|j}|jjdd}y||}Wn0tk r|i|d6|d6|d6}nX|jjtd|t j d |d } t j | j } | j} | r8|jj| tjtd t j| q8q8W|jjtd d S(stell bugzilla to send mail.stelling bugzilla to send mail: s bug %s R RRR_R#srunning notify command %s s (%s) 2>&1trbsbugzilla notify command %ssdone N(R(R^Rtget_bugzilla_userR]R)RTt TypeErrorRLR tpopenRt fromnativeeoltreadtclosetwarnRRJt explainexit( R+RR/R#tuseridR_tcmdfmtRtcmdtfptouttret((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyRs& # cC@sy|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( RStKeyErrorRKt ValueErrorR(RLRR[RPR\RM(R+R#Rjtall((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyt get_user_ids    cC@s|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.R Rs#cannot find bugzilla user id for %ss)cannot find bugzilla user id for %s or %s(R0RsRpR(R)RRJR(R+R/R#Rjt defaultuser((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyRb.s    cC@st|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( RMR(RhRRbttimetstrftimeR[RRROtcommit(R+R4R5R6R/R#Rjtnow((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyR7BsN(R8R9R:t staticmethodRCtNoneRHR,R[RQR1R3RRsRbR7(((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyR;s     t bzmysql_2_18cB@seZdZdZRS(s!support for bugzilla 2.18 series.cC@stj||d|_dS(Ns>cd %(bzdir)s && perl -T contrib/sendbugmail.pl %(id)s %(user)s(R;R,RT(R+R(((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyR,Xs(R8R9R:R,(((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyR{Ust bzmysql_3_0cB@s eZdZdZdZRS(s support for bugzilla 3.0 series.cC@stj||dS(N(R{R,(R+R(((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyR,`scC@sR|jd|jj}t|dkrFtjtdn|ddS(sget identity of longdesc fields0select id from fielddefs where name = "longdesc"isunknown database schemai(R[RPR\RMRRJR(R+RB((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyRQcs  (R8R9R:R,RQ(((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyR|]s tcookietransportrequestcB@s)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 prior to 4.4.3. 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. cC@s4|jr0x$|jD]}|jd|qWndS(NtCookie(tcookiest putheader(R+t connectiontcookie((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyt send_cookies~s ic C@s||_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_hostRtsend_user_agentt send_contentt getresponsetAttributeErrort_conntmsgtgetallmatchingheaderstsplitRtappendR^t xmlrpclibt ProtocolErrortreasontheadersRft getparsertfeedRg( R+Rthandlert request_bodyRthtresponsetheadertvalRtpayloadtparsert unmarshaller((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pytrequests4        (R8R9R:RRR(((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyR}ms  tcookietransportcB@seZddZRS(icC@s/tjtjdr+tjj||ndS(NR,(Rt safehasattrRt TransportR,(R+t use_datetime((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyR,s(R8R9R,(((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyRstcookiesafetransportcB@seZddZRS(icC@s/tjtjdr+tjj||ndS(NR,(RRRRt SafeTransportR,(R+R((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyR,s(R8R9R,(((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyRstbzxmlrpccB@sDeZdZdZdZdZdZdZdZRS(snSupport for access to Bugzilla via the Bugzilla XMLRPC API. Requires a minimum Bugzilla version 3.4. cC@s8tj|||jjdd}|jdd}|jjdd}|jjdd}|jjdd|_|jjdd|_tj||j ||_ |j j j d j d }t|d |_t|d |_|j jji|d 6|d6td6}|jdd|_dS(NR Rt/s /xmlrpc.cgiR#RRRR%t.iitlogintrestrict_loginttokenR(R&R,R(R)trstripRRRt ServerProxyt transporttbzproxytBugzillaR%RRKt bzvermajort bzverminortUserRtTruetgettbztoken(R+R(tbzwebR#REtverR((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyR,s cC@s1tjj|dddkr&tStSdS(Nthttpithttps(RturlreqturlparseRR(R+turi((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyRscC@si|jjji|gd6dgd6|jd6}djg|dt|dD]}|d^qRS(s0Return a string with all comment text for a bug.RBR6tinclude_fieldsRRRtcomments(RtBugRRR?RA(R+R_tctt((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pytget_bug_commentss cC@s|jjjit|jd6gd6td6|jd6}x=|dD]1}|d}|jjt d|||=qKWdS(NRBRt permissiveRtfaultsR_sbug %d does not exist ( RRRtsortedR]RRR(R^R(R+RtprobetbadbugR_((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyR1s" cC@suxnt|jD]Z}|j|jt|dkr|jjtd|t|f||=qqWdS(Nis(bug %d already knows about changeset %s (RR]RtfindRR(R^R(R+R2RR_((s4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyR3s $cC@si}d|kr#|d|dss(bug %d already knows about changeset %s N( RR]RRRAtanyR(R^R(R+R2RR4RtresultR((Rs4/usr/lib64/python2.7/site-packages/hgext/bugzilla.pyR3s cC@si}d|kr#|d|d$s   .                      -DNQ}