--- /dev/null
+#!/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)