revert git config when an exception occurs. 42/140742/1
authorJunghyun Kim <jh0822.kim@samsung.com>
Wed, 26 Jul 2017 07:42:45 +0000 (16:42 +0900)
committerJunghyun Kim <jh0822.kim@samsung.com>
Wed, 26 Jul 2017 07:42:45 +0000 (16:42 +0900)
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 <jh0822.kim@samsung.com>
job_add_git_tag.py

index fd5057a..03a9e24 100644 (file)
@@ -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)