hgext/phabricator.py
changeset 43076 2372284d9457
parent 43047 0f90c2d2d7e8
child 43077 687b865b95ad
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
    87 command = eh.command
    87 command = eh.command
    88 configtable = eh.configtable
    88 configtable = eh.configtable
    89 templatekeyword = eh.templatekeyword
    89 templatekeyword = eh.templatekeyword
    90 
    90 
    91 # developer config: phabricator.batchsize
    91 # developer config: phabricator.batchsize
    92 eh.configitem(b'phabricator', b'batchsize',
    92 eh.configitem(
    93     default=12,
    93     b'phabricator', b'batchsize', default=12,
    94 )
    94 )
    95 eh.configitem(b'phabricator', b'callsign',
    95 eh.configitem(
    96     default=None,
    96     b'phabricator', b'callsign', default=None,
    97 )
    97 )
    98 eh.configitem(b'phabricator', b'curlcmd',
    98 eh.configitem(
    99     default=None,
    99     b'phabricator', b'curlcmd', default=None,
   100 )
   100 )
   101 # developer config: phabricator.repophid
   101 # developer config: phabricator.repophid
   102 eh.configitem(b'phabricator', b'repophid',
   102 eh.configitem(
   103     default=None,
   103     b'phabricator', b'repophid', default=None,
   104 )
   104 )
   105 eh.configitem(b'phabricator', b'url',
   105 eh.configitem(
   106     default=None,
   106     b'phabricator', b'url', default=None,
   107 )
   107 )
   108 eh.configitem(b'phabsend', b'confirm',
   108 eh.configitem(
   109     default=False,
   109     b'phabsend', b'confirm', default=False,
   110 )
   110 )
   111 
   111 
   112 colortable = {
   112 colortable = {
   113     b'phabricator.action.created': b'green',
   113     b'phabricator.action.created': b'green',
   114     b'phabricator.action.skipped': b'magenta',
   114     b'phabricator.action.skipped': b'magenta',
   117     b'phabricator.drev': b'bold',
   117     b'phabricator.drev': b'bold',
   118     b'phabricator.node': b'',
   118     b'phabricator.node': b'',
   119 }
   119 }
   120 
   120 
   121 _VCR_FLAGS = [
   121 _VCR_FLAGS = [
   122     (b'', b'test-vcr', b'',
   122     (
   123      _(b'Path to a vcr file. If nonexistent, will record a new vcr transcript'
   123         b'',
   124        b', otherwise will mock all http requests using the specified vcr file.'
   124         b'test-vcr',
   125        b' (ADVANCED)'
   125         b'',
   126      )),
   126         _(
       
   127             b'Path to a vcr file. If nonexistent, will record a new vcr transcript'
       
   128             b', otherwise will mock all http requests using the specified vcr file.'
       
   129             b' (ADVANCED)'
       
   130         ),
       
   131     ),
   127 ]
   132 ]
       
   133 
   128 
   134 
   129 def vcrcommand(name, flags, spec, helpcategory=None, optionalrepo=False):
   135 def vcrcommand(name, flags, spec, helpcategory=None, optionalrepo=False):
   130     fullflags = flags + _VCR_FLAGS
   136     fullflags = flags + _VCR_FLAGS
       
   137 
   131     def hgmatcher(r1, r2):
   138     def hgmatcher(r1, r2):
   132         if r1.uri != r2.uri or r1.method != r2.method:
   139         if r1.uri != r2.uri or r1.method != r2.method:
   133             return False
   140             return False
   134         r1params = r1.body.split(b'&')
   141         r1params = r1.body.split(b'&')
   135         r2params = r2.body.split(b'&')
   142         r2params = r2.body.split(b'&')
   136         return set(r1params) == set(r2params)
   143         return set(r1params) == set(r2params)
   137 
   144 
   138     def sanitiserequest(request):
   145     def sanitiserequest(request):
   139         request.body = re.sub(
   146         request.body = re.sub(
   140             br'cli-[a-z0-9]+',
   147             br'cli-[a-z0-9]+', br'cli-hahayouwish', request.body
   141             br'cli-hahayouwish',
       
   142             request.body
       
   143         )
   148         )
   144         return request
   149         return request
   145 
   150 
   146     def sanitiseresponse(response):
   151     def sanitiseresponse(response):
   147         if r'set-cookie' in response[r'headers']:
   152         if r'set-cookie' in response[r'headers']:
   151     def decorate(fn):
   156     def decorate(fn):
   152         def inner(*args, **kwargs):
   157         def inner(*args, **kwargs):
   153             cassette = pycompat.fsdecode(kwargs.pop(r'test_vcr', None))
   158             cassette = pycompat.fsdecode(kwargs.pop(r'test_vcr', None))
   154             if cassette:
   159             if cassette:
   155                 import hgdemandimport
   160                 import hgdemandimport
       
   161 
   156                 with hgdemandimport.deactivated():
   162                 with hgdemandimport.deactivated():
   157                     import vcr as vcrmod
   163                     import vcr as vcrmod
   158                     import vcr.stubs as stubs
   164                     import vcr.stubs as stubs
       
   165 
   159                     vcr = vcrmod.VCR(
   166                     vcr = vcrmod.VCR(
   160                         serializer=r'json',
   167                         serializer=r'json',
   161                         before_record_request=sanitiserequest,
   168                         before_record_request=sanitiserequest,
   162                         before_record_response=sanitiseresponse,
   169                         before_record_response=sanitiseresponse,
   163                         custom_patches=[
   170                         custom_patches=[
   164                             (urlmod, r'httpconnection',
   171                             (
   165                              stubs.VCRHTTPConnection),
   172                                 urlmod,
   166                             (urlmod, r'httpsconnection',
   173                                 r'httpconnection',
   167                              stubs.VCRHTTPSConnection),
   174                                 stubs.VCRHTTPConnection,
   168                         ])
   175                             ),
       
   176                             (
       
   177                                 urlmod,
       
   178                                 r'httpsconnection',
       
   179                                 stubs.VCRHTTPSConnection,
       
   180                             ),
       
   181                         ],
       
   182                     )
   169                     vcr.register_matcher(r'hgmatcher', hgmatcher)
   183                     vcr.register_matcher(r'hgmatcher', hgmatcher)
   170                     with vcr.use_cassette(cassette, match_on=[r'hgmatcher']):
   184                     with vcr.use_cassette(cassette, match_on=[r'hgmatcher']):
   171                         return fn(*args, **kwargs)
   185                         return fn(*args, **kwargs)
   172             return fn(*args, **kwargs)
   186             return fn(*args, **kwargs)
       
   187 
   173         inner.__name__ = fn.__name__
   188         inner.__name__ = fn.__name__
   174         inner.__doc__ = fn.__doc__
   189         inner.__doc__ = fn.__doc__
   175         return command(name, fullflags, spec, helpcategory=helpcategory,
   190         return command(
   176                        optionalrepo=optionalrepo)(inner)
   191             name,
       
   192             fullflags,
       
   193             spec,
       
   194             helpcategory=helpcategory,