Last active
January 27, 2022 17:35
-
-
Save dgulinobw/07c0b4883e6a5c9e4599d7898c5508bc to your computer and use it in GitHub Desktop.
bitbar/argos gerrit open and unapproved change reviews
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# <bitbar.title>Gerrit Incoming Code Reviews</bitbar.title> | |
# <bitbar.version>v1.1</bitbar.version> | |
# <bitbar.author>Ryan Sydnor</bitbar.author> | |
# <bitbar.author>Drew Gulino</bitbar.author> | |
# <bitbar.author.github>ryansydnor</bitbar.author.github> | |
# <bitbar.desc>Displays your incoming code reviews.</bitbar.desc> | |
# <bitbar.dependencies>python</bitbar.dependencies> | |
import sys | |
import json | |
import subprocess | |
gerrit_user = '$USERNAME' | |
gerrit_host = '$HOST' | |
gerrit_port = '29418' | |
#sys.setdefaultencoding('utf8') | |
def query_gerrit(): | |
gerrit_query = ['ssh', '%s@%s' % (gerrit_user, gerrit_host), '-p', gerrit_port, | |
'gerrit', 'query', '--format=JSON', '--all-approvals', | |
'status:open', 'reviewer:"%s"' % gerrit_user] | |
try: | |
results = subprocess.check_output( | |
gerrit_query, stderr=subprocess.STDOUT, universal_newlines=True) | |
return strip_unused_results(results) | |
except: | |
print('Unable to query gerrit') | |
print('---') | |
print("Ensure you've uploaded your SSH key to gerrit") | |
sys.exit(1) | |
def strip_unused_results(results): | |
# last line is blank | |
# second to last line is an aggregate | |
results = results.split('\n') | |
results = results[:-2] if len(results) > 2 else [] | |
return [json.loads(x) for x in results] | |
def is_self(result): | |
return result.get('owner', {}).get('username', '') == gerrit_user | |
def filter_self(results): | |
return [x for x in results if not is_self(x)] | |
def is_approved(result): | |
approval_total = 0 | |
patch_sets = result.get("patchSets", {}) | |
for patch in patch_sets: | |
approvals = patch.get("approvals",[]) | |
for approval in approvals: | |
value = int(approval.get("value",0)) | |
approval_total += value | |
if approval_total >= 2: | |
return True | |
else: | |
return False | |
def filter_approved(results): | |
return [x for x in results if not is_approved(x)] | |
def print_results(results): | |
num_results = len(results) | |
if num_results == 0: | |
print('%s CRs | dropdown=false' % num_results) | |
else: | |
print('%s CRs :zap: | dropdown=true' % num_results) | |
print('---') | |
for r in results: | |
subj = r.get('subject') | |
url = r.get('url') | |
print('--%s | href=%s' % (subj, url)) | |
def main(): | |
all_incoming = filter_self(query_gerrit()) | |
all_unapproved = filter_approved(all_incoming) | |
print_results(all_unapproved) | |
print("gerrit dashboard | href=https://%s/#/dashboard/self" % (gerrit_host)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment