Page MenuHomePhabricator
Paste P611

Get your gerrit activity as a pretty list of merged commits
ActivePublic

Authored by bd808 on May 5 2015, 10:14 PM.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Bryan Davis and Wikimedia Foundation. All Rights Reserved.
# Licensed under GPLv3
"""
Pretty print Gerrit json query results.
Say you want a list of all the patches that you submitted that were merged in
the last year:
ssh user@gerrit -p 29418 gerrit query --format=JSON -- \
owner:self status:merged -age:1y limit:10000 |
python prettyGerrit.py |
sort > gerrit-report.txt
"""
import collections
import datetime
import json
import sys
def stdin_lines():
while True:
line = sys.stdin.readline()
if not line:
raise StopIteration
yield line
projects = collections.defaultdict(int)
for line in stdin_lines():
line = line.strip()
if line[0] != '{':
continue
rec = json.loads(line)
if 'createdOn' not in rec:
continue
dstamp = datetime.datetime.fromtimestamp(rec['createdOn'])
rec['date'] = dstamp.strftime('%Y-%m-%d')
projects[rec['project']] += 1
print ("%(date)s %(project)s %(subject)s" % rec).encode('utf-8')
for k in sorted(projects, key=projects.get, reverse=True):
print "%s: %d" % (k, projects[k])

Event Timeline

bd808 changed the title of this paste from untitled to Get your gerrit activity as a pretty list of merged commits.
bd808 updated the paste's language from autodetect to python.
bd808 added a project: User-bd808.
NOTE: gerrit's age is based on the last change to the patch so you may get some older things that creep in because someone commented on them long after they were done.

@bd808, thank you for sharing! I made a similar script last year but yours is far better. In case you're interested, here's how you can use your script to track team contributions (number of groups of comments, votes, and amendments to Gerrit patches other than your own):

ssh gerrit.wikimedia.org \
  -p 29418 \
  gerrit query --format=json --comments \
  -- reviewer:self -owner:self -age:1y limit:10000|
jq -c 'select(.comments[]?.reviewer.username == "<USERNAME>")'|
./prettyGerrit.py|
sort

I find this a useful additional metric to consider, especially for junior heavy teams requiring a lot of patch revisions.

And here's kind of a dodgy script for listing Phabricator tickets created by a user since last year.