Skip to content

Instantly share code, notes, and snippets.

@globau
Last active February 14, 2023 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save globau/7fe0f060f73d91beb843fcb83a8f4ba2 to your computer and use it in GitHub Desktop.
Save globau/7fe0f060f73d91beb843fcb83a8f4ba2 to your computer and use it in GitHub Desktop.
my-phabricator-activity.py
#!/usr/bin/env python3
import argparse
import datetime
import functools
import json
import urllib.parse as url_parse
import urllib.request as url_request
from pathlib import Path
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"]["%s/api/" % PHURL.rstrip("/")]["token"]
except (ValueError, FileNotFoundError, KeyError):
raise Exception("Failed to find API-KEY for %s" % PHURL)
def conduit(method, **kwargs):
req = url_request.Request(
"%s/api/%s" % (PHURL.rstrip("/"), 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"]:
raise Exception(res["error_info"])
return res["result"]
def ymd(value):
try:
return datetime.datetime.strptime(value, "%Y-%m-%d")
except ValueError:
raise argparse.ArgumentTypeError("Invalid YYYY-MM-DD date: %s" % value)
def print_revisions(title, constraints):
revisions = conduit(
"differential.revision.search", constraints=constraints, order="newest",
)
has_more = revisions["cursor"]["after"] is not None
revisions = revisions["data"]
print("\n%s: %s%s" % (title, "more than " if has_more else "", len(revisions)))
if not revisions:
return
print("")
for revision in reversed(revisions):
fields = revision["fields"]
date_created = datetime.datetime.utcfromtimestamp(fields["dateCreated"])
print(
"%5s %s %s"
% (
"D%s" % revision["id"],
date_created.strftime("%Y-%m-%d"),
fields["title"],
)
)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--since", "-s", type=ymd, help="Start date (defaults to 4 weeks ago)"
)
args = parser.parse_args()
if not args.since:
args.since = datetime.datetime.today() - datetime.timedelta(weeks=4)
since = args.since.replace(hour=0, minute=0, second=0, microsecond=0)
user_phid = conduit("user.whoami")["phid"]
print("Finding revisions created since %s" % args.since.strftime("%Y-%m-%d"))
since = int(since.timestamp())
print_revisions("Authored", dict(authorPHIDs=[user_phid], createdStart=since))
print_revisions("Reviewed", dict(reviewerPHIDs=[user_phid], createdStart=since))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment