def create_job(branch, template, config, branch_config): '''Create a jenkins job. :param branch: svn branch name (ex: branches/feature-one) :param template: the config of the template job to use :param config: global config (parsed yaml) :param branch_config: the effective config for this branch ''' print('\nprocessing branch: %s' % branch) # job names with '/' in them are problematic sanitized_branch = branch.replace('/', branch_config['namesep']) groups = branch_config['re'].match(branch).groups() # placeholders available to the 'substitute' and 'namefmt' options fmtdict = { 'branch': branch.split('/')[-1], 'path': branch.replace('/', branch_config['namesep']), 'path-orig': branch, } job_name = branch_config['namefmt'].format(*groups, **fmtdict) job = Job(job_name, branch, template, _main.jenkins) fmtdict['job_name'] = job_name print('. job name: %s' % job.name) print('. job exists: %s' % job.exists) try: scm_el = job.xml.xpath('scm[@class="hudson.scm.SubversionSCM"]')[0] except IndexError: msg = 'Template job %s is not configured to use SVN as an SCM' raise RuntimeError(msg % template) # :bug: # set branch el = scm_el.xpath('//remote')[0] el.text = path.join(config['repo'], branch) # set the branch that git plugin will locally checkout to el = scm_el.xpath('//local')[0] el.text = '.' # set the state of the newly created job job.set_state(branch_config['enable']) # since some plugins (such as sidebar links) can't interpolate the job # name, we do it for them job.substitute(list(branch_config['substitute'].items()), fmtdict) job.create(branch_config['overwrite'], config['dryrun']) if config['debug']: debug_refconfig(branch_config) return job_name
def create_job(ref, template, config, ref_config): '''Create a jenkins job. :param ref: hg branch name :param template: the config of the template job to use :param config: global config (parsed yaml) :param ref_config: the effective config for this branch ''' print('\nprocessing branch: %s' % ref) sanitized_ref = sanitize(ref, ref_config['sanitize']) sanitized_ref = sanitized_ref.replace('/', ref_config['namesep']) match = ref_config['re'].match(ref) groups, groupdict = match.groups(), match.groupdict() # placeholders available to the 'substitute' and 'namefmt' options fmtdict = { 'branch': sanitized_ref, 'branch-orig': ref, } job_name = ref_config['namefmt'].format(*groups, **merge(groupdict, fmtdict)) job = Job(job_name, ref, template, _main.jenkins) fmtdict['job_name'] = job_name print('. job name: %s' % job.name) print('. job exists: %s' % job.exists) try: scm_el = job.xml.xpath( 'scm[@class="hudson.plugins.mercurial.MercurialSCM"]')[0] except IndexError: msg = 'Template job %s is not configured to use Mercurial as an SCM' raise RuntimeError(msg % template) # :bug: # set branch el = scm_el.xpath('//branch')[0] el.text = ref # set the state of the newly created job job.set_state(ref_config['enable']) # since some plugins (such as sidebar links) can't interpolate the job # name, we do it for them job.substitute(list(ref_config['substitute'].items()), fmtdict, groups, groupdict) job.create(ref_config['overwrite'], config['dryrun']) if config['debug']: debug_refconfig(ref_config) return job_name
def create_job(ref, template, config, ref_config): '''Create a jenkins job. :param ref: git ref name (ex: refs/heads/something) :param template: the config of the template job to use :param config: global config (parsed yaml) :param ref_config: the effective config for this ref :returns: the name of the newly created job ''' print('\nprocessing ref: %s' % ref) shortref = re.sub('^refs/(heads|tags|remotes)/', '', ref) sanitized_ref = sanitize(ref, ref_config['sanitize']) sanitized_shortref = sanitize(shortref, ref_config['sanitize']) # job names with '/' in them are problematic (todo: consolidate with sanitize()) sanitized_ref = sanitized_ref.replace('/', ref_config['namesep']) sanitized_shortref = sanitized_shortref.replace('/', ref_config['namesep']) groups = ref_config['re'].match(ref).groups() # placeholders available to the 'substitute' and 'namefmt' options fmtdict = { 'ref': sanitized_ref, 'shortref': sanitized_shortref, 'ref-orig': ref, 'shortref-orig': shortref, } job_name = ref_config['namefmt'].format(*groups, **fmtdict) job = Job(job_name, ref, template, _main.jenkins) fmtdict['job_name'] = job_name print('. job name: %s' % job.name) print('. job exists: %s' % job.exists) try: scm_el = job.xml.xpath('scm[@class="hudson.plugins.git.GitSCM"]')[0] except IndexError: msg = 'Template job %s is not configured to use Git as an SCM' raise RuntimeError(msg % template) # :bug: # get remote name remote = scm_el.xpath('//hudson.plugins.git.UserRemoteConfig/name') remote = remote[0].text if remote else 'origin' # set branch el = scm_el.xpath('//hudson.plugins.git.BranchSpec/name')[0] # :todo: jenkins is being very caprecious about the branchspec # el.text = '%s/%s' % (remote, shortref) # :todo: el.text = shortref # set the branch that git plugin will locally checkout to el = scm_el.xpath('//localBranch') el = etree.SubElement(scm_el, 'localBranch') if not el else el[0] el.text = shortref # the original shortref (with '/') # set the state of the newly created job job.set_state(ref_config['enable']) # since some plugins (such as sidebar links) can't interpolate the job # name, we do it for them job.substitute(list(ref_config['substitute'].items()), fmtdict) job.create(ref_config['overwrite'], config['dryrun']) if config['debug']: debug_refconfig(ref_config) return job_name