Skip to content

Instantly share code, notes, and snippets.

@ahpex
Last active May 31, 2018 14:30
Show Gist options
  • Save ahpex/078523addccdf7ecb301ae8676a323cc to your computer and use it in GitHub Desktop.
Save ahpex/078523addccdf7ecb301ae8676a323cc to your computer and use it in GitHub Desktop.
Retrieve Gerrit changes and pretty-print them on the console
#!/usr/bin/python3
"""
Gerrit response for /changes/ endpoint
[
{
"id": "gerrit~master~Iacd3f2db3728a9c1936170b575016973bf0336d9",
"project": "gerrit",
"branch": "master",
"hashtags": [],
"change_id": "Iacd3f2db3728a9c1936170b575016973bf0336d9",
"subject": "Merge branch \u0027stable-2.15\u0027",
"status": "NEW",
"created": "2018-05-31 12:39:54.000000000",
"updated": "2018-05-31 13:11:19.000000000",
"submit_type": "MERGE_IF_NECESSARY",
"mergeable": true,
"insertions": 259,
"deletions": 79,
"unresolved_comment_count": 0,
"has_review_started": true,
"_number": 182113,
"owner": {
"_account_id": 1011123
},
"requirements": []
}
]
"""
import urllib.request
import json
from datetime import datetime
# pp=0 -> no prettyprinting
params = urllib.parse.urlencode({
'q': 'status:open assignee:logan', # no "+" sign in between!
'pp': 0
})
resp = urllib.request.urlopen("https://gerrit-review.googlesource.com/changes/?%s" % params)
data = resp.read()
# Strip magic first chars (see https://gerrit-review.googlesource.com/Documentation/rest-api.html#output)
changes = json.loads(data.decode("utf-8")[4:])
for change in changes:
respReviewers = urllib.request.urlopen("https://gerrit-review.googlesource.com/changes/%s/reviewers/?pp=0" % change['id'])
# merge reviewers into changes
change['reviewers'] = json.loads(respReviewers.read().decode("utf-8")[4:])
for chg in changes:
print("\033[0;35m#%6s\033[0m \033[1;36m%s\033[0m %s %s" %
(chg['_number'],
chg['updated'][:19],
chg['subject'],
chg['reviewers'][1]
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment