-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathphabricator-query.py
executable file
·71 lines (59 loc) · 2.45 KB
/
phabricator-query.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
# Retrieves release notes from Phabricator
# Usage:
# 1. Go to https://phabricator.example.com/maniphest/
# 2. Fill the search form and run "Search". You will be redirected to a URL like
# https://phabricator.example.com/maniphest/query/<queryKey>/#R
# Copy the <queryKey> part. That will be your -q/--query-key argument
# 3. Take the query key part out of that URL
# 4. Go to https://phabricator.example.com/settings/user/<user>/page/apitokens/
# and generate yourself a token. It goes to -t/--api-token
# 5. Now run phabricator-query.py -t <API key> -q <queryKey> https://phabricator.example.com
import re
import sys
import json
import argparse
import urllib.request
parser = argparse.ArgumentParser()
parser.add_argument("-q", "--query-key", type=str, help="Query key", required=True)
parser.add_argument("-t", "--api-token", type=str, help="API token", required=True)
parser.add_argument("-f", "--format", type=str, default="html", help="Output format (html, rst, md, or plain)")
parser.add_argument("-c", "--constraint", type=str, help="Search constraint", required=False)
parser.add_argument("phabricator_url", type=str, help="Phabricator URL")
args = parser.parse_args()
## Templates
template_html = """<tr>
<td><a href="{base_url}/T{id}">{id}</a></td>
<td>{title}</td>
</tr>"""
template_plain = "T{id}\t{title}"
template_rst = "* `T{id} <{base_url}/T{id}>`_ {title}"
template_md = "* [T{id}]({base_url}/T{id}) {title}"
if args.format == 'html':
template = template_html
elif args.format == 'rst':
template = template_rst
elif args.format == 'md':
template = template_md
else:
template = template_plain
# Do the deed
query_url = "{0}/api/maniphest.search".format(args.phabricator_url)
request_data = urllib.parse.urlencode({'api.token': args.api_token, 'queryKey': args.query_key}).encode('ascii')
with urllib.request.urlopen(query_url, request_data) as f:
resp = json.load(f)
data = resp["result"]["data"]
for t in data:
if args.constraint:
data = re.split(r'\s*=\s*', args.constraint)
if len(data) != 2:
raise ValueError("Malformed constraint: {}".format(args.constraint))
else:
field = data[0]
value = data[1]
if not (t["fields"][field] == value):
continue
if args.format == 'json':
print(json.dumps(t))
else:
print(template.format(id=t["id"], title=t["fields"]["name"], base_url=args.phabricator_url))