From 6e5a6ae98cf056c308ebf1984f48ac1ac8262eda Mon Sep 17 00:00:00 2001 From: FrantiĊĦek Zatloukal Date: Nov 30 2019 10:03:00 +0000 Subject: Comment/karma submit: just use bodhi-client CLI called by os.system --- diff --git a/fedora-easy-karma.py b/fedora-easy-karma.py index c1dfb61..069ec5c 100755 --- a/fedora-easy-karma.py +++ b/fedora-easy-karma.py @@ -30,12 +30,14 @@ import sys import re from optparse import OptionParser +from shlex import quote from textwrap import wrap # extra python modules # Used to catch fedora.client.AuthError and fedora.client.ServerError # exceptions import fedora +import bodhi # fedora_cert is optional. It is only used to get the real fas_username, which # is also supplied as a command line option and eventually in a config file. @@ -190,8 +192,6 @@ class FEK_helper(object): # the format of the user has changed, add a data member comment["username"] = comment["user"]["name"] - if comment["anonymous"]: - comment["username"] += " (unauthenticated)" comments.append( "%(indent)s%(username)s - %(timestamp)s " @@ -222,6 +222,7 @@ class FEK_helper(object): values["karma_status"] = "%d/%s" % (values["karma"], values["stable_karma"]) + values["submitter"] = values["user"]["name"] return format_string % values @staticmethod @@ -653,7 +654,7 @@ class FedoraEasyKarma(object): # Ignore own updates if self.options.ignore_own and \ - update["submitter"] == self.options.fas_username: + update["user"]["name"] == self.options.fas_username: continue if update not in processed_updates and \ @@ -773,7 +774,7 @@ class FedoraEasyKarma(object): def already_commented(self, update, user): for comment in update["comments"]: - if not comment["anonymous"] and comment["user"]["name"] == user: + if comment["user"]["name"] == user: return True return False @@ -828,22 +829,31 @@ class FedoraEasyKarma(object): pass def send_comment(self, bc, update, comment, karma): - orig_retries = bc.retries - bc.retries = 1 - for retry in range(0, self.options.retries + 1): + # Prepare command for bodhi CLI + command = "bodhi updates comment --karma {} --user {} {} {}".format( + karma, self.options.fas_username, update["updateid"], quote(comment) + ) + res = os.system(command) + if res != 0: + # If return code isn't 0, something went wrong, try to explain or workaround some cases + if res == 1: + return (False, 'Failed to connect to bodhi, is your internet connection working?') + # Try to workaround https://github.com/fedora-infra/bodhi/issues/3298 + homedir = os.path.expanduser("~") + if not homedir: + # Bail out if we weren't able to get homedir + return (False, 'Failed to get homedir') + self.warning("Working around https://github.com/fedora-infra/bodhi/issues/3298 ...") try: - res = bc.comment(update["title"], comment, karma=karma) - bc.retries = orig_retries - return (True, res) - except fedora.client.AuthError as e: - self.warning("Authentication error") - bc.password = getpass.getpass('FAS password for %s: ' % - self.options.fas_username) - except fedora.client.ServerError as e: - self.warning("Server error: %s" % str(e)) - - bc.retries = orig_retries - return (False, 'too many errors') + os.remove(homedir + '/.fedora/openidbaseclient-sessions.cache') + except FileNotFoundError: + return (False, 'Failed to remove ~/.fedora/openidbaseclient-sessions.cache') + res = os.system(command) + if res != 0: + return (False, 'Failed to submit comment and karma') + return (True, res) + + return (True, res) if __name__ == "__main__":