Last active
June 15, 2023 14:34
-
-
Save globau/29029ddf3af3dbec30b6e037b67b0c38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from pathlib import Path | |
import argparse | |
import functools | |
import json | |
import re | |
import urllib.request as url_request | |
import urllib.parse as url_parse | |
PHURL = "https://phabricator.services.mozilla.com/" | |
@functools.lru_cache() | |
def api_token(): | |
try: | |
arc_rc = json.loads((Path.home() / ".arcrc").read_text()) | |
return arc_rc["hosts"][f"{PHURL.rstrip('/')}/api/"]["token"] | |
except (ValueError, FileNotFoundError, KeyError): | |
raise Exception(f"Failed to find API-KEY for {PHURL}") | |
def conduit(method, **kwargs): | |
req = url_request.Request( | |
f"{PHURL.rstrip('/')}/api/{method}", | |
method="POST", | |
data=url_parse.urlencode( | |
{ | |
"params": json.dumps({**kwargs, "__conduit__": {"token": api_token()}}), | |
"output": "json", | |
"__conduit__": True, | |
} | |
).encode(), | |
) | |
with url_request.urlopen(req) as r: | |
res = json.load(r) | |
if res["error_code"] and res["error_info"]: | |
print(req.full_url) | |
print(url_parse.unquote(req.data.decode()).replace("+", " ")) | |
raise Exception(res["error_info"]) | |
return res["result"] | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("revision", help="revision ID or URL") | |
args = parser.parse_args() | |
m = re.search(rf"^(?:{re.escape(PHURL)})?D?(\d+)(?:\?|$)", args.revision) | |
if m: | |
revision = int(m.group(1)) | |
else: | |
raise ValueError("invalid revision id or url") | |
print(f"loading {PHURL}D{revision}") | |
res = conduit("differential.querydiffs", revisionIDs=[revision]) | |
for rev_id in sorted(res.keys()): | |
print(f"{PHURL}D{revision}?id={rev_id}: {res[rev_id]['creationMethod']}") | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment