
    6h[                    H   S r SSKJr  SSKrSSKrSSKrSSKJr  SSKJ	r	  SSK
JrJrJrJrJrJrJr  SSKJrJr  \R*                  rSr0 r\R0                  " \5      r\" S	S
SS9  \" S	SSS9  \" S	SSS9  \" S	SSS9  \" S	SSS9  \" S	SSS9  \" S	SSS9  \" S	SSS9  \" S	SSS9  \" S	SSS9  \" S	S\R2                  S9  \" S	SSS9  \" S	SS S9  \" S	S!SS9  \" S	S"SS9  \" S	S#SS9  \" S	S$S%S9  \" S	S&SS9  \" S	S'SS9  \" S	S(SS9   " S) S*5      r " S+ S,\5      r " S- S.\5      r " S/ S0\5      r " S1 S25      r " S3 S4\\R>                  5      r  " S5 S6\\RB                  5      r" " S7 S8\5      r# " S9 S:\#5      r$ " S; S<\%5      r& " S= S>\5      r' " S? S@5      r(SBSA jr)g)Ca)  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 ``<ids>`` containing the bug
  IDs separated by non-digit characters. It may also contain
  a named group ``<hours>`` 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 ``<ids>` containing
  the bug IDs separated by non-digit characters. It may also contain
  a named group ``<hours>`` 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.
    )annotationsN)_)short)error
logcmdutilmailpycompat	registrarurlutil)procutil
stringutils   ships-with-hg-core   bugzilla   apikey    )default   bzdirs   /var/www/html/bugzilla   bzemail   bzurls   http://localhost/bugzilla/   bzuser   db   bugs	   fixregexps   fix(?:es)?\s*(?:bugs?\s*)?,?\s*(?:nos?\.?|num(?:ber)?s?)?\s*(?P<ids>(?:#?\d+\s*(?:,?\s*(?:and)?)?\s*)+)\.?\s*(?:h(?:ours?)?\s*(?P<hours>\d*(?:\.\d+)?))?   fixresolutions   FIXED	   fixstatuss   RESOLVED   hosts	   localhost   notify   password   regexps   bugs?\s*,?\s*(?:#|nos?\.?|num(?:ber)?s?)?\s*(?P<ids>(?:\d+\s*(?:,?\s*(?:and)?)?\s*)+)\.?\s*(?:h(?:ours?)?\s*(?P<hours>\d*(?:\.\d+)?))?   strip   style   template   timeout      user   usermap   versionc                  <    \ rS rSrSrS rS rS rS rS r	S r
S	rg
)bzaccessi  z"Base class for access to Bugzilla.c                    Xl         U R                   R                  SS5      nU(       a  U R                   R                  US/S9  g g )Nr   r&   )sections)uiconfig
readconfig)selfr,   usermaps      0/usr/lib/python3/dist-packages/hgext/bugzilla.py__init__bzaccess.__init__  s<    ''..j9GGw*> r   c                    U R                   R                  S5       H+  u  p#UR                  5       UR                  5       :X  d  M)  Us  $    U$ )z,map name of committer to Bugzilla user name.r&   )r,   configitemslower)r/   user	committerbzusers       r1   map_committerbzaccess.map_committer  s@    !%!4!4Z!@I DJJL0 "A r   c                    g)7remove bug IDs that do not exist in Bugzilla from bugs.N )r/   bugss     r1   filter_real_bug_idsbzaccess.filter_real_bug_ids      r   c                    g);remove bug IDs where node occurs in comment text from bugs.Nr>   )r/   noder?   s      r1   filter_cset_known_bug_ids"bzaccess.filter_cset_known_bug_ids  rB   r   c                    g)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/   bugidnewstatetextr8   s        r1   	updatebugbzaccess.updatebug  rB   r   c                    gzForce sending of Bugzilla notification emails.

Only required if the access method does not trigger notification
emails automatically.
Nr>   r/   r?   r8   s      r1   notifybzaccess.notify  rB   r   )r,   N)__name__
__module____qualname____firstlineno____doc__r2   r:   r@   rF   rM   rR   __static_attributes__r>   r   r1   r)   r)     s%    ,?FJr   r)   c                  b    \ rS rSrSr\S 5       rSrS rS r	S r
S rS	 rS
 rS rS rS rSrg)bzmysqli  zSupport 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.
c                L    SSR                  [        [        U 5      5      -   S-   $ )z#return SQL-friendly list of bug ids   (   ,   ))joinmapstr)idss    r1   sql_buglistbzmysql.sql_buglist  s#     diiC..55r   Nc           
     6    SS K nU[        l        [        R                  X5        U R                  R                  SS5      nU R                  R                  SS5      nU R                  R                  SS5      nU R                  R                  SS	5      n[        U R                  R                  SS
5      5      nU R                  R                  [        S5      XGUS[        U5      -  4-  5        [        R                  R                  XEXgUS9U l        U R                  R!                  5       U l        U R#                  5       U l        0 U l        SU l        g ! [         a6  n[        R
                  " [        S5      U-  [        S5      S-  -   5      eS nAff = f)Nr   s&   python mysql support not available: %ss     (try installing the %s package)s   python3-mysqldbr   r   r%   r   r   r#   s'   connecting to %s:%s as %s, password %s
   *)hostr7   passwddbconnect_timeouts-   cd %(bzdir)s && ./processmail %(id)s %(user)s)MySQLdbr[   _MySQLdbImportErrorr   Abortr   r)   r2   r,   r-   intnotelenconnectconncursorget_longdesc_idlongdesc_iduser_idsdefault_notify)	r/   r,   mysqlerrrh   r7   ri   rj   timeouts	            r1   r2   bzmysql.__init__  s^   	#$G 	$#ww~~k73ww~~k73[9WW^^K/dggnn[*=>9:tc&k123	
 $$,, - 
	 ii&&(//1N/  	++;<sB569KKL 	s   E 
F"1FFc                $   U R                   R                  [        S5      X4-  5         U R                  R                  " U0 UD6  g! [
        R                  R                   a*    U R                   R                  [        S5      X4-  5        e f = f)zrun a query.s   query: %s %s
s   failed query: %s %s
N)r,   rq   r   ru   executer[   rm   
MySQLError)r/   argskwargss      r1   runbzmysql.run  sv    Q()TN:;	KK00** 	GGLL34~EF	s   A ABc                    U R                  S5        U R                  R                  5       n[        U5      S:w  a  [        R
                  " [        S5      5      eUS   S   $ )get identity of longdesc fields5   select fieldid from fielddefs where name = "longdesc"      unknown database schemar   r   ru   fetchallrr   r   ro   r   r/   rc   s     r1   rv   bzmysql.get_longdesc_id  sO    IJkk""$s8q=++a :;<<1vayr   c                \   U R                  S[        R                  UR                  5       5      -  5        U R                  R                  5        Vs/ s H  u  o"PM     nnUR                  5        H3  nX#;  d  M
  U R                  R                  [        S5      U-  5        X	 M5     gs  snf )z"filter not-existing bugs from set.s*   select bug_id from bugs where bug_id in %s   bug %d does not exist
N)	r   r[   rd   keysru   r   r,   statusr   )r/   r?   idexistings       r1   r@   bzmysql.filter_real_bug_ids#  s    9!!$))+./	
 %)KK$8$8$:;$:5BB$:;))+B!q!;<rABH  <s   B)c                :   U R                  S[        R                  UR                  5       5      < S[	        U5      < S35        U R
                  R                  5        H9  u  nU R                  R                  [        S5      U[	        U5      4-  5        X#	 M;     g)z=filter bug ids that already refer to this changeset from set.zAselect bug_id from longdescs where
                    bug_id in z and thetext like "%z%"(   bug %d already knows about changeset %s
N)
r   r[   rd   r   r   ru   r   r,   r   r   r/   rE   r?   r   s       r1   rF   !bzmysql.filter_cset_known_bug_ids/  s{     ""499;/t>	

 [[))+ERGGNN>?uT{#$  ,r   c                   U R                   R                  [        S5      5        U R                  U5      u  p4UR	                  5        GH?  nU R                   R                  [        S5      U-  5        U R                   R                  SSU R                  5      nU R                   R                  SS5      n Xe-  nU R                   R                  [        S5      U-  5        [        R                  " SU-  S	5      n	[        R                  " U	R                  5       5      n
U	R                  5       nU(       d  M  U R                   R                  U
5        [         R"                  " [        S
5      [        R$                  " U5      -  5      e   U R                   R                  [        S5      5        g! [         a    XgXSS.-  n GN
f = f)ztell bugzilla to send mail.s   telling bugzilla to send mail:
s	     bug %s
r   r   r   )r      idr%   s   running notify command %s
s	   (%s) 2>&1s   rbs   bugzilla notify command %ss   done
N)r,   r   r   get_bugzilla_userr   r-   ry   	TypeErrorrq   r   popenr   fromnativeeolreadclosewarnr   ro   explainexit)r/   r?   r8   r7   useridr   cmdfmtbzdircmdfpoutrets               r1   rR   bzmysql.notify=  s[   q<=>//	:))+BGGNN1]+b01WW^^KD<O<OPFGGNN;9EK k GGLL9:S@As 2E:B$$RWWY/C((*CsS!kk34x7K7KC7PP # ( 	q|$  KJJKs   7F11GGc                    U R                   U   $ ! [         a     [        U5      nO! [         a    U R                  R                  [        S5      U-  5        U R                  SU5        U R                  R                  5       n[        U5      S:w  a  [        U5      e[        US   S   5      n Of = fX R                   U'   Us $ f = f)z!look up numeric bugzilla user id.s   looking up user %s
zPselect userid from profiles
                            where login_name like %sr   r   )rx   KeyErrorrp   
ValueErrorr,   rq   r   r   ru   r   rr   )r/   r7   r   alls       r1   get_user_idbzmysql.get_user_idW  s    	==&& 	(T 
(Q67$>?8
 kk**,s8q="4.(SVAY
( #)MM$M	s0    
C	(C	BB30C	2B33C	C	c                   U R                  U5      n U R                  U5      nX#4$ ! [         a     U R                  R	                  SS5      nU(       d"  [
        R                  " [        S5      U-  5      eU R                  U5      nUn X#4$ ! [         a%    [
        R                  " [        S5      UW4-  5      ef = ff = f)zSee if committer is a registered bugzilla user. Return
bugzilla username and userid if so. If not, return default
bugzilla username and userid.r   r   s#   cannot find bugzilla user id for %ss)   cannot find bugzilla user id for %s or %s)r:   r   r   r,   r-   r   ro   r   )r/   r8   r7   r   defaultusers        r1   r   bzmysql.get_bugzilla_userl  s     !!),	%%d+F ~  	"ggnn[)D"++@ADH  ))+6" ~  kkBC[)* 	s   ' 
CAB/B==Cc                T   [        U5      S:  a$  U R                  R                  [        S5      5        U R	                  U5      u  pV[
        R                  " S5      nU R                  SXXs45        U R                  SXXpR                  45        U R                  R                  5         g)zvupdate bug state with comment text.

Try adding comment as committer of changeset, otherwise as
default bugzilla user.r   s'   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)rr   r,   r   r   r   timestrftimer   rw   rt   commit)r/   rJ   rK   rL   r8   r7   r   nows           r1   rM   bzmysql.updatebug  s    
 x=1GGLLFGH//	:mm/0/ C&		
 	/C!1!12	

 			r   )rt   ru   ry   rw   rx   )rT   rU   rV   rW   rX   staticmethodrd   rm   r2   r   rv   r@   rF   rR   r   r   rM   rY   r>   r   r1   r[   r[     sP     6 6 HO<
%4*.r   r[   c                      \ rS rSrSrS rSrg)bzmysql_2_18i  z!support for bugzilla 2.18 series.c                <    [         R                  X5        SU l        g )Ns>   cd %(bzdir)s && perl -T contrib/sendbugmail.pl %(id)s %(user)s)r[   r2   ry   r/   r,   s     r1   r2   bzmysql_2_18.__init__  s    "M 	r   )ry   N)rT   rU   rV   rW   rX   r2   rY   r>   r   r1   r   r     s
    +
r   r   c                  $    \ rS rSrSrS rS rSrg)bzmysql_3_0i  z support for bugzilla 3.0 series.c                .    [         R                  X5        g N)r   r2   r   s     r1   r2   bzmysql_3_0.__init__  s    d'r   c                    U R                  S5        U R                  R                  5       n[        U5      S:w  a  [        R
                  " [        S5      5      eUS   S   $ )r   s0   select id from fielddefs where name = "longdesc"r   r   r   r   r   s     r1   rv   bzmysql_3_0.get_longdesc_id  sO    DEkk""$s8q=++a :;<<1vayr   r>   N)rT   rU   rV   rW   rX   r2   rv   rY   r>   r   r1   r   r     s    *(r   r   c                  ,    \ rS rSrSr/ rS rSS jrSrg)cookietransportrequesti  az  A 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.
c                r    U R                   (       a&  U R                    H  nUR                  SU5        M     g g )Ns   Cookie)cookies	putheader)r/   
connectioncookies      r1   send_cookies#cookietransportrequest.send_cookies  s,    <<,,$$Y7 ' r   c                   X@l         SU l        U R                  U5      nU(       a  UR                  S5        U R	                  XRU5        U R                  XQ5        U R                  U5        U R                  U5        U R                  XS5         UR                  5       nUR                  R                  S5       HH  nUR                  SS5      S   nUR                  SS5      S   n	U R                  R!                  U	5        MJ     UR"                  S:w  aB  [$        R'                  X-   UR"                  UR(                  UR                  R*                  5      eUR-                  5       n
U R/                  5       u  pUR1                  U
5        UR3                  5         UR3                  5       $ ! [         a    UR                  R                  5       n GN3f = f)NFr   s
   Set-Cookies   :    ;r      )verboseaccept_gzip_encodingmake_connectionset_debuglevelsend_request	send_hostr   send_user_agentsend_contentgetresponseAttributeError_connmsggetallmatchingheaderssplitr   appendr   	xmlrpclibProtocolErrorreasonheadersr   	getparserfeedr   )r/   rh   handlerrequest_bodyr   hresponseheadervalr   payloadparserunmarshallers                r1   requestcookietransportrequest.request  s   $)!   &Q!l3q!Q!*	-}}H
 ll88GF,,ua(+CYYtQ'*FLL' H
 ??c!))$$	  --/#~~/G!!##-  	-ww**,H	-s   F* *$GG)r   r   Nr   )	rT   rU   rV   rW   rX   r   r   r   rY   r>   r   r1   r   r     s    	 G8
,$r   r   c                      \ rS rSrSS jrSrg)cookietransporti  c                    [        [        R                  S5      (       a   [        R                  R                  X5        g g Nr2   )hasattrr   	Transportr2   r/   use_datetimes     r1   r2   cookietransport.__init__  s.    9&&
33((< 4r   r>   Nr   rT   rU   rV   rW   r2   rY   r>   r   r1   r   r     s    =r   r   c                      \ rS rSrSS jrSrg)cookiesafetransporti  c                    [        [        R                  S5      (       a   [        R                  R	                  X5        g g r   )r   r   r   SafeTransportr2   r   s     r1   r2   cookiesafetransport.__init__  s.    9&&
33##,,T@ 4r   r>   Nr   r   r>   r   r1   r   r     s    Ar   r   c                  <    \ rS rSrSrS rS rS rS rS r	S r
S	rg
)bzxmlrpci  zfSupport for access to Bugzilla via the Bugzilla XMLRPC API.

Requires a minimum Bugzilla version 3.4.
c                4   [         R                  X5        U R                  R                  SS5      nUR	                  S5      S-   nU R                  R                  SS5      nU R                  R                  SS5      nU R                  R                  SS5      U l        U R                  R                  SS5      U l        [        R                  [        R                  " U5      U R                  U5      5      U l        U R                  R                  R                  5       S	   R                  S
5      n[!        US   5      U l        [!        US   5      U l        U R                  R&                  R)                  X4SS.5      nUR+                  SS5      U l        g )Nr   r      /s   /xmlrpc.cgir%   r   r   r   r'      .r   r   T)   loginr   s   restrict_login   tokenr   )r)   r2   r,   r-   rstrip	fixstatusfixresolutionr   ServerProxyr	   strurl	transportbzproxyBugzillaversionr   rp   
bzvermajor
bzverminorUserlogingetbztoken)r/   r,   bzwebr7   ri   verr  s          r1   r2   bzxmlrpc.__init__  s4   $#{H5T"^3ww~~k73[9\B!WW^^K9IJ ,,OOE"DNN5$9
 ll##++-j9??Ec!f+c!f+!!''TJ
 yy3/r   c                x    [         R                  R                  US5      S   S:X  a
  [        5       $ [	        5       $ )Ns   httpr   s   https)r   urlrequrlparser   r   )r/   uris     r1   r  bzxmlrpc.transport*  s2    ;;W-a0H<&(("$$r   c                    U R                   R                  R                  U/S/U R                  S.5      nSR	                  US   SU-     S    Vs/ s H  o3S   PM	     sn5      $ s  snf )z0Return a string with all comment text for a bug.   text)   ids   include_fieldsr  r   r   s   %d   comments)r  Bugcommentsr  r`   )r/   r   cts       r1   get_bug_commentsbzxmlrpc.get_bug_comments0  sl    LL%%Twi4<<P
 xx!"7EBJ!7!DE!DAwZ!DE
 	
Es   A&c                   U R                   R                  R                  [        UR	                  5       5      / SU R
                  S.5      nUS    H1  nUS   nU R                  R                  [        S5      U-  5        X	 M3     g )NT)r"  r#  s
   permissiver  s   faultsr   r   )	r  r%  r  sortedr   r  r,   r   r   )r/   r?   probebadbugr   s        r1   r@   bzxmlrpc.filter_real_bug_ids9  sv      $$tyy{+#%#,,	
 I&FBGGNN1782=> 'r   c                
   [        UR                  5       5       Hf  nU R                  U5      R                  [	        U5      5      S:w  d  M2  U R
                  R                  [        S5      U[	        U5      4-  5        X#	 Mh     g )Nr   )r,  r   r)  findr   r,   r   r   r   s       r1   rF   "bzxmlrpc.filter_cset_known_bug_idsG  sj    %B$$R(--eDk:b@BC5;'( H &r   c                   0 nSU;   a  US   US'   U R                   S:  af  U/US'   SU0US'   SU;   a  U R                  US'   U R                  US	'   U R                  US
'   U R                  R
                  R                  U5        g SU;   a$  U R                  R                  [        S5      5        XS'   X5S'   U R                  R
                  R                  U5        g )N   hours	   work_time   r"  s   body   comment   fix   status
   resolutionr  s?   Bugzilla/XMLRPC needs Bugzilla 4.0 or later to mark bugs fixed
r   )r  r
  r  r  r  r%  updater,   r   r   add_comment)r/   rJ   rK   rL   r8   r   s         r1   rM   bzxmlrpc.updatebugP  s    x!)(!3D??a!7DL 'D!"&..Y&*&8&8]#!\\DNLL##D)!0  K#LL((.r   )r  r  r  r  r  r
  N)rT   rU   rV   rW   rX   r2   r  r)  r@   rF   rM   rY   r>   r   r1   r  r    s%    
0.%
/r   r  c                  0    \ rS rSrSrS rS rS rS rSr	g)	bzxmlrpcemailij  a  Read 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.
c                   [         R                  X5        U R                  R                  SS5      U l        U R                  (       d  [
        R                  " [        S5      5      e[        R                  " U R                  5        g )Nr   r   s   configuration 'bzemail' missing)
r  r2   r,   r-   bzemailr   ro   r   r   validateconfigr   s     r1   r2   bzxmlrpcemail.__init__~  sT    $#ww~~k:>||++a BCDDDGG$r   c                    U R                   S:  a  SU[        R                  " U5      4-  $ US:X  a  SnSU[        R                  " U5      4-  $ )Nr7  s   @%s %sr   s   bug_ids   @%s = %s)r  r	   bytestr)r/   	fieldnamevalues      r1   makecommandlinebzxmlrpcemail.makecommandline  sQ    ??a	8+;+;E+BCCCE!%	)X-=-=e-D!EEEr   c                    U R                  U5      nU R                  R                  R                  U/U R                  S.5      nUS   (       d{  U R
                  R                  SS5      nU R                  R                  R                  U/U R                  S.5      nUS   (       d"  [        R                  " [        S5      U-  5      eUS   S   S   nUR                  U R                  SU5      5        S	R                  U5      S
-   U-   n[        R                  " U R
                  5      n[        R                  " U R
                  XX5      n[        R                  " U R
                  U R                   U5      n	[        R"                  " U R
                  Xx5      n
XZS'   XS'   [        R$                  " U R
                  SU5      U
S'   [        R&                  " U R
                  5      nU" XYU
R)                  5       5        g)aq  send 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.
)s   matchr  s   usersr   r%   s(   default bugzilla user %s email not foundr   s   emailr      
s   

s   Froms   Tos   Bug modifications   SubjectN)r:   r  r  r  r  r,   r-   r   ro   r   r   rI  r`   r   	_charsetsaddressencoderB  
mimeencode
headencoders   	as_string)r/   rJ   commandscommentr8   r7   matchesrL   rM  rB  r   sendmails               r1   send_bug_modify_email#bzxmlrpcemail.send_bug_modify_email  s    !!),,,##''v6
 x 77>>+w7Dll''++ 6T\\:G 8$kkABTI  x #H-,,UE:;zz(#g-7NN477+	!!$''4;$$TWWdllIFoodggt7GE
//GG()
J <<(0r   c                8   / nSU;   a$  UR                  U R                  SUS   5      5        SU;   aV  UR                  U R                  SU R                  5      5        UR                  U R                  SU R                  5      5        U R	                  XX45        g )Nr5  r6  r9  s
   bug_statusr;  )r   rI  r
  r  rV  )r/   rJ   rK   rL   r8   cmdss         r1   rM   bzxmlrpcemail.updatebug  s    xKK,,\8H;MNOXKK,,]DNNKLKK,,]D<N<NOP""5@r   )rB  N)
rT   rU   rV   rW   rX   r2   rI  rV  rM   rY   r>   r   r1   r@  r@  j  s    
&%F'1RAr   r@  c                      \ rS rSrSrg)NotFoundi  r>   N)rT   rU   rV   rW   rY   r>   r   r1   r\  r\    s    r   r\  c                  P    \ rS rSrSrS rSS jrS rSS jrS r	S	 r
S
 rS rSrg)	bzrestapii  zNRead and write bugzilla data using the REST API available since
Bugzilla 5.0.
c                   [         R                  X5        U R                  R                  SS5      nSR	                  US/5      U l        U R                  R                  SS5      U l        U R                  R                  SS5      U l        U R                  R                  SS5      U l        U R                  R                  SS5      U l	        U R                  R                  SS	5      U l
        g )
Nr   r   r  s   restr   r%   r   r   r   )r)   r2   r,   r-   r`   bzrootapikeyr7   ri   r
  r  )r/   r,   bzs      r1   r2   bzrestapi.__init__  s    $#WW^^K2iiW.ggnn[)<GGNN;8	ggnn[+>\B!WW^^K9IJr   Nc           	        SR                  U R                  /U Vs/ s H  n[        R                  " U5      PM     sn-   5      n0 nU R                  (       a  U R                  US'   O@U R
                  (       a/  U R                  (       a  U R
                  US'   U R                  US'   U(       a  X%S'   U(       a$  SU[        R                  R                  U5      4-  nU$ s  snf )Nr  s   api_keyr  r   r#  s   %s?%s)
r`   r`  r	   rF  ra  r7   ri   r   r  	urlencode)r/   targetsinclude_fieldsr(  r   qvs         r1   apiurlbzrestapi.apiurl  s    iig(Ng)9)9!)<g(NNO;;![[BzNYY4;;99BxL"kkB{O$2 !c4;;#8#8#<==C
 )Os    C 
c                n    [         R                  " U R                  U5      n[        R                  " UR                  5       5      $ ! [        R                  R                   aO  nUR                  S:X  a  [        R                  " [        S5      5      eUR                  S:X  a
  [        5       ee S nAff = f)N     authorization failed  )r   openr,   r	   
json_loadsr   r   urlerr	httperrorcoder   ro   r   r\  )r/   burlrespinsts       r1   _fetchbzrestapi._fetch  s    		88DGGT*D&&tyy{33{{$$ 	yyCkk!$;"<==yyCj 	s   AA B4%A
B//B4c                X   [         R                  " U5      nUS:X  a&   " S S[        R                  R                  5      nUnO[        R                  R                  nU" XSS05      n [
        R                  " U R                  5      R                  U5      n[        R                  " UR                  5       5      $ ! [        R                  R                   aO  nUR                  S:X  a  [        R                   " [#        S5      5      eUR                  S:X  a
  [%        5       ee S nAff = f)	N   PUTc                      \ rS rSrS rSrg)%bzrestapi._submit.<locals>.putrequesti  c                    g)Nrz  r>   )r/   s    r1   
get_method0bzrestapi._submit.<locals>.putrequest.get_method  s    !r   r>   N)rT   rU   rV   rW   r~  rY   r>   r   r1   
putrequestr|    s    "r   r  s   Content-Types   application/jsonrl  rm  rn  )jsondumpsr   r  r   r   openerr,   ro  r	   rp  r   rq  rr  rs  r   ro   r   r\  )	r/   rt  datamethodr  request_typereqru  rv  s	            r1   _submitbzrestapi._submit  s    zz$V"T[[00 " &L;;..L49L'MN		::dgg&++C0D&&tyy{33{{$$ 	yyCkk!$;"<==yyCj 	s   )AB< <D)A
D$$D)c                    [        5       nU H'  nU R                  SU4SS9n U R                  U5        M)     U H  nX	 M     g! [         a    UR	                  U5         MU  f = f)r=      bugr:  rg  N)setri  rw  r\  add)r/   r?   badbugsrJ   rt  s        r1   r@   bzrestapi.filter_real_bug_ids  sh    %E;;y;ID#D!  E   #E"#s   AA#"A#c                ^  ^ [        U5      mUR                  5        H  nU R                  SUS4SS9nU R                  U5      nUS   [        R
                  " U5         S   n[        U4S jU 5       5      (       d  Mb  U R                  R                  [        S5      UT4-  5        X#	 M     g	)
rD   r  r8  r!  r  r   r$  c              3  4   >#    U  H  nTUS    ;   v   M     g7f)r!  Nr>   ).0r'  sns     r1   	<genexpr>6bzrestapi.filter_cset_known_bug_ids.<locals>.<genexpr>  s     6X27#Xs   r   N)
r   r   ri  rw  r	   rF  anyr,   r   r   )r/   rE   r?   rJ   rt  resultr&  r  s          @r1   rF   #bzrestapi.filter_cset_known_bug_ids  s    4[YY[E;;
+G  D [[&Fgx'7'7'>?LH6X666BCbk" K !r   c                   0 nSU;   a  US   US'   SU;   a  U R                   US'   U R                  US'   U(       aK  USSS.US'   U R                  S	U45      nU R                  XeS
S9  U R                  R                  SU-  5        gU R                  S	US45      nU R                  UUSSS.5        U R                  R                  SU-  5        g)rI   r5  r6  r9  r:  r;  F)r8  s
   is_privates   is_markdownr8  r  rz  )r  s   updated bug %s
s   added comment to bug %s
N)r
  r  ri  r  r,   debug)r/   rJ   rK   rL   r8   bugmodrt  s          r1   rM   bzrestapi.updatebug!  s     x#+H#5F< X $F9$($6$6F=! !$ %"F:
 ;;/DLLfL5GGMM-56;;z:;DLL $#($) GGMM6>?r   c                    grP   r>   rQ   s      r1   rR   bzrestapi.notifyC  s     	r   )ra  r`  r  r
  ri   r7   r   )s   POST)rT   rU   rV   rW   rX   r2   ri  rw  r  r@   rF   rM   rR   rY   r>   r   r1   r^  r^    s2    K
.
   @Dr   r^  c                  >    \ rS rSr\\\\\\	S.r
S rS rS rS rSrg)	bugzillaiL  )s   2.16s   2.18s   3.0s   xmlrpcs   xmlrpc+emails   restapic                P   Xl         X l        U R                   R                  SS5      n [        R                  U   nU" U R                   5      U l	        [        R                  " U R                   R                  SS5      [        R                  5      U l        [        R                  " U R                   R                  SS5      [        R                  5      U l        [        R                  " S5      U l        g ! [
         a#    [        R                  " [        S5      U-  5      ef = f)Nr   r'   s!   bugzilla version %s not supportedr   r   s   \D+)r,   repor-   r  	_versionsr   r   ro   r   bzdriverrecompile
IGNORECASEbug_refix_resplit_re)r/   r,   r  	bzversionbzclasss        r1   r2   bugzilla.__init__X  s    	GGNN;
;		((3G
  (jjGGNN;	2BMM
 jjGGNN;5r}}
 

7+  	++67)C 	s   C8 8-D%c                   Sn0 nU R                   R                  UR                  5       U5      nU R                  R                  UR                  5       U5      n 0 nU(       d	  U(       d  GOU(       d  UnO1U(       d  UnO'UR	                  5       UR	                  5       :  a  UnOUnUR                  5       nXtL a4  U R                   R                  UR                  5       U5      nSU;   a  US	 O/U R                  R                  UR                  5       U5      nSUS'    UR                  S5      n [        UR                  S5      5      n	XS'   U R                  R                  U5       H  n
U
(       d  M  Xc[!        U
5      '   M     GM0  U(       a  U R"                  R%                  U5        U(       a*  U R"                  R'                  UR)                  5       U5        U$ ! [         a    UR                  S5      n Nf = f! [         a     N[         a     N[         a:    U R                  R                  [        S5      UR                  S5      -  5         GN
f = f)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.
r   r9  Nr"  r   r5  s   %s: invalid hours
)r  searchdescriptionr  startendgroup
IndexErrorfloatr   r   r,   r   r   r  r   rp   r  r@   rF   rE   )r/   ctxr  r?   bugmatchfixmatch
bugattribsmrc   hoursr   s              r1   	find_bugsbugzilla.find_bugsm  s    ;;%%coo&7?;;%%coo&7?JH>>#hnn&66 A AEEGE};;--coo.?GZ'"6*;;--coo.?G%)
6"!ggfoNaggh/0',8$ mm))#. *SW /I P MM--d3MM33CHHJE)  !ggaj!
    Nq!781778;LLMNs0   G 1H  G=<G= 
I	I?IIc           
     ,  ^  U 4S jnSnT R                   R                  SS5      nU(       d  T R                   R                  SS5      nU(       d  U(       d  [        S5      n[        R                  " Xe5      n[        R
                  " T R                   T R                  U5      nT R                   R                  5         UR                  UUR                  5       [        R                  " U5      T R                   R                  SS5      T R                  R                  U" T R                  R                  5      S	9  T R                   R                  5       n	T R                  R                  XU	[         R"                  " UR%                  5       5      5        g)
z0update bugzilla bug with reference to changeset.c                   > [        TR                  R                  SS5      5      n[        R                  " U 5      n US:  a.  U R                  S5      nUS:X  a   U $ XS-   S n US-  nUS:  a  M.  U $ )z>strip leading prefix of repo root and turn into
url-safe path.r   r    r   r  r1  r   N)rp   r,   r-   r   pconvertr2  )rootcountr'  r/   s      r1   webroot bugzilla.update.<locals>.webroot  sy     {H=>E==&D!)IIdO7 K EG}
 !) Kr   Nr   r"   r!   sU   changeset {node|short} in repo {root} refers to bug {bug}.
details:
	{desc|tabindent}s   webs   baseurl)changesbughgwebr  r  )r,   r-   r   r   templatespecchangesettemplaterr  
pushbuffershow	changesetr	   rF  r  	popbufferr  rM   r   emailr7   )
r/   rJ   rK   r  r  mapfiletmplspecr(  r  s
   `         r1   r<  bugzilla.update  s"   	 ww~~k;7ggnn[(;Gt?D &&t5))$''499dC	MMO  '''..4DIINN+ 	 	
 ww  "T:#3#3CHHJ#?	
r   c                :    U R                   R                  X5        g)z1ensure Bugzilla users are notified of bug change.N)r  rR   rQ   s      r1   rR   bugzilla.notify  s    T-r   )r  r  r  r  r  r,   N)rT   rU   rV   rW   r[   r   r   r  r@  r^  r  r2   r  r<  rR   rY   r>   r   r1   r  r  L  s3     &I,*7r'
R.r   r  c                   Uc"  [         R                  " [        S5      U-  5      e [        X5      nX   nUR	                  U5      nU(       aS  U H  nUR                  XU   U5        M     UR                  U[        R                  " UR                  5       5      5        gg! [         a;  n	[         R                  " [        S5      [        R                  " U	5      -  5      eSn	A	ff = f)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 ids   Bugzilla error: %s)r   ro   r   r  r  r<  rR   r   r  r7   	Exceptionforcebytestr)
r,   r  hooktyperE   r   rb  r  r?   r  es
             r1   hookr    s     |kk:;hF
 	
	Qbj||C 		#Cy#. IIdJ,,SXXZ89   Qkk!12Z5L5LQ5OOPPQs   A9B" "
C',6C""C'r   )*rX   
__future__r   r  r  r   mercurial.i18nr   mercurial.noder   	mercurialr   r   r   r	   r
   r   r   mercurial.utilsr   r   r   
testedwithconfigtable
configitemdynamicdefaultr)   r[   r   r   r   r   r   r   r   r  r@  LookupErrorr\  r^  r  r  r>   r   r1   <module>r     s  [z #  	      
 NN	 #
!!+.
 

 %
 
 )
 
 	
 	>		 
 
 
 %%
 
 	>	 
 
 
 
 
 
 * *\th tn
7 
, $C$ C$T=,i.A.A =A0)2I2I AZ/x Z/zTAH TAn	{ 	D DNE. E.PQr   