o ^Ub@s|dZddlmZddlZddlZddlZddlmZddlm Z ddl m Z m Z m Z mZmZmZmZddlmZmZejZdZiZeeZed d d d ed d dd ed ddd ed ddd ed ddd ed ddd ed ddd ed ddd ed ddd ed ddd ed dejd ed ddd ed dd d ed d!dd ed d"dd ed d#dd ed d$d%d ed d&dd ed d'dd ed d(dd Gd)d*d*eZGd+d,d,eZGd-d.d.eZGd/d0d0eZGd1d2d2eZGd3d4d4eej Z!Gd5d6d6eej"Z#Gd7d8d8eZ$Gd9d:d:e$Z%Gd;d<dd>eZ(Gd?d@d@eZ)dCdAdBZ*dS)Da)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. )absolute_importN)_)short)error logcmdutilmailpycompat registrarurlutil)procutil stringutilsships-with-hg-corebugzillaapikey)defaultbzdirs/var/www/html/bugzillabzemailbzurlshttp://localhost/bugzilla/bzuserdbbugs fixregexpsfix(?:es)?\s*(?:bugs?\s*)?,?\s*(?:nos?\.?|num(?:ber)?s?)?\s*(?P(?:#?\d+\s*(?:,?\s*(?:and)?)?\s*)+)\.?\s*(?:h(?:ours?)?\s*(?P\d*(?:\.\d+)?))? fixresolutionsFIXED fixstatussRESOLVEDhosts localhostnotifypasswordregexpsbugs?\s*,?\s*(?:#|nos?\.?|num(?:ber)?s?)?\s*(?P(?:\d+\s*(?:,?\s*(?:and)?)?\s*)+)\.?\s*(?:h(?:ours?)?\s*(?P\d*(?:\.\d+)?))?stripstyletemplatetimeoutuserusermapversionc@@eZdZdZddZddZddZdd Zd d Zd d Z dS)bzaccessz"Base class for access to Bugzilla.cCs2||_|jdd}|r|jj|dgddSdS)Nrr%)sections)uiconfig readconfig)selfr*usermapr/0/usr/lib/python3/dist-packages/hgext/bugzilla.py__init__s zbzaccess.__init__cCs2|jdD]\}}||kr|Sq|S)z,map name of committer to Bugzilla user name.r%)r* configitemslower)r-user committerbzuserr/r/r0 map_committers zbzaccess.map_committercCdS)7remove bug IDs that do not exist in Bugzilla from bugs.Nr/)r-bugsr/r/r0filter_real_bug_idszbzaccess.filter_real_bug_idscCr8);remove bug IDs where node occurs in comment text from bugs.Nr/)r-noder:r/r/r0filter_cset_known_bug_idsr<z"bzaccess.filter_cset_known_bug_idscCr8)update 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. Nr/)r-bugidnewstatetextr5r/r/r0 updatebugr<zbzaccess.updatebugcCr8zForce sending of Bugzilla notification emails. Only required if the access method does not trigger notification emails automatically. Nr/r-r:r5r/r/r0notifyr<zbzaccess.notifyN) __name__ __module__ __qualname____doc__r1r7r;r?rDrGr/r/r/r0r(s r(c@sheZdZdZeddZdZddZddZd d Z d d Z d dZ ddZ ddZ ddZddZdS)bzmysqlzSupport 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. cCsddtt|dS)z#return SQL-friendly list of bug ids(,))joinmapstr)idsr/r/r0 sql_buglistszbzmysql.sql_buglistNc Csz ddl}|t_Wnty$}zttd|tddd}~wwt|||j dd}|j dd}|j dd}|j dd }t |j dd }|j td |||d t |ftjj|||||d |_|j|_||_i|_d|_dS)Nrs&python mysql support not available: %ss (try installing the %s package)spython3-mysqldbrrr$rrr"s'connecting to %s:%s as %s, password %s *)hostr4passwddbconnect_timeouts-cd %(bzdir)s && ./processmail %(id)s %(user)s)MySQLdbrL_MySQLdb ImportErrorrAbortrr(r1r*r+intnotelenconnectconncursorget_longdesc_id longdesc_iduser_idsdefault_notify) r-r*mysqlerrrVr4rWrXtimeoutr/r/r0r1s:        zbzmysql.__init__cOs^|jtd||fz |jj|i|WdStjjy.|jtd||fw)z run a query.s query: %s %s sfailed query: %s %s N)r*r_rrcexecuterLr[ MySQLError)r-argskwargsr/r/r0runsz bzmysql.runcC:|d|j}t|dkrttd|ddS)get identity of longdesc fields5select fieldid from fielddefs where name = "longdesc"unknown database schemarrorcfetchallr`rr]rr-rSr/r/r0rd    zbzmysql.get_longdesc_idcCs`|dt|dd|jD}|D]}||vr-|jtd|||=qdS)z"filter not-existing bugs from set.s*select bug_id from bugs where bug_id in %scSsg|]\}|qSr/r/).0idr/r/r0 )sz/bzmysql.filter_real_bug_ids..bug %d does not exist N) rorLrTkeysrcrur*statusr)r-r:existingryr/r/r0r;#s  zbzmysql.filter_real_bug_idscCsX|dt|t|f|jD]\}|jt d|t|f||=qdS)z=filter bug ids that already refer to this changeset from set.z]select bug_id from longdescs where bug_id in %s and thetext like "%%%s%%"(bug %d already knows about changeset %s N) rorLrTr|rrcrur*r}rr-r>r:ryr/r/r0r?/s z!bzmysql.filter_cset_known_bug_idsc Cs|jtd||\}}|D]e}|jtd||jdd|j}|jdd}z||}WntyF||||d}Ynw|jtd|t d|d } t | } | } | rx|j| ttd t | q|jtd d S) ztell bugzilla to send mail.stelling bugzilla to send mail: s bug %s rrr)ridr$srunning notify command %s s (%s) 2>&1srbsbugzilla notify command %ssdone N)r*r}rget_bugzilla_userr|r+rg TypeErrorr_r popenr fromnativeeolreadclosewarnrr] explainexit) r-r:r5r4useridrycmdfmtbzdircmdfpoutretr/r/r0rG=s,    zbzmysql.notifyc Csz|j|WStyMzt|}Wn0tyC|jtd||d||j }t |dkr9t|t|dd}Ynw||j|<|YSw)z!look up numeric bugzilla user id.slooking up user %s zPselect userid from profiles where login_name like %srrr) rfKeyErrorr^ ValueErrorr*r_rrorcrur`)r-r4rallr/r/r0 get_user_idWs&       zbzmysql.get_user_idc Cs||}z ||}W||fStyIz|jdd}|s)ttd|||}|}WntyCttd||fwY||fSw)zSee 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)r7rrr*r+rr]r)r-r5r4r defaultuserr/r/r0rls.      zbzmysql.get_bugzilla_usercCslt|dkr|jtd||\}}td}|d||||f|d||||jf|j dS)zupdate bug state with comment text. Try adding comment as committer of changeset, otherwise as default bugzilla user.rs'Bugzilla/MySQL cannot update bug state z%Y-%m-%d %H:%M:%Szvinsert into longdescs (bug_id, who, bug_when, thetext) values (%s, %s, %s, %s)zfinsert into bugs_activity (bug_id, who, bug_when, fieldid) values (%s, %s, %s, %s)N) r`r*rrrtimestrftimerorerbcommit)r-rArBrCr5r4rnowr/r/r0rDs    zbzmysql.updatebug)rHrIrJrK staticmethodrTr[r1rordr;r?rGrrrDr/r/r/r0rLs    rLc@seZdZdZddZdS) bzmysql_2_18z!support for bugzilla 2.18 series.cCst||d|_dS)Ns>cd %(bzdir)s && perl -T contrib/sendbugmail.pl %(id)s %(user)s)rLr1rgr-r*r/r/r0r1s zbzmysql_2_18.__init__N)rHrIrJrKr1r/r/r/r0rs rc@s eZdZdZddZddZdS) bzmysql_3_0z support for bugzilla 3.0 series.cCst||dSN)rr1rr/r/r0r1szbzmysql_3_0.__init__cCrp)rqs0select id from fielddefs where name = "longdesc"rrrsrrtrvr/r/r0rdrwzbzmysql_3_0.get_longdesc_idN)rHrIrJrKr1rdr/r/r/r0rs rc@s&eZdZdZgZddZdddZdS) cookietransportrequestaA 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. cCs&|jr|jD] }|d|qdSdS)NsCookie)cookies putheader)r- connectioncookier/r/r0 send_cookiess  z#cookietransportrequest.send_cookiesrc Cs||_d|_||}|r|d||||||||||||||z| }Wnt yC|j }Ynw|j dD]}|ddd}|ddd} |j| qJ|jdkrvt|||j|j|j j|} |\} } | | | | S)NFrrs Set-Cookies: ;r)verboseaccept_gzip_encodingmake_connectionset_debuglevel send_request send_hostrsend_user_agent send_content getresponseAttributeError_connmsggetallmatchingheaderssplitrappendr} xmlrpclib ProtocolErrorreasonheadersr getparserfeedr) r-rVhandler request_bodyrhresponseheadervalrpayloadparser unmarshallerr/r/r0requests>           zcookietransportrequest.requestNr)rHrIrJrKrrrr/r/r/r0rs rc@eZdZdddZdS)cookietransportrcCs$ttjdrtj||dSdSNr1)r safehasattrr Transportr1r- use_datetimer/r/r0r1zcookietransport.__init__NrrHrIrJr1r/r/r/r0rrc@r)cookiesafetransportrcCs$ttjdrtj||dSdSr)r rrr SafeTransportr1rr/r/r0r1rzcookiesafetransport.__init__Nrrr/r/r/r0rrrc@r')bzxmlrpcznSupport for access to Bugzilla via the Bugzilla XMLRPC API. Requires a minimum Bugzilla version 3.4. cCst|||jdd}|dd}|jdd}|jdd}|jdd|_|jdd|_tt || ||_ |j j d d }t|d |_t|d |_|j j||d d}|dd|_dS)Nrr/s /xmlrpc.cgir$rrrr&.rrrT)loginrsrestrict_logintokenr)r(r1r*r+rstrip fixstatus fixresolutionr ServerProxyrstrurl transportbzproxyBugzillaversionrr^ bzvermajor bzverminorUserlogingetbztoken)r-r*bzwebr4rWverrr/r/r0r1s"  zbzxmlrpc.__init__cCs"tj|dddkrtStS)Nshttprshttps)r urlrequrlparserr)r-urir/r/r0r*szbzxmlrpc.transportcCs@|jj|gdg|jd}ddd|dd|dDS) z0Return a string with all comment text for a bug.text)idsinclude_fieldsrrcSsg|]}|dqS)rr/rxtr/r/r0rz6sz-bzxmlrpc.get_bug_comments..rs%dcomments)rBugcommentsrrP)r-rycr/r/r0get_bug_comments0s zbzxmlrpc.get_bug_commentscCsV|jjt|gd|jd}|dD]}|d}|jtd|||=qdS)NT)rrs permissiversfaultsrr{) rrrsortedr|rr*r}r)r-r:probebadbugryr/r/r0r;9s  zbzxmlrpc.filter_real_bug_idscCsPt|D]}||t|dkr%|jtd|t|f||=qdS)Nr)rr|rfindrr*r}rrr/r/r0r?Gs z"bzxmlrpc.filter_cset_known_bug_idscCsi}d|vr |d|d<|jdkr8|g|d<d|i|d<d|vr*|j|d<|j|d <|j|d <|jj|dSd|vrD|jt d ||d <||d<|jj |dS) Nhours work_timersbodycommentfixstatus resolutionrs?Bugzilla/XMLRPC needs Bugzilla 4.0 or later to mark bugs fixed r) rrrrrrupdater*rr add_comment)r-rArBrCr5rmr/r/r0rDPs(       zbzxmlrpc.updatebugN) rHrIrJrKr1rrr;r?rDr/r/r/r0r s  rc@s0eZdZdZddZddZddZdd Zd S) bzxmlrpcemailaRead data from Bugzilla via XMLRPC, send updates via email. Advantages of sending updates via email: 1. Comments can be added as any user, not just logged in user. 2. Bug statuses or other fields not accessible via XMLRPC can potentially be updated. There is no XMLRPC function to change bug status before Bugzilla 4.0, so bugs cannot be marked fixed via XMLRPC before Bugzilla 4.0. But bugs can be marked fixed via email from 3.4 onwards. cCs@t|||jdd|_|jsttdt |jdS)Nrrsconfiguration 'bzemail' missing) rr1r*r+bzemailrr]rrvalidateconfigrr/r/r0r1~s zbzxmlrpcemail.__init__cCs:|jdkrd|t|fS|dkrd}d|t|fS)Nrs@%s %srsbug_ids@%s = %s)rrbytestr)r- fieldnamevaluer/r/r0makecommandlines zbzxmlrpcemail.makecommandlinec Cs ||}|jj|g|jd}|ds5|jdd}|jj|g|jd}|ds5tt d||ddd}| | d|d |d |}t |j}t |j||}t |j|j|} t |j||} || d <| | d <t |jd || d<t |j} | || | dS)asend modification message to Bugzilla bug via email. The message format is documented in the Bugzilla email_in.pl specification. commands is a list of command lines, comment is the comment text. To stop users from crafting commit comments with Bugzilla commands, specify the bug ID via the message body, rather than the subject line, and leave a blank line after it. )smatchrsusersrr$s(default bugzilla user %s email not foundrsemailr s sFromsTosBug modificationsSubjectN)r7rrrrr*r+rr]rrr rPr _charsets addressencoder mimeencode headencodera as_string) r-rAcommandscommentr5r4matchesrCr rrsendmailr/r/r0send_bug_modify_emails6      z#bzxmlrpcemail.send_bug_modify_emailcCsfg}d|vr||d|dd|vr)||d|j||d|j|||||dS)Nrrrs bug_statusr)rr rrr)r-rArBrCr5cmdsr/r/r0rDszbzxmlrpcemail.updatebugN)rHrIrJrKr1r rrDr/r/r/r0rjs  )rc@s eZdZdS)NotFoundN)rHrIrJr/r/r/r0rsrc@sTeZdZdZddZdddZddZdd d Zd d ZddZ ddZ ddZ dS) bzrestapizVRead and write bugzilla data using the REST API available since Bugzilla 5.0. cCs~t|||jdd}d|dg|_|jdd|_|jdd|_|jdd|_|jdd|_ |jdd |_ dS) Nrrrsrestrr$rrr) r(r1r*r+rPbzrootapikeyr4rWrr)r-r*bzr/r/r0r1s zbzrestapi.__init__NcCszd|jgdd|D}i}|jr|j|d<n|jr)|jr)|j|d<|j|d<|r/||d<|r;d|tj|f}|S) NrcSsg|]}t|qSr/)rrrr/r/r0rzsz$bzrestapi.apiurl..sapi_keyrrrs%s?%s)rPrrr4rWr r urlencode)r-targetsinclude_fieldsr qvr/r/r0apiurls    zbzrestapi.apiurlc Csfzt|j|}t|WStjjy2}z|j dkr%t t d|j dkr-t d}~ww)Nauthorization failed)r openr*r json_loadsrr urlerr httperrorcoderr]rr)r-burlrespinstr/r/r0_fetchs  zbzrestapi._fetchPOSTc Cst|}|dkrGdddtjj}|}ntjj}|||ddi}zt|j|}t | WStj j yV}z|jdkrIttd|jdkrQtd}~ww) NPUTc@seZdZddZdS)z%bzrestapi._submit..putrequestcSr8)Nr.r/r-r/r/r0 get_methodsz0bzrestapi._submit..putrequest.get_methodN)rHrIrJr0r/r/r/r0 putrequests r1s Content-Typesapplication/jsonr!r"r#)jsondumpsr rrr openerr*r$rr%rr&r'r(rr]rr) r-r)datamethodr1 request_typereqr*r+r/r/r0_submits"   zbzrestapi._submitc Cs`t}|D] }|jd|fdd}z||Wqty%||Yqw|D]}||=q(dS)r9bugrrN)setr r,radd)r-r:badbugsrAr)r/r/r0r;s zbzrestapi.filter_real_bug_idscst||D]6}|jd|dfdd}||}|dt|d}tfdd|Dr>|jt d |f||=qd S) r=r:rrr;rrc3s|] }|dvVqdS)rNr/)rxrsnr/r0 sz6bzrestapi.filter_cset_known_bug_ids..rN) rr|r r,rranyr*r}r)r-r>r:rAr)resultrr/r?r0r?s    z#bzrestapi.filter_cset_known_bug_idscCsi}d|vr |d|d<d|vr|j|d<|j|d<|r=|ddd|d<|d |f}|j||d d |jd |dS|d |df}|||ddd|jd |dS)r@rrrrrF)rs is_privates is_markdownrr:r.)r6supdated bug %s sadded comment to bug %s N)rrr r9r*debug)r-rArBrCr5bugmodr)r/r/r0rD!s.    zbzrestapi.updatebugcCr8rEr/rFr/r/r0rGCszbzrestapi.notifyr)r-) rHrIrJrKr1r r,r9r;r?rDrGr/r/r/r0rs    "rc@s>eZdZeeeeeedZ ddZ ddZ ddZ dd Z d S) bugzilla)s2.16s2.18s3.0sxmlrpcs xmlrpc+emailsrestapicCs||_||_|jdd}ztj|}Wnty$ttd|w||j|_ t |jddt j |_ t |jddt j |_t d|_dS)Nrr&s!bugzilla version %s not supportedrrs\D+)r*repor+rF _versionsrrr]rbzdriverrecompile IGNORECASEbug_refix_resplit_re)r-r*rG bzversionbzclassr/r/r0r1Xs$   zbugzilla.__init__c Csd}i}|j||}|j||} i}|s|sn|s#|}n|s(|}n ||kr3|}n|}|}||urN|j||}d|vrM|d=n |j||}d|d<z|d}Wntyp|d}Ynwz t|d} | |d<Wn&tyYnt yYnt y|j t d|dYnw|j|D] } | sq||t| <qq|r|j||r|j|||S) zreturn bugs dictionary created from commit comment. Extract bug info from changeset comments. Filter out any that are not known to Bugzilla, and any that already have a reference to the given changeset in their comments. rTrNrrrrs%s: invalid hours )rMsearch descriptionrNstartendgroup IndexErrorfloatrrr*r}rrOrr^rIr;r?r>) r-ctxrTr:bugmatchfixmatch bugattribsmrShoursryr/r/r0 find_bugsms`     ( zbugzilla.find_bugsc sfdd}d}jdd}|sjdd}|s |s td}t||}tjj|}j|j|| t |jdd jj |jj d j } j||| t|dS) z0update bugzilla bug with reference to changeset.cs`tjdd}t|}|dkr.|d}|dkr |S||dd}|d8}|dks|S)zJstrip leading prefix of repo root and turn into url-safe path.rrrrrrrN)r^r*r+r pconvertr)rootcountrr/r/r0webroots  z bugzilla.update..webrootNrr!r sUchangeset {node|short} in repo {root} refers to bug {bug}. details: {desc|tabindent}swebsbaseurl)changesbughgwebrarc)r*r+rr templatespecchangesettemplaterrG pushbuffershow changesetrrra popbufferrIrDr emailr4) r-rArBrYrcmapfiletmplspecrr5r/r/r0rs0       zbugzilla.updatecCs|j||dS)z1ensure Bugzilla users are notified of bug change.N)rIrGrFr/r/r0rGszbugzilla.notifyN)rHrIrJrLrrrrrrHr1r_rrGr/r/r/r0rFLs 9 )rFc Ks|dur ttd|z/t||}||}||}|r:|D] }|||||q ||t| WdSWdSt yT} z ttdt | d} ~ ww)zadd comment to bugzilla for each changeset that refers to a bugzilla bug id. only add a comment once per bug, so same change seen multiple times does not fill bug with duplicate data.Ns)hook type %s does not pass a changeset idsBugzilla error: %s) rr]rrFr_rrGr rmr4 Exception forcebytestr) r*rGhooktyper>rnrrYr:reer/r/r0hooks"   rur)+rK __future__rr2rJrmercurial.i18nrmercurial.noder mercurialrrrrr r r mercurial.utilsr r r testedwith configtable configitemdynamicdefaultobjectr(rLrrrrrrrrr LookupErrorrrrFrur/r/r/r0s   $    .8 J]W