2015-11-10 00:24:13 +03:00
|
|
|
# phabstatus.py
|
|
|
|
#
|
|
|
|
# Copyright 2015 Facebook, Inc.
|
|
|
|
#
|
|
|
|
# This software may be used and distributed according to the terms of the
|
|
|
|
# GNU General Public License version 2 or any later version.
|
|
|
|
|
2016-01-11 21:30:13 +03:00
|
|
|
from mercurial import templatekw, extensions
|
2015-11-10 00:24:13 +03:00
|
|
|
from mercurial import util as hgutil
|
|
|
|
|
|
|
|
import re
|
2016-01-11 21:30:13 +03:00
|
|
|
from pprint import pprint as pp
|
2015-11-10 00:24:13 +03:00
|
|
|
import subprocess
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
|
2015-11-17 18:13:12 +03:00
|
|
|
def memoize(f):
|
2016-01-11 21:30:13 +03:00
|
|
|
"""
|
|
|
|
NOTE: This is a hack
|
|
|
|
if f args are like (a, b1, b2, b3) and returns [o1, o2, o3] where
|
|
|
|
o1, o2, o3 are output of f respectively for (a, b1), (a, b2) and
|
|
|
|
(a, b3) then we memoize f(a, b1, b2, b3)'s result but also
|
|
|
|
f(a, b1) => o1 , f(a, b2) => o2 and f(a, b3) => o3.
|
|
|
|
Example:
|
|
|
|
|
|
|
|
>>> partialsum = lambda a, *b: [a + bn for bn in b]
|
|
|
|
>>> partialsum = memoize(partialsum)
|
|
|
|
|
|
|
|
Create a class that wraps the integer '3', otherwise we cannot add
|
|
|
|
_phabstatuscache to it for the test
|
|
|
|
>>> class IntWrapperClass(int):
|
|
|
|
... def __new__(cls, *args, **kwargs):
|
|
|
|
... return super(IntWrapperClass, cls).__new__(cls, 3)
|
|
|
|
|
|
|
|
>>> three = IntWrapperClass()
|
|
|
|
>>> partialsum(three, 1, 2, 3)
|
|
|
|
[4, 5, 6]
|
|
|
|
|
|
|
|
As expected, we have 4 entries in the cache for a call like f(a, b, c, d)
|
|
|
|
>>> pp(three._phabstatuscache)
|
|
|
|
{(3, 1): [4], (3, 1, 2, 3): [4, 5, 6], (3, 2): [5], (3, 3): [6]}
|
|
|
|
"""
|
2015-11-17 18:13:12 +03:00
|
|
|
def helper(*args):
|
|
|
|
repo = args[0]
|
|
|
|
if not hgutil.safehasattr(repo, '_phabstatuscache'):
|
|
|
|
repo._phabstatuscache = {}
|
|
|
|
if args not in repo._phabstatuscache:
|
2016-01-11 21:30:13 +03:00
|
|
|
u = f(*args)
|
|
|
|
repo._phabstatuscache[args] = u
|
|
|
|
if isinstance(u, list):
|
|
|
|
revs = args[1:]
|
|
|
|
for x, r in enumerate(revs):
|
|
|
|
repo._phabstatuscache[(repo, r)] = [u[x]]
|
2015-11-17 18:13:12 +03:00
|
|
|
return repo._phabstatuscache[args]
|
|
|
|
return helper
|
|
|
|
|
|
|
|
@memoize
|
2016-01-11 21:30:13 +03:00
|
|
|
def getdiffstatus(repo, *diffid):
|
2015-11-10 00:24:13 +03:00
|
|
|
"""Perform a Conduit API call by shelling out to `arc`
|
|
|
|
|
|
|
|
Returns status of the diff"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
proc = subprocess.Popen(['arc', 'call-conduit', 'differential.query'],
|
2016-01-08 05:30:24 +03:00
|
|
|
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
|
|
|
|
preexec_fn=os.setsid)
|
2016-01-11 21:30:13 +03:00
|
|
|
input = json.dumps({'ids': diffid})
|
2016-01-08 05:30:24 +03:00
|
|
|
repo.ui.debug("[diffrev] echo '%s' | "
|
|
|
|
"arc call-conduit differential.query\n" %
|
2015-11-10 00:24:13 +03:00
|
|
|
input)
|
|
|
|
proc.stdin.write(input)
|
|
|
|
proc.stdin.close()
|
2016-01-08 05:30:24 +03:00
|
|
|
resp = proc.stdout.read()
|
2015-11-10 00:24:13 +03:00
|
|
|
jsresp = json.loads(resp)
|
|
|
|
if not jsresp:
|
|
|
|
return 'Could not decode Conduit response'
|
|
|
|
|
|
|
|
resp = jsresp.get('response')
|
|
|
|
if not resp:
|
|
|
|
error = jsresp.get('errorMessage', 'unknown error')
|
|
|
|
return error
|
2016-01-11 21:30:13 +03:00
|
|
|
|
|
|
|
# This makes the code more robust in case conduit does not return
|
|
|
|
# what we need
|
|
|
|
result = []
|
|
|
|
for diff in diffid:
|
|
|
|
matchingresponse = [r for r in resp
|
|
|
|
if r.get("id", None) == int(diff)]
|
|
|
|
if not matchingresponse:
|
|
|
|
result.append("Error")
|
|
|
|
else:
|
|
|
|
result.append(matchingresponse[0].get('statusName'))
|
|
|
|
return result
|
2016-01-08 05:30:24 +03:00
|
|
|
except Exception as e:
|
2015-11-10 00:24:13 +03:00
|
|
|
return 'Could not not call "arc call-conduit": %s' % e
|
|
|
|
|
|
|
|
def showphabstatus(repo, ctx, templ, **args):
|
2015-12-08 22:28:38 +03:00
|
|
|
""":phabstatus: String. Return the diff approval status for a given hg rev
|
|
|
|
"""
|
2016-01-11 21:30:13 +03:00
|
|
|
if hgutil.safehasattr(repo, '_smartlogrevs'):
|
|
|
|
alldiffnumbers = [getdiffnum(repo, repo[rev])
|
|
|
|
for rev in repo._smartlogrevs]
|
|
|
|
okdiffnumbers = [d for d in alldiffnumbers if d is not None]
|
|
|
|
# To populate the cache, the result will be used by the templater
|
|
|
|
getdiffstatus(repo, *okdiffnumbers)
|
|
|
|
# Do this once per smartlog call, not for every revs to be displayed
|
|
|
|
del repo._smartlogrevs
|
|
|
|
|
|
|
|
diffnum = getdiffnum(repo, ctx)
|
|
|
|
if diffnum is not None:
|
|
|
|
return getdiffstatus(repo, diffnum)[0]
|
|
|
|
|
|
|
|
def getdiffnum(repo, ctx):
|
2015-11-10 00:24:13 +03:00
|
|
|
descr = ctx.description()
|
2016-01-08 05:30:24 +03:00
|
|
|
match = re.search('Differential Revision: https://phabricator.fb.com/(D\d+)'
|
|
|
|
, descr)
|
2015-11-10 00:24:13 +03:00
|
|
|
revstr = match.group(1) if match else ''
|
|
|
|
if revstr.startswith('D') and revstr[1:].isdigit():
|
2016-01-11 21:30:13 +03:00
|
|
|
return revstr[1:]
|
|
|
|
return None
|
2015-11-10 00:24:13 +03:00
|
|
|
|
2016-01-11 21:30:13 +03:00
|
|
|
def _getdag(orig, *args):
|
|
|
|
repo = args[1]
|
|
|
|
# We retain the smartlogrevision, this way showphabstatus knows that there
|
|
|
|
# are multiple revisions to resolve
|
|
|
|
repo._smartlogrevs = args[2]
|
|
|
|
return orig(*args)
|
2015-11-10 00:24:13 +03:00
|
|
|
|
|
|
|
def extsetup(ui):
|
|
|
|
templatekw.keywords['phabstatus'] = showphabstatus
|
2016-01-11 21:30:13 +03:00
|
|
|
smartlog = extensions.find("smartlog")
|
|
|
|
extensions.wrapfunction(smartlog, 'getdag', _getdag)
|