From 1f1f59189c30e26a55e2d2f0be5d27a3de7f4b29 Mon Sep 17 00:00:00 2001 From: Junghyun Kim Date: Wed, 26 Jul 2017 16:42:45 +0900 Subject: [PATCH] revert git config when an exception occurs. PROBLEM: - When an excepition occurs, it is possible that masquerade user remains. SOLUTION: - When an exception occurs, restore original user name. Change-Id: Ida4e2d9f68dde8ccb93e23c25c788a3d5ea58f46 Signed-off-by: Junghyun Kim --- job_add_git_tag.py | 75 +++++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 34 deletions(-) diff --git a/job_add_git_tag.py b/job_add_git_tag.py index fd5057a..03a9e24 100644 --- a/job_add_git_tag.py +++ b/job_add_git_tag.py @@ -33,11 +33,27 @@ from gbp.errors import GbpError sr_tag="" +orig_git_user_name = "" +orig_git_user_email = "" class LocalError(Exception): """Local error exception.""" pass +def revert_git_config(): + global orig_git_user_name, orig_git_user_email + + # revert masqueraded user name/ email + if orig_git_user_name != "": + gitcmd = "git config user.name \"%s\"" % orig_git_user_name + if runner.show(gitcmd)[0] != 0: + raise Exception("failed to execute %s" % gitcmd) + + if orig_git_user_email != "": + gitcmd = "git config user.email \"%s\"" % orig_git_user_email + if runner.show(gitcmd)[0] != 0: + raise Exception("failed to execute %s" % gitcmd) + def main(): print '---[JOB STARTED]-------------------------' @@ -58,15 +74,17 @@ def main(): redis_port = int(os.getenv("REDIS_PORT")) backenddb = BackendDB(redis_host, redis_port) + global orig_git_user_name, orig_git_user_email + # backup previous user.name and user.email - gitcmd = "git config --global user.name" + gitcmd = "git config user.name" rcode, out = runner.show(gitcmd) if rcode != 0: print "failed to execute %s" % gitcmd return 1 orig_git_user_name = out.strip() - gitcmd = "git config --global user.email" + gitcmd = "git config user.email" rcode, out = runner.show(gitcmd) if rcode != 0: print "failed to execute %s" % gitcmd @@ -74,15 +92,13 @@ def main(): orig_git_user_email = out.strip() # masquerade user name/ email - gitcmd = "git config --global user.name \"%s\"" % (event['tagger_username']) + gitcmd = "git config user.name \"%s\"" % (event['tagger_username']) if runner.show(gitcmd)[0] != 0: - print "failed to execute %s" % gitcmd - return 1 + raise Exception("failed to execute %s" % gitcmd) - gitcmd = "git config --global user.email \"%s\"" % (event['tagger_useremail']) + gitcmd = "git config user.email \"%s\"" % (event['tagger_useremail']) if runner.show(gitcmd)[0] != 0: - print "failed to execute %s" % gitcmd - return 1 + raise Exception("failed to execute %s" % gitcmd) os.environ["GIT_AUTHOR_EMAIL"] = event['tagger_useremail'] os.environ["GIT_AUTHOR_NAME"] = event['tagger_username'] @@ -103,13 +119,13 @@ def main(): print "Cloning project %s..." % proj # clone gerrit project to local dir if not clone_gitproject(proj, prjdir): - LocalError('Error cloning %s' % proj) + raise LocalError('Error cloning %s' % proj) mygit = Git(prjdir) branches = mygit.branch_contains(commit) if event['branch'] not in branches: - LocalError("commit %s is not in branch %s" % (commit, event['branch'])) + raise LocalError("commit %s is not in branch %s" % (commit, event['branch'])) for proj, commit in zip(projects, commit_ids): print "Now creating tag for %s" %proj @@ -128,17 +144,7 @@ def main(): global sr_tag sr_tag = tag_name - # revert masqueraded user name/ email - gitcmd = "git config --global user.name \"%s\"" % orig_git_user_name - if runner.show(gitcmd)[0] != 0: - print "failed to execute %s" % gitcmd - return 1 - - gitcmd = "git config --global user.email \"%s\"" % orig_git_user_email - if runner.show(gitcmd)[0] != 0: - print "failed to execute %s" % gitcmd - return 1 - + revert_git_config() return 0 @@ -151,20 +157,21 @@ if __name__ == '__main__': print err error_string = err exit_status = 1 + revert_git_config() -# status is changed to 'Succeeded' or 'Failed' -status_str = "Processing" -if exit_status != 0 : - status_str = "Failed" + # status is changed to 'Succeeded' or 'Failed' + status_str = "Processing" + if exit_status != 0 : + status_str = "Failed" -sr_submit_log_id=(int)(os.getenv("GERRIT_SR_SUBMIT_LOG_ID")) -bm_stage = "Add_Tag" + sr_submit_log_id=(int)(os.getenv("GERRIT_SR_SUBMIT_LOG_ID")) + bm_stage = "Add_Tag" -bm_data = {"bm_stage": bm_stage, - "sr_submit_log_id": sr_submit_log_id, - "sr_tag": sr_tag, - "status": status_str, - "status_reason": error_string} -trigger_next("BUILD-MONITOR", bm_data); + bm_data = {"bm_stage": bm_stage, + "sr_submit_log_id": sr_submit_log_id, + "sr_tag": sr_tag, + "status": status_str, + "status_reason": error_string} + trigger_next("BUILD-MONITOR", bm_data); -sys.exit(exit_status) + sys.exit(exit_status) -- 2.7.4