Created
December 17, 2025 20:40
-
-
Save dgulino/d0f3679ca1fbefc75363db6fd01a2f1f to your computer and use it in GitHub Desktop.
xBar gerrit review monitor
This file contains hidden or 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 python3 | |
| ##!/usr/bin/python3 | |
| # -*- coding: utf-8 -*- | |
| import os | |
| import sys | |
| import json | |
| import subprocess | |
| import logging | |
| gerrit_user = '$USER' | |
| gerrit_host = '$GERRIT_URL' | |
| gerrit_port = '29418' | |
| 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 Exception: | |
| logging.error('Unable to query gerrit') | |
| logging.error('---') | |
| logging.error("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): | |
| if len(results) > 0: | |
| print('%s CRs' % len(results)) | |
| print('---') | |
| for r in results: | |
| subj = r.get('subject') | |
| url = r.get('url') | |
| print('%s | href=%s' % (subj, url)) | |
| else: | |
| print('x') | |
| print('---') | |
| def main(): | |
| all_unapproved = [] | |
| all_incoming = filter_self(query_gerrit()) | |
| all_unapproved = filter_approved(all_incoming) | |
| print_results(all_unapproved) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment