Last active
June 12, 2023 19:32
-
-
Save dgulino/42915dbbe71583e2d421bc5e6fe6fccd to your computer and use it in GitHub Desktop.
waybar custom script to display open gerrit 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 python3 | |
##!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
import os | |
import sys | |
import json | |
import subprocess | |
import logging | |
# "custom/gerrit": { | |
# "format": "G{}", | |
# "exec": "/home/<USERNAME>/.config/gerrit/gerrit_incoming_crs.waybar.py", | |
# "interval": 60, | |
# "return-type": "json", | |
# "on-click": "exec google-chrome https://gerrit.<YOUR_DOMAIN>/#/dashboard/self" | |
# }, | |
# Create the file | |
# and output every level since 'DEBUG' is used | |
# and remove all headers in the output | |
# using empty format='' | |
#logging.basicConfig(filename='/tmp/log.txt', level=logging.DEBUG, format='') | |
gerrit_user = '<USERNAME>' | |
gerrit_host = 'gerrit.<YOUR_DOMAIN>' | |
gerrit_port = '29418' | |
#sys.setdefaultencoding('utf8') | |
def query_gerrit(): | |
ssh_auth_sock_query="find /tmp/ -type s -path '/tmp/ssh-*/agent.*' -user $(whoami) 2>/dev/null | tail -1" | |
ssh_auth_sock_results = subprocess.check_output( | |
ssh_auth_sock_query, stderr=subprocess.STDOUT, universal_newlines=True, shell=True) | |
#logging.debug("ssh_auth_sock_results: %s" % ssh_auth_sock_results) | |
my_env = os.environ.copy() | |
my_env['SSH_AUTH_SOCK'] = ssh_auth_sock_results.strip() | |
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, env=my_env) | |
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): | |
num_results = len(results) | |
if num_results == 0: | |
print(json.dumps({ | |
"text": "0", | |
"tooltip": "href=https://%s/#/dashboard/self" % (gerrit_host) | |
})) | |
else: | |
print(json.dumps({ | |
"text": "⚡%s" % (num_results), | |
"tooltip": "href=https://%s/#/dashboard/self" % (gerrit_host) | |
})) | |
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