from common.git import Git, clone_gitproject
from common import obspkg
from common import mapping
-from common.gerrit import Gerrit, get_gerrit_event
+from common.gerrit import Gerrit, get_gerrit_event, GerritError
from common.send_mail import prepare_mail
from common.buildservice import BuildService
return [prj for prj in set(obs_prjs) if not bs.exists(prj)]
+def sync_ref(event, git, user, host, port):
+ """
+ Push ref to gerrit. Create project if necessary
+ Return: True if sync was successful, False otherwise.
+ """
+
+ try:
+ mygerrit = Gerrit(host, user, port)
+ mygerrit.create_project(event['project'], '-p',
+ os.getenv('GERRIT_PARENT_PROJECT'))
+ except GerritError, err:
+ print >> sys.stderr, "Error creating gerrit project %s: %s" % \
+ (event['project'], str(err))
+
+ repo = 'ssh://%s@%s:%s/%s' % (user, host, port, event['project'])
+ refname = event['refname']
+ try:
+ if event['newrev'] == '0000000000000000000000000000000000000000':
+ git.push(repo=repo, src=':%s' % event['refname'])
+ elif event['refname'].startswith("refs/tags/"):
+ git.push(repo=repo, src=refname, tags=True, force=True)
+ else:
+ git.push(repo=repo, src='origin/%s' % refname,
+ dst='refs/heads/%s' % refname, tags=True, force=True)
+ except GitRepositoryError, err:
+ print >> sys.stderr, "Error pushing %s to %s: %s" % (refname, repo, err)
+ return False
+
+ return True
+
def main():
"""script entry point"""
+ print '---[JOB STARTED]----------------------------------------'
+
# prepare related global variables
workspace = os.getenv('WORKSPACE')
gitcache = os.getenv('GIT_CACHE_DIR')
apipasswd = os.getenv('OBS_API_PASSWD')
event = get_gerrit_event()
- # prepare separate temp directory for each build
- tmpdir = tempfile.mkdtemp(prefix=workspace+'/')
- prjdir = os.path.join(tmpdir, event['project'])
- print '---[JOB STARTED]----------------------------------------'
- # check whether tag name is start with 'submit/'
- if event['event_type'] == "ref-updated":
- if not event['refname'].startswith('refs/tags/submit/'):
- print '\nREFNAME "%s" isn\'t start with refs/tags/submit, exit '\
- 'now' % event['refname']
- return 0
- elif is_tag_deleted(event['oldrev'], event['newrev']):
- print '\nREFNAME "%s" is deleted, exit now' % event['refname']
- return 0
+ if event['event_type'] != "ref-updated":
+ # This is just a sanity check as ref-updated is the only event we
+ # react on and it's configured in the job configuraion
+ print >> sys.stderr, "Configuration error: This job can't process"\
+ "event %s! Only ref-updated events are allowed" % \
+ event['event_type']
+ return 1
if os.getenv('SYNC_GERRIT_PROJECT_PATTERN'):
# check whether need to sync this project to inside obs
print 'Do not need to sync project %s, exit now' % event['project']
return 0
+ if event['refname'].startswith('refs/changes/'):
+ print 'Do not need to process refname %s, exit now' % event['refname']
+ return 0
+
+ # prepare separate temp directory for each build
+ tmpdir = tempfile.mkdtemp(prefix=workspace+'/')
+ prjdir = os.path.join(tmpdir, event['project'])
+
+ # clone gerrit project to local dir
+ if not clone_gitproject(event['project'], prjdir):
+ print >> sys.stderr, 'Error cloning %s' % event['project']
+ return 1
+ mygit = Git(prjdir)
+
+ # sync refs if it's sync instance
+ sync_user = os.getenv('SYNC_GERRIT_USERNAME')
+ sync_host = os.getenv('SYNC_GERRIT_HOSTNAME')
+ sync_port = os.getenv('SYNC_GERRIT_SSHPORT')
+ if sync_user and sync_host:
+ if not sync_ref(event, mygit, sync_user, sync_host, sync_port):
+ return 1
+
+ # check whether tag name is start with 'submit/'
+ if not event['refname'].startswith('refs/tags/submit/'):
+ print '\nREFNAME "%s" isn\'t start with refs/tags/submit, exit now'\
+ % event['refname']
+ return 0
+ elif is_tag_deleted(event['oldrev'], event['newrev']):
+ print '\nREFNAME "%s" is deleted, exit now' % event['refname']
+ return 0
+
obs_mapping = '%s/%s/git-obs-mapping.xml' % (gitcache, mapping_prj)
# check whether git-obs-mapping.xml exist in local
if not os.path.isfile(obs_mapping):
print '\nThis project do not map to any OBS project, exit now'
return 0
- # clone gerrit project to local dir
- if not clone_gitproject(event['project'], prjdir):
- return 1
-
- mygit = Git(prjdir)
mygerrit = Gerrit(event['hostname'], event['username'], \
event['sshport'], int(os.getenv('GERRIT_SILENT_MODE')))
+++ /dev/null
-#!/usr/bin/env python
-# vim: ai ts=4 sts=4 et sw=4
-
-"""This script will sync a merged change in gerrit to OBS corresponding project.
-"""
-
-import os
-import sys
-import tempfile
-import shutil
-import re
-
-# internal module
-from common.git import Git, clone_gitproject
-from common.gerrit import get_gerrit_event
-from common import gerrit
-
-NULL_COMMIT = '0000000000000000000000000000000000000000'
-
-def end(rc = 0, tmpdir = None):
- # cleanup workspace
- if tmpdir and os.path.isdir(tmpdir):
- shutil.rmtree(tmpdir)
- return rc
-
-def main():
-
- print '---[JOB STARTED]----------------------------------------'
-
- event = get_gerrit_event()
-
- # check whether need to sync this project to inside gerrit
- r = re.compile(os.getenv('SYNC_GERRIT_PROJECT_PATTERN'))
- if not r.match(event['project']):
- print 'Do not need to sync project %s, exit now' % event['project']
- return end(0)
-
- if event['refname'].startswith('refs/changes/'):
- print 'Do not need to sync REFNAME %s, exit now' % event['refname']
- return end(0)
-
- workspace = os.getenv('WORKSPACE')
- tmpdir = tempfile.mkdtemp(prefix=workspace+'/')
- prjdir = os.path.join(tmpdir, event['project'])
-
- tz_git_url = 'ssh://%s@%s:%s' % (os.getenv('SYNC_GERRIT_USERNAME'),
- os.getenv('SYNC_GERRIT_HOSTNAME'),
- os.getenv('SYNC_GERRIT_SSHPORT'))
-
- # clone gerrit project to local dir
- if not clone_gitproject(event['project'], prjdir):
- return end(1, tmpdir)
-
- mygit = Git(prjdir)
-
- try:
- mygerrit = gerrit.Gerrit(os.getenv('SYNC_GERRIT_HOSTNAME'),
- os.getenv('SYNC_GERRIT_USERNAME'),
- os.getenv('SYNC_GERRIT_SSHPORT'))
-
- mygerrit.create_project(event['project'], '-p',
- os.getenv('GERRIT_PARENT_PROJECT'))
- except gerrit.GerritError, err:
- print >> sys.stderr, "Error creating gerrit project %s: %s" % \
- (event['project'], str(err))
-
- if event['newrev'] == NULL_COMMIT:
- mygit.push(repo='%s/%s' % (tz_git_url, event['project']), src=':%s' % event['refname'])
- else:
- if event['refname'].startswith("refs/tags/"):
- mygit.push(repo='%s/%s' % (tz_git_url, event['project']),
- src=event['refname'], tags=True, force=True)
- else:
- mygit.push(repo='%s/%s' % (tz_git_url, event['project']),
- src='origin/%s' % event['refname'], dst='refs/heads/%s' %
- event['refname'], tags=True, force=True)
-
- return end(0, tmpdir)
-
-if __name__ == '__main__':
- sys.exit(main())