-
-
Save montehurd/40ab4dd5cc81b66d78aaaa46752b1f5f to your computer and use it in GitHub Desktop.
Example use of 2 Phabricator APIs. Executes a saved task query, extracts portions of each result, modifies them, converts them to HTML and sends them to Safari for instant preview. Approach used below is for purpose of quick prototyping. Tested on macOS, where you can simply paste the *entire* example into Terminal and press return:
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
#!/bin/bash | |
# Edit: This code is just horrible, but it was early in my shell scripting journey. | |
# This script does the following: | |
# | |
# Hits the "maniphest.search" API using a saved query's "query key": | |
# Any query key may be used. Simply construct your query using Phabricator's "Advanced Search" interface: | |
# https://phabricator.wikimedia.org/maniphest/query/advanced/ | |
# When you have it fetching what you want, use the "Save Query" button, then grab the saved query's key from its URL. | |
# Presently it's using this query I constructed for testing purposes: | |
# https://phabricator.wikimedia.org/maniphest/query/8Nu74C_eapwI | |
# | |
# Transforms the results a bit: | |
# Chooses a few fields from each result and adds a little "remarkup" to each result's ticket description remarkup. | |
# | |
# Hits the "remarkup.process" API: | |
# Converts our transformed remarkup to HTML. | |
# | |
# Wraps results in some structural HTML: | |
# Ensures things like links and styles work. | |
# | |
# Sends everthing to a browser: | |
# So you can instantly see the results. | |
# Settings: | |
BASE_URL="https://phabricator.wikimedia.org" | |
API_TOKEN="api-2lfamy3hsjzihfo6jwmnbd6lphn2" | |
QUERY_KEY="8Nu74C_eapwI" | |
# Result transformation script: | |
read -r -d '' RESULT_TRANSFORMATION << '--END' | |
import sys, json, urllib; | |
resultDicts = json.load(sys.stdin)['result']['data']; | |
remarkup = '\n{F31669139 size=full}\n'.join(list(map(lambda item: | |
'''== T{number} == | |
== {name} == | |
{description} | |
''' | |
.format( | |
number=item['id'], | |
name=item['fields']['name'].encode('utf-8'), | |
priority=item['fields']['priority']['name'].encode('utf-8'), | |
description=item['fields']['description']['raw'].encode('utf-8') | |
), resultDicts))); | |
encodedRemarkup = urllib.quote_plus(remarkup); | |
print('contents[0]=' + encodedRemarkup); | |
--END | |
# Page presentation script: | |
read -r -d '' PAGE_PRESENTATION << --END | |
import sys, json; | |
html = json.load(sys.stdin)['result'][0]['content']; | |
print(''' | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<link rel="stylesheet" type="text/css" href="https://phab.wmfusercontent.org/res/defaultX/phabricator/31c46c5c/core.pkg.css"> | |
<base href="$BASE_URL" target="_blank"> | |
</head> | |
<body style="padding:20pt"> | |
<div class="phabricator-remarkup"> | |
{html} | |
</div> | |
</body> | |
</html> | |
'''.format(html=html)); | |
--END | |
# Use the query key to hit the "maniphest.search" API: | |
curl -s "$BASE_URL/api/maniphest.search" \ | |
-d "api.token=$API_TOKEN" \ | |
-d "order=priority" \ | |
-d "queryKey=$QUERY_KEY" | | |
# Pipe the results to Python for processing: | |
# - Extracts each result | |
# - Lightly transforms each result to pretty things up by joining each phab ticket number, title ("name") and description (which is remarkup https://secure.phabricator.com/book/phabricator/article/remarkup/) around a remarkup spacer ("F31669139") | |
# This "RESULT_TRANSFORMATION" script would be the place to extract some sought description remarkup sub-string if we wanted to further mine/reduce tickets in some way. | |
python -c "$RESULT_TRANSFORMATION" | | |
# Pipe our remixed remarkup to "remarkup.process" to convert it to HTML: | |
curl -s "$BASE_URL/api/remarkup.process" \ | |
-d "api.token=$API_TOKEN" \ | |
-d "context=phriction" \ | |
-d @- | | |
# Extract HTML, add stylesheet and base URL so it looks nice and links work, then send all of it to a browser so we can see it: | |
python -c "$PAGE_PRESENTATION" > /tmp/phabricator.tmp.html && open -a "Safari" /tmp/phabricator.tmp.html | |
# To see html as text use line below instead of line above: | |
# python -c "$PAGE_PRESENTATION" | open -a "Safari" -f | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A version of the script with functional "Quick options" buttons for Status and Priority, with Comment support.
This script also moves all fetching and processing into Python.
This version moves everything to Python3.