Rewrite job_deletetag.sh with Python
authorLingchaox Xin <lingchaox.xin@intel.com>
Fri, 7 Dec 2012 10:54:16 +0000 (18:54 +0800)
committerLin Yang <lin.a.yang@intel.com>
Thu, 28 Feb 2013 07:54:52 +0000 (15:54 +0800)
Handle gerrit patchset-created and change_abandoned events, when a new
patchset created, remove all tags from previous patchset; when a change
abandoned, remove all tags from latest patchset.

Change-Id: I7d17e33c73f7afd3a06a73f8c57dccf19138b5a9

job_deletetag.py [new file with mode: 0755]

diff --git a/job_deletetag.py b/job_deletetag.py
new file mode 100755 (executable)
index 0000000..d251715
--- /dev/null
@@ -0,0 +1,82 @@
+#!/usr/bin/env python
+# vim: ai ts=4 sts=4 et sw=4
+
+"""This script will delete all tags on abandoned commit adapted from
+job_deletetag.sh"""
+
+import os
+import tempfile
+import shutil
+
+from common import runner
+from common import utils
+from common import git
+from common.envparas import export
+from common.gerrit import Gerrit
+
+envparas = ['GERRIT_PROJECT',
+            'GERRIT_SSHPORT',
+            'GERRIT_USERNAME',
+            'GERRIT_HOSTNAME',
+            'GERRIT_PATCHSET_NUMBER',
+            'GERRIT_CHANGE_NUMBER',
+            'GERRIT_EVENT_TYPE',
+            'GIT_CACHE_DIR',
+            'WORKSPACE']
+
+export(envparas, locals())
+
+GIT_URL = 'ssh://%s@%s:%s' % (GERRIT_USERNAME, GERRIT_HOSTNAME, GERRIT_SSHPORT)
+
+def cloneTmpGit(tmpdir):
+    gitdir = os.path.join(GIT_CACHE_DIR, GERRIT_PROJECT)
+
+    try:
+        if os.path.exists(gitdir):
+           print '\nuse local repo as reference to clone'
+           if runner.show('git clone %s/%s --reference %s %s' % (GIT_URL, GERRIT_PROJECT, gitdir, '%s/%s' % (tmpdir, GERRIT_PROJECT)))[0]:
+               print 'use locl repo as reference to clone: Failed.'
+    except Exception, ex:
+        print '\nExcept occur when use reference repo to clone code'
+        print ex
+    if not utils.retry(git.update_git_project, (tmpdir, GERRIT_PROJECT, GIT_URL)):
+        shutil.rmtree(tmpdir)
+        exit(-1)
+
+    return git.Git(os.path.join(tmpdir, GERRIT_PROJECT))
+
+def deleteTags(mygit, revision):
+
+    while 1:
+        result = mygit.describe('--tags --exact-match %s' % revision)
+        if result:
+            mygit.push('origin :refs/tags/%s -f' % result)
+            mygit.delete_tag(result)
+        else:
+            break;
+
+if __name__ == '__main__':
+
+    tmpdir = tempfile.mkdtemp(prefix=WORKSPACE+'/')
+    gerrit = Gerrit(GERRIT_HOSTNAME, GERRIT_USERNAME, GERRIT_SSHPORT)
+    patchs = gerrit.query('change:%s --patch-sets' % GERRIT_CHANGE_NUMBER)
+
+    if GERRIT_EVENT_TYPE == 'PATCHSET_CREATED':
+        if GERRIT_PATCHSET_NUMBER != '1':
+            for patch in patchs[0]['patchSets']:
+                if int(patch['number']) == int(GERRIT_PATCHSET_NUMBER)-1:
+                    deleteTags(cloneTmpGit(tmpdir), patch['revision'])
+                    break;
+        else:
+            print 'Less than 2 patchSets, exit...'
+
+    elif GERRIT_EVENT_TYPE == 'CHANGE_ABANDONED':
+        mygit = cloneTmpGit(tmpdir)
+        for patch in patchs[0]['patchSets']:
+            if int(patch['number']) == len(patchs[0]['patchSets']):
+                deleteTags(mygit, patch['revision'])
+                break;
+    else:
+        print 'Nothing to do...'
+
+    shutil.rmtree(tmpdir)